Skip to main content

Manage Docker Compose with systemd on Linux

Create the systemd file

Make sure to change the COMPOSE_FILE variable for your specific setup.

## Static Variables
COMPOSE_FILE=/path/to/compose.yaml

## Dynamic Variables
COMPOSE_DIR=$( dirname ${COMPOSE_FILE} )
APP=$( basename ${COMPOSE_DIR} )

tee /etc/systemd/system/$APP.service << EOF
[Unit]
Description=$APP Service
After=docker.service
Requires=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=${COMPOSE_DIR}
ExecStart=/usr/bin/bash -c "docker compose -f ${COMPOSE_FILE} start"
ExecStop=/usr/bin/bash -c "docker compose -f ${COMPOSE_FILE} stop"

[Install]
WantedBy=multi-user.target
EOF

## Enable app on startup
systemctl enable $APP --now

The app should be up and running now, but you can also reboot to make sure it comes back online automatically.

Back