假设我有一个想要运行的Docker容器,然后我可以调用它
$ docker run ...
一切都很好。是否有一种内置的方法来运行容器,使其在系统崩溃并重新启动时自动重新启动?
如果是的话,这在Docker Compose中也可用吗?
假设我有一个想要运行的Docker容器,然后我可以调用它
$ docker run ...
一切都很好。是否有一种内置的方法来运行容器,使其在系统崩溃并重新启动时自动重新启动?
如果是的话,这在Docker Compose中也可用吗?
当前回答
你可以使用docker update——restart=on-failure <容器ID或名称>。
顾名思义,On -failure不仅会在失败时重新启动容器,还会在系统引导时重新启动容器。
根据文档,有多个重启选项:
Flag Description
no Do not automatically restart the container. (the default)
on-failure Restart the container if it exits due to an error, which manifests as a non-zero exit code.
always Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in restart policy details)
unless-stopped Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.
其他回答
如果你希望容器在没有用户登录的情况下也能启动(比如我只启动了VirtualBox虚拟机,不想每次都登录)。下面是我为Ubuntu 16.04 LTS执行的步骤。例如,我安装了一个oracle db容器:
$ docker pull alexeiled/docker-oracle-xe-11g
$ docker run -d --name=MYPROJECT_oracle_db --shm-size=2g -p 1521:1521 -p 8080:8080 alexeiled/docker-oracle-xe-11g
$ vim /etc/systemd/system/docker-MYPROJECT-oracle_db.service
并增加以下内容:
[Unit]
Description=Redis container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a MYPROJECT_oracle_db
ExecStop=/usr/bin/docker stop -t 2 MYPROJECT_oracle_db
[Install]
WantedBy=default.target
并在启动时启用该服务
sudo systemctl enable docker-MYPROJECT-oracle_db.service
欲了解更多信息,请访问https://docs.docker.com/engine/admin/host_integration/
是的,docker有重启策略,比如docker run——restart=always,可以处理这个问题。这也可以在compose中使用。Yml配置文件的重启:始终。
启动容器,并将其设置为在系统重新引导时自动重新启动
docker run -d --restart unless-stopped ecstatic_ritchie
其中,ecstasy atic_ritchie是指定感兴趣的容器的示例名称。使用docker ps -a列出所有容器名称。
使特定的运行容器在系统重新启动时自动启动
docker update --restart unless-stopped ecstatic_ritchie
使所有运行的容器在系统重新启动时自动启动
docker update --restart unless-stopped $(docker ps -q)
详见Docker主页
缺省情况下,重启策略为no。
对于已创建的容器,使用docker update更新重启策略。
docker update --restart=always 0576df221c0b
0576df221c0b是容器id。
你可以使用docker update——restart=on-failure <容器ID或名称>。
顾名思义,On -failure不仅会在失败时重新启动容器,还会在系统引导时重新启动容器。
根据文档,有多个重启选项:
Flag Description
no Do not automatically restart the container. (the default)
on-failure Restart the container if it exits due to an error, which manifests as a non-zero exit code.
always Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in restart policy details)
unless-stopped Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.