Skip to content

Instantly share code, notes, and snippets.

@rbuckland
Last active April 15, 2021 22:41
Show Gist options
  • Save rbuckland/99b29287c802a25f7c454f78aacfeaf0 to your computer and use it in GitHub Desktop.
Save rbuckland/99b29287c802a25f7c454f78aacfeaf0 to your computer and use it in GitHub Desktop.
template systemd service unit - /lib/systemd/system/[email protected]
[Unit]
Description=%i service powered by docker compose
Requires=docker.service
After=docker.service
[Service]
Restart=always
TimeoutStartSec=300
# Create a directory for each docker-composed service at /srv/docker/.
# Name the directory as the service.
WorkingDirectory=/srv/docker/%i
# Remove old containers, images and volumes and update it
ExecStartPre=-/usr/local/bin/docker-compose down
ExecStartPre=-/usr/local/bin/docker-compose rm -f
# Comment the following to not automatically update your images!
#ExecStartPre=/usr/bin/docker-compose pull
# Run Compose up on service start.
ExecStart=/usr/local/bin/docker-compose up
# Run Compose down, remove containers and volumes on service stop.
ExecStop=/usr/local/bin/docker-compose down
[Install]
WantedBy=multi-user.target
@rbuckland
Copy link
Author

  1. Create the above service template in /lib/systemd/system/[email protected] - dc means docker-compose
  2. At line 12, you see WorkingDirectory=/srv/docker/%i; create a directory there my-special-thing, and place your docker-compose.yaml (eg: vi /srv/docker/my-special-thing/docker-compose.yml)
  3. Enable it sudo systemctl enable [email protected]
  4. Start it sudo systemctl start [email protected] , status it sudo systemctl status [email protected]

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