docker中文文档-自动启动容器
自动启动容器
你可以通过进程管理器来操容器 upstart, systemd and supervisor.
说明
如果你想通过进程管理器来管理你的容器,那么你需要开启一个带有 -r=false 的守护进程,那么docker就会在你重启主机的时候自动启动容器
When you have finished setting up your image and are happy with your running container, you may want to use a process manager to manage it. When your rundocker start -a docker will automatically attach to the process and forward all signals so that the process manager can detect when a container stops and correctly restart it.
下面是一些示例脚本
简单的Upstart脚本
In this example we’ve already created a container to run Redis with an id of 0a7e070b698b. To create an upstart script for our container, we create a file named/etc/init/redis.conf and place the following into it:
description "Redis container"
author "Me"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
script
# Wait for docker to finish starting up first.
FILE=/var/run/docker.sock
while [ ! -e $FILE ] ; do inotifywait -t 2 -e create $(dirname $FILE)
done
/usr/bin/docker start -a 0a7e070b698b
end script
Next, we have to configure docker so that it’s run with the option -r=false. Run the following command:
$ sudo sh -c "echo 'DOCKER_OPTS=\"-r=false\"' > /etc/default/docker"
简单的systemd脚本
[Unit]Description=Redis container
Author=Me
After=docker.service
[Service]Restart=always
ExecStart=/usr/bin/docker start -a 0a7e070b698b
ExecStop=/usr/bin/docker stop -t 2 0a7e070b698b
[Install]WantedBy=local.target