Docker and systemd - service stopping after 10 seconds

﹥>﹥吖頭↗ 提交于 2019-12-11 03:05:49

问题


I'm having trouble getting a Docker container to stay up when it's started by systemd. When I start it manually with sudo docker start containername, it stays up without trouble, but when it's started via systemd with sudo systemctl start containername, it stays up for 10 seconds then mysteriously dies, leaving messages in syslog something like the following:

Mar 13 14:01:09 hostname docker[329]: time="2015-03-13T14:01:09Z" level="info" msg="POST /v1.17/containers/containername/stop?t=10"
Mar 13 14:01:09 hostname docker[329]: time="2015-03-13T14:01:09Z" level="info" msg="+job stop(containername)"

I am making the assumption that it's systemd killing the process, but I can't work out why it might be happening. The systemd unit file (/etc/systemd/system/containername.service) is pretty simple, as follows:

[Unit]
Description=MyContainer
After=docker.service
Requires=docker.service

[Service]
ExecStart=/usr/bin/docker start containername
ExecStop=/usr/bin/docker stop containername

[Install]
WantedBy=multi-user.target

Docker starts fine on boot, and it looks like it does even start the docker container, but no matter if on boot or manually, it then quits after exactly 10 seconds. Help gratefully received!


回答1:


Solution: The start command seems to need the -a (attach) parameter as described in the documentation when used in a systemd script. I assume this is because it by default forks to the background, although the systemd expect daemon feature doesn't appear to fix the issue.

from the docker-start manpage:

-a, --attach=true|false
   Attach container's STDOUT and STDERR and forward all signals to the process. The default is false.

The whole systemd script then becomes:

[Unit]
Description=MyContainer
After=docker.service
Requires=docker.service

[Service]
ExecStart=/usr/bin/docker start -a containername
ExecStop=/usr/bin/docker stop containername

[Install]
WantedBy=multi-user.target


来源:https://stackoverflow.com/questions/29034381/docker-and-systemd-service-stopping-after-10-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!