假设我有一个想要运行的Docker容器,然后我可以调用它
$ docker run ...
一切都很好。是否有一种内置的方法来运行容器,使其在系统崩溃并重新启动时自动重新启动?
如果是的话,这在Docker Compose中也可用吗?
假设我有一个想要运行的Docker容器,然后我可以调用它
$ docker run ...
一切都很好。是否有一种内置的方法来运行容器,使其在系统崩溃并重新启动时自动重新启动?
如果是的话,这在Docker Compose中也可用吗?
当前回答
启动容器,并将其设置为在系统重新引导时自动重新启动
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主页
其他回答
你可以使用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.
1)首先,你必须在启动时启用docker服务
$ sudo systemctl enable docker
2)如果你有docker-compose .yml文件,添加restart: always,或者如果你有docker容器,添加restart=always,如下所示:
Docker run——restart=always并运行Docker容器
确保
如果您手动停止容器,它的重启策略将被忽略,直到Docker守护进程重新启动或容器手动重新启动。
请看Docker官方页面上的重启策略
3)如果你想启动docker-compose,所有的服务都在你重启系统时运行,所以你只运行下面的命令一次
$ docker-compose up -d
启动容器,并将其设置为在系统重新引导时自动重新启动
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主页
你可以运行一个总是重启的容器:
$ docker run -dit --restart unless-stopped <image name OR image hash>
如果你想改变一个正在运行的容器的配置,你应该通过以下方式更新它:
$ docker update --restart=<options> <container ID OR name>
如果你想查看容器的当前策略,首先执行下面的命令:
docker inspect gateway | grep RestartPolicy -A 3
毕竟,不要忘记在系统引导时启用已安装的docker守护进程:
$ systemctl enable docker
要查看重启策略的完整列表,请参见:重启策略
我在运行Linux系统时也遇到了类似的问题。在系统启动后,一个具有“unless-stopped”重启策略的容器将不会自动重启,除非我输入了一个以某种方式使用docker的命令,例如“docker ps”。我很惊讶,因为我以为这个命令只是报告一些状态信息。接下来我尝试了命令“systemctl status docker”。在一个没有运行docker命令的系统上,该命令报告如下:
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; disabled; vendor preset: enabled)
Active: inactive (dead) TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
在没有其他docker命令的情况下运行“docker ps”的系统上,我得到了以下信息:
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2020-11-22 08:33:23 PST; 1h 25min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 3135 (dockerd)
Tasks: 13
Memory: 116.9M
CGroup: /system.slice/docker.service
└─3135 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
... [various messages not shown ]
最有可能的解释是,在完全初始化和启动容器之前,Docker会等待一些Docker命令。您可以假定在容器所需的所有服务初始化后的某个时刻在systemd单元文件中运行“docker ps”。我通过放置一个名为docker-onboot的文件来测试这一点。将服务放在/lib/systemd/system目录下,内容如下:
[Unit]
# This service is provided to force Docker containers
# that should automatically restart to restart when the system
# is booted. While the Docker daemon will start automatically,
# it will not be fully initialized until some Docker command
# is actually run. This unit merely runs "docker ps": any
# Docker command will result in the Docker daemon completing
# its initialization, at which point all containers that can be
# automatically restarted after booting will be restarted.
#
Description=Docker-Container Startup on Boot
Requires=docker.socket
After=docker.socket network-online.target containerd.service
[Service]
Type=oneshot
ExecStart=/usr/bin/docker ps
[Install]
WantedBy = multi-user目标。
到目前为止(在一次测试中,启用了此服务),容器在计算机启动时启动。我没有尝试依赖docker。服务因为docker。在运行docker命令之前,服务不会启动。下一个测试将禁用docker-onboot(看看WantedBy依赖项是否会自动启动它)。