在Docker容器启动中运行多个命令

安纳贝

我有一个基于CMD为dockerfile的在ubuntu 16.04上运行的简单docker映像"/sbin/ejabberdctl foreground"为了使Docker容器在启动后仍然存活,我曾经在前台运行ejabberd服务器。但是,在启动容器之后,/sbin/ejabberdctl一旦ejabberdctl已经运行(例如ejabberdctl list_cluster我就需要执行另一个命令试图将两个命令都添加到bash脚本中,但是不起作用。试图运行/sbin/ejbberdctl start &,但也没有运行哪种方式挖?

Adiii

选项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

在选项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.shbash脚本,一旦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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Docker容器启动后运行命令

如何在启动时在 docker 容器上运行多个命令?

启动Docker服务时运行多个命令

在Jenkins中的Docker容器中运行命令

NPM 启动脚本从本地 shell 运行,但在 Docker 容器命令中失败

在Docker容器中运行protoc命令

无法在Docker容器中运行命令

如何在Docker容器中运行命令

在Azure容器中运行docker命令

如何在 docker 容器中并行运行多个命令,然后等待它们全部完成?

Docker CMD 命令在容器启动后未运行,而命令进入容器工作正常

如何对多个容器运行单个 docker exec 命令

容器启动后运行命令

从Docker容器运行docker命令

无法使用docker-compose在docker容器中运行命令

尝试在单个命令中在docker容器中运行chmod

在使用Jenkinsfile中的Dockerfile创建的Docker容器中运行命令

在Docker容器中以非root用户运行sudo命令

在Docker容器中以非root用户运行sudo命令

我可以在Docker容器环境中运行主机命令吗?

如何在Docker容器中运行npm命令?

无法在我的Docker容器中运行Curl命令

GitHub动作:如何在Docker容器中运行命令

在带有参数的docker容器中运行“ apk add”命令

Docker-如何在postgres容器中运行psql命令?

在Windows中运行jenkins容器的docker命令是什么

在 docker 容器中运行 php -S 命令(oneoff)

在 docker Ansible 容器中运行复杂的 linux 命令

Azure Docker容器-如何将启动命令传递给Docker运行?