Skip to content

Instantly share code, notes, and snippets.

@tsopokis
Forked from mosquito/README.md
Last active March 10, 2023 14:00
Show Gist options
  • Save tsopokis/d49e67e37416594e440c1cbbe88193b2 to your computer and use it in GitHub Desktop.
Save tsopokis/d49e67e37416594e440c1cbbe88193b2 to your computer and use it in GitHub Desktop.
Add doker-compose as a systemd unit

Docker compose as a systemd unit

Create file /etc/systemd/system/[email protected]. SystemD calling binaries using an absolute path. In my case is prefixed by /usr/bin, you should use paths specific for your environment.

[Unit]
Description=%i service with docker compose
PartOf=docker.service
After=docker.service

[Service]
Type=simple
WorkingDirectory=/opt/containers/%i
ExecStart=/usr/bin/docker compose up --force-recreate --remove-orphans
ExecStop=/usr/bin/docker compose down

[Install]
WantedBy=default.target

Place your docker-compose.yml into /opt/containers/<SERVICE_NAME> and call

systemctl start docker-compose@<SERVICE_NAME>

Docker cleanup timer with system

Create /etc/systemd/system/docker-cleanup.timer with this content:

[Unit]
Description=Docker cleanup timer

[Timer]
OnUnitInactiveSec=12h

[Install]
WantedBy=timers.target

And service file /etc/systemd/system/docker-cleanup.service:

[Unit]
Description=Docker cleanup
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
WorkingDirectory=/tmp
User=root
Group=root
ExecStart=/usr/bin/docker system prune -af

[Install]
WantedBy=multi-user.target

run systemctl enable docker-cleanup.timer for enabling the timer

JournalD support

Just add the following line to the /etc/docker/daemon.json:

{
    ...
    "log-driver": "journald",
    ...
}

And restart your docker service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment