默認(rèn)情況下,Docker Desktop里容器默認(rèn)是不會(huì)自動(dòng)隨Docker Desktop的啟動(dòng)而啟動(dòng)的,不過Docker提供了容器的重新啟動(dòng)策略。
運(yùn)行PowerShell通過下面的命令設(shè)置容器的自動(dòng)啟動(dòng)(記得把“<containerName>”換成需要自動(dòng)啟動(dòng)的容器的名字,并要注意容器名字是區(qū)分大小寫的):
docker update --restart=always <containerName>

圖1. 命令運(yùn)行示例
關(guān)于重啟策略說明如下:
- no:缺省的設(shè)定,就是容器不會(huì)自動(dòng)啟動(dòng)
- always:只要容器沒有被人為關(guān)閉,只要它停了就自動(dòng)啟動(dòng)它
- unless-stopped:只要Docker的后臺(tái)關(guān)閉之前容器不是停止?fàn)顟B(tài)那么自動(dòng)啟動(dòng)該容器
- on-failure:如果Docker的后臺(tái)重啟或者容器以非0代碼退出那么重啟該容器
以下是以上重啟策略內(nèi)容的參考來源:
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也自動(dòng)啟動(dòng),打開Docker Desktop,點(diǎn)擊右上角設(shè)置Setting齒輪圖標(biāo),然后在常規(guī)General卡片勾選登錄后啟動(dòng)Docker(Start Docker Desktop when you log in)然后點(diǎn)右下角應(yīng)用并重啟Apply & Restart按鈕。

圖2. 設(shè)置自動(dòng)啟動(dòng)Docker Desktop
注意,如此設(shè)置后,Docker Desktop只有在用戶登錄后才會(huì)啟動(dòng),如果需要做無人值守啟動(dòng),需要將Windows也設(shè)成自動(dòng)登錄,這里就不探討了。