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 :
/lib/systemd/system/mariadb.service
[Unit]
Description=MariaDB
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=always
ExecStart=/usr/bin/docker run --rm \
--name=mariadb \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=test \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=user \
-v /var/lib/mysql:/var/lib/mysql \
mariadb:latest
ExecStop=-/usr/bin/docker stop mariadb # See UPDATEs 1 & 2
[Install]
WantedBy=multi-user.target
The service starts and stops properly in seconds when I use systemctl command. But when I shut the system down, it shows :
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.
Best Answer
A solution which is no longer maintained is systemd-docker, described as:
The problem of getting systemd-docker to work with recent Linux versions is discussed in the post Doesn't work with recent systemd and/or docker releases , where a user named james-cxx has reported success:
An alternative to systemd-docker might be to use rkt, described as: