erlang 启动
docker容器是否退出跟容器的主进程有关,主进程结束后,容器就exit了,那么erlang in docker时,erl shell做为主进程,erl shell 需以前台方式启动,此时docker run必须指定-t
以监听shell输入,否则docker run -d erlang erl
会立刻exit,应该用docker run -dt erlang erl
才能保持容器在后台运行。
当用docker swarm 创建服务时,不能指定-t
,那么erlang集群容器就会exit,此时需要将erl启动命令改为noshell 启动,即erl -noshell -name xxx
即可。
docker swarm中erlang集群互联
TODO
以下是各种脚本:
build.sh
#!/bin/bash
git checkout .
chmod +x rebar
chmod +x ./docker/start.sh
make deps
make clean
cp ./docker/Dockerfile ./../Dockerfile
docker rmi songcf/scene:v1 --force
docker build -t="songcf/scene:v1" ./../
rm -f ./../Dockerfile
Dockerfile
FROM erlang:latest
MAINTAINER Docker SongCF <fuxiao333@qq.com>
ENV SOURCE_DIR /jh/source/scene/
WORKDIR $SOURCE_DIR
COPY ./scene $SOURCE_DIR
EXPOSE 9901
EXPOSE 9911
RUN ["chmod", "+x", "rebar"]
RUN ["make"]
RUN ["make", "g"]
RUN ["make"]
ENTRYPOINT ["/bin/bash", "./docker/start.sh"]
CMD ["./docker/start.sh"]
start.sh
#!/bin/bash
# 1.更改leader_selection:app_id
sed -i "s/platform_scene_app_id/$LEADER_SELECT_APP_ID/g" scripts/scene.config
# 2.更改mysql配置
sed -i "s/localhost/$MYSQL_IP/g" scripts/scene.configsed -i "s/port, 3306/port, $MYSQL_PORT/g" scripts/scene.config
# 3.更改erlang node ip
# 按照自己的网络更改 |grep 10.0|grep /24
# ip a|grep inet|grep 10.0|grep /24|awk '{print $2}'|awk -F"/" '{print $1}'
NODE_IP=$(ip a|grep inet|grep ${SUBNET}|grep ${SUBNET_MASK}|awk '{print $2}'|awk -F"/" '{print $1}')
erl -noshell -pa ebin -pa deps/*/ebin -name scene@${NODE_IP} -config scripts/scene.config -s scene
boot
# sigle start
docker run -d \
--name platform_scene \
-w /jh/source/scene/ \
-e LEADER_SELECT_APP_ID=dockerswarm_platform_scene \
-e MYSQL_IP=127.0.0.1 \
-e MYSQL_PORT=3306 \
-e SUBNET=127.0 \
-e SUBNET_MASK=/8 \
songcf/scene:v1
# docker swarm service
docker network create -d overlay --subnet 10.0.9.0/24 --opt encrypted net-scene
curl -X DELETE http://127.0.0.1:10001/api/v1/leaderselect/dockerswarm_platform_scene
docker service create \
--name platform_scene \
--replicas 2 \
--network net-scene \
-e LEADER_SELECT_APP_ID=dockerswarm_platform_scene \
-e MYSQL_IP=192.168.31.96 \
-e MYSQL_PORT=3306 \
-e SUBNET=10.0 \
-e SUBNET_MASK=/24 \
songcf/scene:v1