我有一个基于CMD为dockerfile的在ubuntu 16.04上运行的简单docker映像"/sbin/ejabberdctl foreground"
。为了使Docker容器在启动后仍然存活,我曾经在前台运行ejabberd服务器。但是,在启动容器之后,/sbin/ejabberdctl
一旦ejabberdctl已经运行(例如ejabberdctl list_cluster
),我就需要执行另一个命令。试图将两个命令都添加到bash脚本中,但是不起作用。试图运行/sbin/ejbberdctl start &
,但也没有运行。哪种方式挖?
选项A:创建一个简单的bash脚本,该脚本运行container和list_cluster,而无需修改ejabberd docker映像的入口点。
#!/bin/bash
if [ "${1}" = "remove_old" ]; then
echo "removing old ejabberd container"
docker rm -f ejabberd
fi
docker run --rm --name ejabberd -d -p 5222:5222 ejabberd/ecs
sleep 5
echo -e "*******list_cluster******"
docker exec -it ejabberd ash -c "/home/ejabberd/bin/ejabberdctl list_cluster"
在选项B中,您需要修改ejabberd官方映像入口点,因为它不允许您在启动时运行多个脚本。因此,在稍加修改的情况下在启动时添加脚本。
https://github.com/processone/docker-ejabberd/blob/master/ecs/Dockerfile
我建议使用仅30 MB的ejabberd的官方高山映像,而不是Ubuntu。https://hub.docker.com/r/ejabberd/ecs/
该演示也可以在Ubuntu上进行修改,但这已针对高山ejabberd官方映像进行了测试。
如果对群集感兴趣,请使用ejabberd官方映像作为基础映像,ENV [email protected]
并用于主节点。
From ejabberd/ecs:latest
USER root
RUN whoami
COPY supervisord.conf /etc/supervisord.conf
RUN apk add supervisor
RUN mkdir -p /etc/supervisord.d
COPY pm2.conf /etc/supervisord.d/ejabberd.conf
COPY start.sh /opt/ejabberd/start.sh
RUN chmod +x /opt/ejabberd/start.sh
ENV [email protected]
ENTRYPOINT ["supervisord", "--nodaemon", "--configuration", "/etc/supervisord.conf"]
现在创建主管配置文件
[unix_http_server]
file = /tmp/supervisor.sock
chmod = 0777
chown= nobody:nogroup
[supervisord]
logfile = /tmp/supervisord.log
logfile_maxbytes = 50MB
logfile_backups=10
loglevel = info
pidfile = /tmp/supervisord.pid
nodaemon = true
umask = 022
identifier = supervisor
[supervisorctl]
serverurl = unix:///tmp/supervisor.sock
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[include]
files = /etc/supervisord.d/*.conf
现在创建ejabberd.conf,以使用supervisorsd启动ejabberd 。请注意,如果您想加入集群,这里的join cluster参数用于加入集群。如果不需要,请将其删除。
[supervisord]
nodaemon=true
[program:list_cluster]
command: /opt/ejabberd/start.sh join_cluster
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
[program:ejabberd]
command=/home/ejabberd/bin/ejabberdctl foreground
autostart=true
priority=1
autorestart=true
username=ejabberd
exitcodes=0 , 4
一个/opt/ejabberd/start.sh
bash脚本,一旦ejabberd启动,它将列出list_cluster,并且如果在调用脚本时传递了参数,也可以加入join_cluster。
#!/bin/sh
until nc -vzw 2 localhost 5222; do sleep 2 ;echo -e "Ejabberd is booting....."; done
if [ $? -eq 0 ]; then
########## Once ejabberd is up then list the cluster ##########
echo -e "***************List_Cluster start***********"
/home/ejabberd/bin/ejabberdctl list_cluster
echo -e "***************List_Cluster End***********"
########## If you want to join cluster once up as pass the master node as ENV then pass first param like join_cluster ##########
if [ "${1}" == "join_cluster" ]; then
echo -e "***************Joining_Cluster start***********"
/home/ejabberd/bin/ejabberdctl join_cluster ejabberd@$MASTER_NODE
echo -e "***************Joining_Cluster End***********"
fi
else
echo -e "**********Ejabberd is down************";
fi
运行Docker容器
docker build -t ejabberd .
docker run --name ejabberd --rm -it ejabberd
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句