使用Supervisord实现Docker Container的自启动

CentOS7 安装Supervisor

# 安装
sudo yum install python-setuptools -y
sudo easy_install supervisor

# 生成默认配置文件
sudo sh -c 'echo_supervisord_conf > /etc/supervisord.conf'

# 创建启动脚本
sudo vim /etc/rc.d/init.d/supervisord

下面是init脚本

#!/bin/bash 
. /etc/init.d/functions

DAEMON=/usr/bin/supervisord
PIDFILE=/var/run/supervisord.pid

[ -x "$DAEMON" ] || exit 0

start() {
        echo -n "Starting supervisord: "
        if [ -f $PIDFILE ]; then
                PID=`cat $PIDFILE`
                echo supervisord already running: $PID
                exit 2;
        else
                daemon  $DAEMON --pidfile=$PIDFILE -c /etc/supervisord.conf
                RETVAL=$?
                echo
                [ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
                return $RETVAL
        fi

}

stop() {
        echo -n "Shutting down supervisord: "
        echo
        killproc -p $PIDFILE supervisord
        echo
        rm -f /var/lock/subsys/supervisord
        return 0
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status supervisord
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $?

别忘了给脚本赋权

sudo chmod +x /etc/rc.d/init.d/supervisord

配置Supervisor

新建一份独立配置文件,比如 /media/storage/work/supervisord.ini, 配置各container的启动

docker start -a 表示附加到已有容器,或者启动新容器(如果未启动)

[program:zookeeper]
command=/usr/bin/docker start -a zookeeper

[program:env3_mysql_log]
command=/usr/bin/docker start -a env3_mysql_log

[program:stage_mysql_log]
command=/usr/bin/docker start -a stage_mysql_log

[program:stage_redis]
command=/usr/bin/docker start -a stage_redis

[program:webapi-1]
command=/usr/bin/docker start -a webapi-1

[program:webapi-2]
command=/usr/bin/docker start -a webapi-2

[program:webapi-3]
command=/usr/bin/docker start -a webapi-3

然后在 /etc/supervisord.conf 配置中引入此配置文件

[include]
files = /media/storage/work/supervisord.ini

最后启动supervisord服务

sudo service supervisord restart

你可能感兴趣的:(docker)