Automate tasks with docker-compose and systemd

posted on bydotcs

I have several tasks encapsulated in Docker containers which I need to trigger on a regular basis. The scheduling can be done with systemd. Both tools together have proven to be a quite powerful combination.

Example: Backup service

Let's assume we have a backup service that lives in /opt/backup. This folder contains a docker-compose.yaml file which contains the configuration of a backup service.

We need one systemd unit for the service and one for the timer. Both are shown below. The setup is pretty straight forward.

# /etc/systemd/system/backup.service
Description=Backup service

# Send mail in case of an error


ExecStart=/usr/local/bin/docker-compose up

# /etc/systemd/system/backup.timer
Description=Timer for backup service


# Time to wait after booting before we run first time

# Define a calendar event (see `man systemd.time`)
OnCalendar=*-*-* 03:00:00 Europe/Berlin


The service unit has a OnFailure hook which runs a separate systemd service in case the exit code of the service is non-zero. This idea comes from the Arch Wiki.
The status email service can be configured in a generic way such that it can be re-used in other systemd services as well.

# /etc/systemd/system/status-email-user@.service
Description=Status Email for %i to user

ExecStart=/usr/local/bin/systemd-email %i



/usr/bin/mail -Ssendwait -t <<ERRMAIL
To: $1
From: Monitor (systemd) <alert@example.tld>
Subject: $2
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8

$(systemctl status --full "$2")

Finally the timer needs to be started/enabled as usual:

systemctl start backup.timer
systemctl enable backup.timer

Show logs

Logs can be read with journalctl:

journalctl -b -u backup.service

All work licensed underunless otherwise stated.