#StackBounty: #debian #docker #systemd #debian-stretch How to properly handle a Docker container as a systemd service?

Bounty: 100

I handle many services (databases, web servers…) implemented by docker containers via systemd units.
It works, but the system takes a very long time to shut down, apparently waiting for docker services to shut down.

Here is an example of a systemd unit I made in a virtual machine under Debian Stretch with Docker CE :



ExecStart=/usr/bin/docker run --rm 
    -p 3306:3306 
    -e MYSQL_DATABASE=test 
    -e MYSQL_USER=user 
    -e MYSQL_PASSWORD=user 
    -v /var/lib/mysql:/var/lib/mysql 
ExecStop=-/usr/bin/docker stop mariadb # See UPDATEs 1 & 2


The service starts and stops properly in seconds when I use systemctl command. But when I shut the system down, it shows :

VM screenshot 1
VM screenshot 2
VM screenshot 3

It lasts more than 3 minutes. It can be longer if there are many containers running via systemd.

The problem is not specific to MariaDB.

Is there a better method to handle docker containers via systemd, avoiding extra shutdown time ?

  • UPDATE 1 : As Bennett Hardwick suggested in comment, I tried removing the ExecStop directive from the definition of the service. It seems better since the service stops after 1min 30 and not 3min.

  • UPDATE 2 : I tried to add the option -t to docker stop command in ExecStop directive (https://docs.docker.com/engine/reference/commandline/stop/). It has no effect.

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.