264 words, 2 min read

When deploying applications with Docker Compose, it’s often useful to have your services managed directly by systemd. This allows you to start, stop, and restart your containers like any other system service, and ensures they come up automatically after a reboot.

The first choice you need to make is whether to run your containers in detached mode (docker compose up -d) or in the foreground (docker compose up). I usually prefer the foreground mode, since it integrates better with systemd’s logging and restart policies, but both approaches are valid.

If you want systemd to start Docker Compose in detached mode, create a file at /etc/systemd/system/docker-compose-app.service with the following content:

[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/srv/docker
ExecStart=/usr/bin/docker compose up -d
ExecStop=/usr/bin/docker compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target

This setup launches the containers once and leaves them running in the background.

If you prefer to run in the foreground and let systemd manage restarts and logs, use this variant:

[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
StartLimitIntervalSec=60
[Service]
WorkingDirectory=/srv/docker
ExecStart=/usr/bin/docker compose up
ExecStop=/usr/bin/docker compose down
TimeoutStartSec=0
Restart=on-failure
StartLimitBurst=3
[Install]
WantedBy=multi-user.target

Here, systemd will monitor the process and restart it if it fails, making it more resilient.

Once the service file is in place, enable it so it starts automatically on boot:

sudo systemctl enable docker-compose-app

You can also manually start or stop it using:

sudo systemctl start docker-compose-app
sudo systemctl stop docker-compose-app

If the service fails to start, make sure the path to the docker binary is correct. You can check with:

which docker

and adjust the ExecStart and ExecStop lines accordingly.