默认情况下,Docker Desktop里容器默认是不会自动随Docker Desktop的启动而启动的,不过Docker提供了容器的重新启动策略。
运行PowerShell通过下面的命令设置容器的自动启动(记得把“<containerName>”换成需要自动启动的容器的名字,并要注意容器名字是区分大小写的):
docker update --restart=always <containerName>
图1. 命令运行示例
关于重启策略说明如下:
- no:缺省的设定,就是容器不会自动启动
- always:只要容器没有被人为关闭,只要它停了就自动启动它
- unless-stopped:只要Docker的后台关闭之前容器不是停止状态那么自动启动该容器
- on-failure:如果Docker的后台重启或者容器以非0代码退出那么重启该容器
以下是以上重启策略内容的参考来源:
Docker restart policies
There are following restart policies for Docker containers:
- no: The default behavior is to not start containers automatically
- always: Always restart a stopped container unless the container was stopped explicitly
- unless-stopped: Restart the container unless the container was in stopped state before the Docker daemon was stopped (explained later)
- on-failure: Restart the container if it exited with a non-zero exit code or if the docker daemon restarts
As I mentioned, if you don't explicitly add a restart policy, it goes with "no", meaning containers won't be restarted automatically.
常情情况下,我们需要让Docker Desktop也自动启动,打开Docker Desktop,点击右上角设置Setting齿轮图标,然后在常规General卡片勾选登录后启动Docker(Start Docker Desktop when you log in)然后点右下角应用并重启Apply & Restart按钮。
图2. 设置自动启动Docker Desktop
注意,如此设置后,Docker Desktop只有在用户登录后才会启动,如果需要做无人值守启动,需要将Windows也设成自动登录,这里就不探讨了。
网友评论