Skip to content

Instantly share code, notes, and snippets.

@hargathor
Last active January 23, 2019 18:26
Show Gist options
  • Save hargathor/57c70e91386d8102f142eadf50b9967c to your computer and use it in GitHub Desktop.
Save hargathor/57c70e91386d8102f142eadf50b9967c to your computer and use it in GitHub Desktop.
Start automatically docker-compose app (systemd)
# Thanks to https://stackoverflow.com/a/48066454/6900177
# When we use crontab or the deprecated /etc/rc.local file, we need a delay (e.g. sleep 10, depending on the machine) to make sure # that system services are available. Usually, systemd (or upstart) is used to manage which services start when the system boots. # You can try use the similar configuration for this:
```bash
# /etc/systemd/system/docker-compose-app.service
[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/srv/docker
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
````
Or, if you want run without the -d flag:
```bash
# /etc/systemd/system/docker-compose-app.service
[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
WorkingDirectory=/srv/docker
ExecStart=/usr/local/bin/docker-compose up
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
Restart=on-failure
StartLimitIntervalSec=60
StartLimitBurst=3
[Install]
WantedBy=multi-user.target
````
Change the WorkingDirectory parameter with your dockerized project path. And enable the service to start automatically:
```bash
systemctl enable docker-compose-app
````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment