Docker Community Forums

Share and learn in the Docker community.

Running Docker Containers with Systemd question


(Mikedavem) #1

Hi all,

This is my first post so apologize if did some mistakes about categories or if some info are missing :slight_smile:

I’m looking for information about how systemd interacts with Docker containers. I implemented systemd unit file for one of my container.

My system unit file

[Unit]
Description=Docker Container %I
Requires=docker.service
After=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStart=/usr/bin/docker start -a %i
ExecStop=/usr/bin/docker stop -t 2 %i

[Install]
WantedBy=default.target

My commands to enable the service

sudo cp ./service-template /etc/systemd/system/docker-container@sql.service
sudo systemctl enable docker-container@sql
sudo systemctl start docker-container@sql

It works like a charm except in one case:

Let’s say I stop my docker container with the following command

sudo systemctl stop docker-container@sql

And thenI start my container with docker command instead

sudo docker start sql

In this case status from systemd is not as expected (Active: Active running)

sudo systemctl status docker-container@sql
● docker-container@sql.service - Docker Container sql
   Loaded: loaded (/etc/systemd/system/docker-container@sql.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2018-06-20 08:12:12 CEST; 22s ago

I have to run the command to revert back to a “normal” situation (from a systemd perspective)

sudo systemctl restart docker-container@sql

I mean normal situation because now if I try to stop my docker container from …

sudo docker stop sql

… systemd comes into play and restarts my container as expected.

My docker version

Client:
 Version:       17.12.0-ce
 API version:   1.35
 Go version:    go1.9.2
 Git commit:    c97c6d6
 Built: Wed Dec 27 20:10:14 2017
 OS/Arch:       linux/amd64

Server:
 Engine:
  Version:      17.12.0-ce
  API version:  1.35 (minimum version 1.12)
  Go version:   go1.9.2
  Git commit:   c97c6d6
  Built:        Wed Dec 27 20:12:46 2017
  OS/Arch:      linux/amd64
  Experimental: false

Thanks for your feedback :slight_smile:


(Antoinetran) #2

I do not recommend systemd with docker unless we want a container with multiple processes.

There are some options you might have missed:
docker run -ti -e container=docker
is necessary for systemd to know we are in docker. Plus the stop signal needs to be overrided for systemd stop (I remember RTMIN+3 in compose file).


(Vyadav) #3

I think you may consider using centos/systemd as base. In fact the recommendation is dedicate the container per service (that’s the theme behind docker and micro-services).