Systemd - Run Utility Docker Container during `ExecStop =`

I'm testing CoreOS to make sure it meets our needs and things are going a little slow so far but OK. I like systemd, but it doesn't seem to work as expected - in particular on shutdown.

My goal

My goal is to run a script when the service starts and stops, which adds and removes records from our DNS server respectively for the service. It works when a service is started by the system at boot, or when it is manually started or shut down - but not when the system is rebooted or stopped ( shutdown -r now

, shutdown -h now

).

Here's a somewhat simplified version of the docker registry service I'm using for the example:

[Unit]
Description=Docker Registry
After=docker.service
Before=registry-ui.service
Wants=registry-ui.service

[Service]
Conflicts=halt.target poweroff.target reboot.target shutdown.target sleep.target
TimeoutStartSec=0
Restart=on-failure
ExecStartPre=-/usr/bin/docker kill Registry
ExecStartPre=-/usr/bin/docker rm Registry
ExecStartPre=-/usr/bin/docker run --rm myrepo:5000/tool runtool arg1 arg2
ExecStart=/usr/bin/docker run various args registry:latest
ExecStop=-/usr/bin/docker run --rm myrepo:5000/tool runtool arg1 arg2
ExecStop=-/usr/bin/docker stop Registry

[X-Fleet]
MachineID=coreos1

[Install]
WantedBy=multi-user.target
RequiredBy=registry-ui.service
Also=registry-ui.service

      

(This machine works in conjunction with another device, registry-ui.service. When it starts up, the other does the same.)

Pay attention to the line Conflicts=...

. I added it after spending time trying to figure out why the service is not closing properly. He didn't do anything. However, according to the docs , services have a Conflicts=shutdown.target

default string . When services conflict and one starts, the other shuts down - or as the docs say.

What did I miss? Why are my lines ExecStop=

not being executed?


Update

I have determined that the lines ExecStop=

are being executed. Using journalctl -u registry.service -n 200

, I got the following message:

Error response from daemon: Cannot start container 7b9083a3f81710febc24256b715fcff1e8146c867500c6e8ce4d170ad1cfd11a: dbus: connection closed by user

      

Which indicates that the problem is (as I suggested in the comments) that my docker container is not starting during shutdown. I added the following lines to the section [Unit]

:

[Unit]
After=docker.service docker.socket
Requires=docker.service docker.socket
...

      

Newlines don't affect the logctl error, so now my question becomes, is there a way to start the utility dock container before shutting down?

+3


source to share


2 answers


If I understand your purpose, you want to run DNS flush on server shutdown, and you are trying to do this in your systemd docker service file. Why don't you use the fleet for this task? try to create a float block that monitors your DNS server and when it detects that the server is unavailable you can run your scavenging tasks.

In a fleet, when you destroy a service with fleetctl destroy, the exec stop lines are not fired (similar to a power off). If you want to conduct a cleaning session, you usually achieve this by using a satellite service with these directives.

serviceFile.service



[Unit]
Wants=cleanup@serviceFile.service

      

cleanup@serviceFile.service

[Unit]
PartOf=%i.service

      

+1


source


Instead of running everything in one single file, you can start 2 different services and bind them with bindsTo so that they start and stop together: http://www.freedesktop.org/software/systemd/man/systemd.unit. html # BindsTo =



This would make two simple and smaller unit files, each dealing with one container. It would also make it easier to debug.

0


source







All Articles