How to stop a container

166 views
Skip to first unread message

Scott Robertson

unread,
Mar 11, 2014, 1:52:20 PM3/11/14
to coreo...@googlegroups.com
What's the proper way to write a systemd.service file to get a container to stop correctly?
I'm having issues stopping containers gracefully with systemctl
If I use the hello example:
-------------
[Unit]
Description=My Service
After=docker.service
Requires=docker.service

[Service]
Restart=always
RestartSec=10s
ExecStart=/usr/bin/docker run busybox /bin/sh -c "while true; do echo Hello World; sleep 1; done"

[Install]
WantedBy=local.target
----
Then run systemctl stop hello.service it throws this in the logs

Mar 11 17:47:29 localhost systemd[1]: hello.service stopping timed out. Killing.
Mar 11 17:47:29 localhost systemd[1]: hello.service: main process exited, code=killed, status=9/KILL
Mar 11 17:47:29 localhost systemd[1]: Stopped My Service.
Mar 11 17:47:29 localhost systemd[1]: Unit hello.service entered failed state.

But docker ps shows the container still running.

Alex Polvi

unread,
Mar 11, 2014, 2:28:37 PM3/11/14
to coreos-dev
Scott, You'll need to add an ExecStop directive in your unit file. There are a few examples on these various docs.


(looks like we need to update our initial example in that doc with this, too)

-Alex

Scott Robertson

unread,
Mar 11, 2014, 6:03:50 PM3/11/14
to coreo...@googlegroups.com
I thought that might be the case ... the part I'm trying to figure out is how to stop an anonymous container. Here's my current approach

[Unit]
Description=6Si Front End Service
Requires=mysql.service
After=mysql.service


[Service]
RestartSec=10s

# Ensure ntropy DB is loaded before starting ui
ExecStartPre=/home/core/share/scripts/admin restore_mysql ntropy

ExecStart=/usr/bin/docker run \
    --cidfile=/var/run/%n.cid \
    -rm \
    -dns 172.12.8.150 \
    -e DEBUG=TRUE \
    -e DB_NAME=ntropy \
    -e DB_USER=root \
    -e DB_PASSWORD= \
    -p 80:8000 \
    -link mysql:db \
    sixsense/ui 


ExecStop=/bin/bash -c "/usr/bin/docker stop $(cat /var/run/%n.cid)"
ExecStop=/bin/rm /var/run/%n.cid 


[Install]
WantedBy=local.target


----

This kind of works though whenever I run systemctl stop, systemd claims the process failed and immediately tries to restart it. The Exec commands are all exiting with return codes of 0.


However,this seems counterintuitive to me. Shouldn't calling docker run without the the '-d' keep the process in the foreground. Shouldn't systemd then kill the docker container for me when I call systemctl stop without me needing to have an explicit script.

Alex Polvi

unread,
Mar 15, 2014, 2:07:07 PM3/15/14
to coreos-dev
On Tue, Mar 11, 2014 at 3:03 PM, Scott Robertson <srobe...@codeit.com> wrote:
This kind of works though whenever I run systemctl stop, systemd claims the process failed and immediately tries to restart it. The Exec commands are all exiting with return codes of 0.


Try adding:

KillMode=none

This will cause systemd to just run the ExecStop commands and do nothing else, I think. 
 

However,this seems counterintuitive to me. Shouldn't calling docker run without the the '-d' keep the process in the foreground. Shouldn't systemd then kill the docker container for me when I call systemctl stop without me needing to have an explicit script.

Yes, that'd be nice! Docker currently has an array of signal handling issues that are being worked through. The main issue is that docker itself has the child process, not pid 1 (systemd). There are plugins and such in the works for this, but nothing clean yet.

-Alex
Reply all
Reply to author
Forward
0 new messages