安装s6及依赖,注意make版本一定要4.0以上.
wget http://skarnet.org/software/skalibs/skalibs-2.2.0.0.tar.gz
tar zxvf skalibs-2.2.0.0.tar.gz
cd skalibs-2.2.0.0
./configure && make && make install
wget http://skarnet.org/software/execline/execline-2.0.1.1.tar.gz
tar zxvf execline-2.0.1.1.tar.gz
cd execline-2.0.1.1
./configure && make && make install
wget http://skarnet.org/software/s6/s6-2.0.1.0.tar.gz
tar zxvf s6-2.0.1.0.tar.gz
cd s6-2.0.1.0
./configure && make && make install
s6启动时会依次执行目录下run命令:
mkdir -p /etc/s6/nginx/
mkdir -p /etc/s6/.s6-svscan
#启动命令
cat > /etc/s6/nginx/run << EOF
#!/bin/bash
nginx -c /etc/nginx/nginx.conf
EOF
chmod +x /etc/s6/nginx/run
#关闭命令
cat > /etc/s6/nginx/finish << EOF
#!/bin/bash
kill `cat /var/run/nginx.pid`
EOF
chmod +x /etc/s6/nginx/finish
注意程序要以foreground非守护方式启动,如nginx要在配置文件添加:
daemon off;
当docker容器关闭时s6执行.s6-svscan/finish:
cat > /etc/s6/.s6-svscan/finish << EOF
#!/bin/bash
for file in /etc/s6/*/finish; do
$file
done
for service in /etc/s6/* ; do
s6-svwait -d $service
done
EOF
chmod +x /etc/s6/.s6-svscan/finish
docker容器启动时执行命令:
s6-svscan /etc/s6/
#-c 最大service数
#-t 重新扫描间隔,如果程序关闭会自动重启,毫秒,默认5秒
进程控制,重启:
s6-svc -t /etc/s6/nginx
关闭不重启:
s6-svc -d /etc/s6/nginx
更多:http://skarnet.org/software/s6/s6-svc.html
参考:
http://blog.tutum.co/2014/12/02/docker-and-s6-my-new-favorite-process-supervisor/
http://kfei.logdown.com/posts/245469-using-s6-as-the-init-process-for-muliple-service-docker-container