docker-compose as systemd service

In this article i will show you how to use docker compose with systemd in order to start docker containers as systemd services

docker-compose as systemd service
Photo by James Baldwin on Unsplash

In this article i will show you how to use docker compose with systemd in order to start docker containers as systemd services

first create this file /etc/systemd/system/docker-compose@.service and enter the following[Unit]
Description=%i service with docker compose
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=true
WorkingDirectory=/etc/docker/compose/%i
ExecStart=/usr/local/bin/docker-compose up -d --remove-orphans
ExecStop=/usr/local/bin/docker-compose down

[Install]
WantedBy=multi-user.target

Note that you might need to adjust your docker-compose directories to match your environment.

Now lets assume that we have a docker-compose.yml file that start two containers, a grafana and an influxdb container, create the following directorymkdir -p /etc/docker/compose/grafana_influxdb

Place your docker compose file inside this dir and we are done!, now we can use the usual systemd commands to start / stop docker-compose configured containers

start

sudo systemctl start docker-compose@grafana_influxdb

stop

sudo systemctl stop docker-compose@grafana_influxdb

You can always verify the actual status with either docker ps or systemctl status commands.

I hope you found this very short article useful