Skip to content

Instantly share code, notes, and snippets.

@tyzbit
Created July 10, 2023 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyzbit/d464d9719c521c6b99054417ffaca90f to your computer and use it in GitHub Desktop.
Save tyzbit/d464d9719c521c6b99054417ffaca90f to your computer and use it in GitHub Desktop.
Docker-Compose Systemd script
[Unit]
Description=%i service
Requires=docker.service
After=docker.service
[Service]
Restart=always
User=root
Group=docker
WorkingDirectory=/services/%i
# Shutdown container (if running) when unit is started
ExecStartPre=/usr/bin/docker-compose -f docker-compose.yml down
# Start container when unit is started
ExecStart=/usr/bin/docker-compose -f docker-compose.yml up
# Stop container when unit is stopped
ExecStop=/usr/bin/docker-compose -f docker-compose.yml down
[Install]
WantedBy=multi-user.target
@tyzbit
Copy link
Author

tyzbit commented Jul 10, 2023

Prerequisites

docker and docker-compose must be installed.

How to use

In these commands, replace appname with whatever you want to call your app/service combo.

  1. Place this file in /etc/systemd/system/
  2. mkdir -p /services/appname
  3. Create docker-compose.yml in /services/appname
  4. sudo systemctl daemon-reload
  5. sudo systemctl enable app@appname
  6. sudo systemctl start app@appname

To read logs

journalctl -u app@appname -f

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