Docker中运行多个进程时的处理

通常,Docker容器适合运行单个进程,但是很多时候我们需要在Docker容器中运行多个进程。这时有两种不同方法来运行多进程容器:使用shell脚本或者supervisor,两种方法都很简单,各有优劣,只是有一些值得注意的细节。

1.shell 脚本

原文链接:https://blog.csdn.net/yibuchen/article/details/80160464

写一个脚本multiple_thread.sh,脚本功能运行两个python程序,将运行结果保存到log文件中。脚本内容如下

#!/bin/bash
# Start the first process
nohup python -u /tmp/thread1.py > /tmp/thread1.log 2>&1 &
ps aux |grep thread1 |grep -q -v grep
PROCESS_1_STATUS=$?
echo "thread1 status..."
echo $PROCESS_1_STATUS
if [ $PROCESS_1_STATUS -ne 0 ]; then
echo "Failed to start my_first_process: $PROCESS_2_STATUS"
exit $PROCESS_1_STATUS
fi
sleep 5
# Start the second process
nohup python -u /tmp/thread2.py > /tmp/thread2.log 2>&1 &
ps aux |grep thread2 |grep -q -v grep
PROCESS_2_STATUS=$?
echo "thread2 status..."
echo $PROCESS_2_STATUS
if [ $PROCESS_2_STATUS -ne 0 ]; then
echo "Failed to start my_second_process: $PROCESS_2_STATUS"
exit $PROCESS_2_STATUS
fi
# 每隔60秒检查进程是否运行
while sleep 60; do
ps aux |grep thread1 |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep thread2 |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit 1
fi

下一步制作Dockerfile:       

FROM centos:latest
COPY thread1.py /tmp/thread1.py
COPY thread2.py /tmp/thread2.py
COPY multiple_thread.sh /tmp/multiple_thread.sh
CMD bash /tmp/multiple_thread.sh

————————————————

使用 Supervisor 来管理进程

原文:https://blog.csdn.net/bbwangj/article/details/81059730

Docker容器在启动的时候开启单个进程,比如,一个 ssh 或者 apache 的 daemon 服务。但我们经常需要在一个机器上开启多个服务,这可以有很多方法,最简单的就是把多个启动命令放到一个启动脚本里面,启动的时候直接启动这个脚本,另外就是安装进程管理工具。

本小节将使用进程管理工具 supervisor 来管理容器中的多个进程。使用Supervisor可以更好的控制、管理、重启我们希望运行的进程。在这里我们演示一下如何同时使用 ssh 和 apache 服务。

配置

首先创建一个Dockerfile,内容和各部分的解释如下。

  1. FROM ubuntu:13.04

  2. MAINTAINER examples@docker.com

  3. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list

  4. RUN apt-get update

  5. RUN apt-get upgrade -y

安装 ssh、apache 和 supervisor

 RUN apt-get install -y openssh-server apache2 supervisor
  1. RUN mkdir -p /var/run/sshd

  2. RUN mkdir -p /var/log/supervisor

这里安装 3 个软件,还创建了 2 个 ssh 和 supervisor 服务正常运行所需要的目录。

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

添加 supervisord 的配置文件,并复制配置文件到对应目录下面。

  1. EXPOSE 22 80

  2. CMD ["/usr/bin/supervisord"]

这里我们映射了 22 和 80 端口,使用 supervisord 的可执行路径启动服务。

supervisor配置文件内容

  1. [supervisord]

  2. nodaemon=true

  3. [program:sshd]

  4. command=/usr/sbin/sshd -D

  5. [program:apache2]

  6. command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"

配置文件包含目录和进程,第一段 supervsord 配置软件本身,使用 nodaemon 参数来运行。第二段包含要控制的 2 个服务。每一段包含一个服务的目录和启动这个服务的命令。

使用方法

创建镜像。

$ sudo docker build -t test/supervisord .

启动 supervisor 容器。

  1. $ sudo docker run -p 22 -p 80 -t -i test/supervisords

  2. 2013-11-25 18:53:22,312 CRIT Supervisor running as root (no user in config file)

  3. 2013-11-25 18:53:22,312 WARN Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing

  4. 2013-11-25 18:53:22,342 INFO supervisord started with pid 1

  5. 2013-11-25 18:53:23,346 INFO spawned: 'sshd' with pid 6

  6. 2013-11-25 18:53:23,349 INFO spawned: 'apache2' with pid 7

使用 docker run 来启动我们创建的容器。使用多个 -p 来映射多个端口,这样我们就能同时访问 ssh 和apache 服务了。

可以使用这个方法创建一个只有 ssh 服务的基础镜像,之后创建镜像可以使用这个镜像为基础来创建。

其他更多第三方等方式:

https://www.cnblogs.com/sunsky303/p/11046681.html

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值