Linux β€’ Infrastructure & Tunnels

How to Run Docker Compose as a Systemd Service

Running Docker Compose as a systemd service guarantees that your multi-container stack boots automatically when the Docker daemon is ready, and issues `docker compose down` gracefully during host reboot.

/etc/systemd/system/docker-compose-app.service
[Unit]
Description=Docker Compose Multi-Container Stack
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/my-stack
ExecStart=/usr/bin/docker compose up -d --remove-orphans
ExecStop=/usr/bin/docker compose down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target
Customize:
User:
WorkingDir:
ExecStart:

Systemctl Lifecycle & Journalctl Monitoring

sudo systemctl daemon-reloadRegister compose service
sudo systemctl enable --now docker-compose-appStart docker compose stack and persist on boot
sudo systemctl stop docker-compose-appGracefully run docker compose down

Linux & Systemd Production Best Practices

  • `RemainAfterExit=yes` is essential for `Type=oneshot` services; it tells systemd to treat the service as active even after the initial `up -d` command completes.
  • Ensure `Requires=docker.service` so systemd does not attempt to launch before the Docker daemon socket is initialized.

Frequently Asked Questions About Docker Compose Systemd Service

Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

Systemd integrates compose with OS-level shutdown sequences, ensures clean volume flushing, and allows standard `systemctl` monitoring alongside other host services.