Skip to content

Instantly share code, notes, and snippets.

@izznogooood
Last active April 9, 2022 16:32
Show Gist options
  • Save izznogooood/5d2b416ea6242c6f170047f23c9dc2a1 to your computer and use it in GitHub Desktop.
Save izznogooood/5d2b416ea6242c6f170047f23c9dc2a1 to your computer and use it in GitHub Desktop.
How to use systemd as a docker manager. Nextcloud example with backup.

I have always found systemd to be a reliable manager for my docker containers, here is a few examples that should be enough to get the general idea.

I thought a symbiotic example would be best as that's about as complicated as it gets.

Some basic systemd usage skills like how to use systemctl and deamon-reload are expeted. (Remember NOT! to deamonize the containers! -d --deamonize in the docker command.)

MariaDB

[Unit]
Description=MariaDB
Documentation=https://hub.docker.com/_/mariadb/
After=network.target docker.socket
Requires=docker.socket

[Service]
RestartSec=10
Restart=on-failure

Environment="NAME=mariadb"
Environment="IMG=mariadb:latest"

# Pull new image for updates
ExecStartPre=-/usr/bin/docker pull $IMG

# Clean-up bad state if still hanging around
ExecStartPre=-/usr/bin/docker rm -f $NAME

# Main process
ExecStart=/usr/bin/docker run \
  --name $NAME \
  -p 3306:3306 \
  -v /opt/docker/mariadb:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=<PASSWORD> \
  $IMG

# Large / complicated images causes a timeout because of the 
# time it takes to download and extract / run.
TimeoutStartSec=600

# Stop Service
ExecStop=/usr/bin/docker stop $NAME

#Prevent systemd from killing the service
KillMode=none

[Install]
WantedBy=multi-user.target

phpMyAdmin (DB Manager)

[Unit]
Description=PHPmyadmin
Documentation=https://hub.docker.com/r/phpmyadmin/phpmyadmin/
After=network.target docker.socket docker-mariadb.service
Requires=docker.socket docker-mariadb.service

[Service]
RestartSec=10
Restart=on-failure

Environment="NAME=phpmyadmin"
Environment="IMG=phpmyadmin/phpmyadmin"

# Pull new image for updates
ExecStartPre=-/usr/bin/docker pull $IMG

# Clean-up bad state if still hanging around
ExecStartPre=-/usr/bin/docker rm -f $NAME

# Main process
ExecStart=/usr/bin/docker run \
  --name $NAME \
  --link mariadb:db \
  -p 8081:80 \
  $IMG

# Large / complicated images causes a timeout because of the 
# time it takes to download and extract / run.
TimeoutStartSec=600

# Stop Service
ExecStop=/usr/bin/docker stop $NAME

[Install]
WantedBy=multi-user.target

Nextcloud

[Unit]
Description=Nextcloud
Documentation=https://hub.docker.com/_/nextcloud/
After=network.target docker.socket docker-mariadb.service
Requires=docker.socket docker-mariadb.service

[Service]
RestartSec=10
Restart=on-failure

Environment="NAME=nextcloud"
Environment="IMG=nextcloud"

# Pull new image for updates
ExecStartPre=-/usr/bin/docker pull $IMG

# Clean-up bad state if still hanging around
ExecStartPre=-/usr/bin/docker rm -f $NAME

# Main process
ExecStart=/usr/bin/docker run \
  --name $NAME \
  -p 8080:80 \
  -v /opt/docker/nextcloud:/var/www/html \
  -v /mnt/Nextcloud/data:/var/www/html/data \
  $IMG

# Large / complicated images causes a timeout because of the 
# time it takes to download and extract / run.
TimeoutStartSec=600

# Stop Service
ExecStop=/usr/bin/docker stop $NAME

#Prevent systemd from killing the service
KillMode=none

[Install]
WantedBy=multi-user.target

Bash backup script

(backs up content of /opt/scripts/backup-files.list) Assumes things like all container services starts with docker-. Make sure to read through.

#!/bin/bash
# System Backup #

# What to backup. 
backupfile="/opt/scripts/backup-files.list"

# Where to backup to.
dest="/mnt/Backup/auto/harbor"

# Create archive filename.
day=$(date +%m-%d-%Y)
hostname=$(hostname -s)
archive_file="$hostname-backup-$day.tar.gz"

# Print start status message.
echo "*** Backup started -- Stoping containers ***"

#Stop systemd docker services
systemctl stop $(ls /etc/systemd/system/ | grep 'docker-')

# Backup the files using tar.
tar czf $dest/$archive_file --files-from=$backupfile

# Print end status message.
echo "*** Backup finished -- Starting containers ***"

# Start systemd docker services
systemctl start $(ls /etc/systemd/system/ | grep 'docker-')

# Notify pushbullet of outcome

if [ -f $dest/$archive_file ]; then

echo "*** Backup Succsess, notifying pushbullet ***"
curl --header 'Access-Token: <API-KEY>' \
     --header 'Content-Type: application/json' \
     --data-binary '{"body":"Backup done.","title":"Harbor","type":"note"}' \
     --request POST \
     https://api.pushbullet.com/v2/pushes

else

echo "*** !!!Backup failed!!!, notifying pushbullet  ***"
curl --header 'Access-Token: <API-KEY>' \
     --header 'Content-Type: application/json' \
     --data-binary '{"body":"Backup FAILED!!!.","title":"Harbor","type":"note"}' \
     --request POST \
     https://api.pushbullet.com/v2/pushes
fi

# Adding line for easier debuging
echo "--------------------"

Systemd timer and service for backup

Timer

Enable the timer, not the service!

[Unit]
Description=Run docker-backup weekly

[Timer]
OnCalendar=Mon *-*-* 05:00:00
Persistent=true

[Install]
WantedBy=timers.target

Service

[Unit]
Description=Backup Docker data volumes and unit files.

[Service]
Type=oneshot
ExecStart=/bin/bash /opt/scripts/backup.sh
TimeoutSec=infinity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment