Systemd template for Docker containers

Here’s a systemd service template for running Dockerized services, for example the Prometheus Blackbox Exporter.

Put this in a file named /etc/systemd/system/blackbox-exporter.service:

[Unit]
Description=Blackbox Exporter
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker stop blackbox_exporter
ExecStartPre=-/usr/bin/docker rm blackbox_exporter
ExecStartPre=/usr/bin/docker pull prom/blackbox-exporter
ExecStart=/usr/bin/docker run --rm --name blackbox_exporter --net=host -v /etc/blackbox-exporter:/etc/blackbox-exporter prom/blackbox-exporter --config.file=/etc/blackbox-exporter/blackbox.yml
ExecStop=/usr/bin/docker kill blackbox_exporter

[Install]
WantedBy=multi-user.target

Then the usual systemctl daemon-reload and systemctl start blackbox-exporter shenanigans, and you’ve got yourself a dockerized blackbox container nicely running under systemd.