n00b
n00b

Reputation: 127

Docker and systemd - service stopping after 10 seconds

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!

Upvotes: 4

Views: 4251

Answers (1)

n00b
n00b

Reputation: 127

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

Upvotes: 3

Related Questions