我在我的 Ubuntu 服务器上运行了许多 docker 容器。它们由 systemd 控制。
如果我运行systemctl stop [email protected]
容器会优雅地关闭。journalctl 的输出如下(逆时输出,最新在上):
Nov 05 10:58:11 ubox0 systemd[1]: Stopped Docker container startup for mw/salcom/app.
Nov 05 10:58:11 ubox0 docker[3622]: mw-salcom-app
Nov 05 10:58:09 ubox0 docker[3470]: [Thu Nov 05 10:58:09.921033 2020] [mpm_prefork:notice] [pid 1] AH00169: caught SIGTERM, shutting down
Nov 05 10:58:09 ubox0 systemd[1]: Stopping Docker container startup for mw/salcom/app...
但是,如果我执行shutdown -h now
,我会得到:
Nov 05 10:51:04 ubox0 systemd[1]: Stopped Docker container startup for mw/salcom/app.
Nov 05 10:51:04 ubox0 systemd[1]: [email protected]: Failed with result 'timeout'.
Nov 05 10:51:04 ubox0 systemd[1]: [email protected]: Main process exited, code=killed, status=9/KILL
Nov 05 10:51:04 ubox0 systemd[1]: [email protected]: Killing process 2077 (docker) with signal SIGKILL.
Nov 05 10:51:04 ubox0 systemd[1]: [email protected]: State 'stop-sigterm' timed out. Killing.
Nov 05 10:50:04 ubox0 systemd[1]: [email protected]: Stopping timed out. Terminating.
Nov 05 10:49:04 ubox0 systemd[1]: Stopping Docker container startup for mw/salcom/app...
容器没有停止,该过程最终在 2 分钟后超时,容器被中止。
这是什么原因?
我希望我的容器能够正常关闭,以便在其中运行的服务停止而不会丢失数据。
我的服务文件是:
[Unit]
Description=Docker container startup for %I
Requires=docker.service
After=docker.service
[Service]
TimeoutStartSec=40
Restart=always
# wait a while before restarting in case we are doing a restore;
# this includes a 'docker stop' command after we restore the data
RestartSec=60
ExecStartPre=-/usr/bin/docker stop %i
ExecStart=/usr/bin/docker start -a %i
ExecStop=/usr/bin/docker stop %i
TimeoutStopSec=60
[Install]
WantedBy=multi-user.target
更新在我进一步搜索之后,我发现这篇文章如何正确处理 Docker 容器作为系统服务?描述了同样的问题,但那里没有答案。