Skip to content

Instantly share code, notes, and snippets.

@noyb34
Last active August 29, 2017 16:26
Show Gist options
  • Save noyb34/389e8fb1172d6c07b2e1f634737e42e2 to your computer and use it in GitHub Desktop.
Save noyb34/389e8fb1172d6c07b2e1f634737e42e2 to your computer and use it in GitHub Desktop.
config unit
########################################################################
Create file /etc/systemd/system/myservice.service and include below code
########################################################################
[Unit]
Description=myservice
Requires=docker.service
After=docker.service
[Service]
Restart=always
User=myuser # Optional
WorkingDirectory=/path/working/dir
# Remove old containers, images and volumes
ExecStartPre=/usr/bin/docker-compose -f my.yml down -v
ExecStartPre=/usr/bin/docker-compose -f my.yml rm -v
ExecStartPre=-/bin/bash -c 'docker volume rm $(docker volume ls -q)'
ExecStartPre=-/bin/bash -c 'docker rmi $(docker images | grep "<none>" | awk \'{print $3}\')'
ExecStartPre=-/bin/bash -c 'docker rm -v $(docker ps -aq)'
# Compose up
ExecStart=/usr/bin/docker-compose -f my.yml up
# Compose down, remove containers and volumes
ExecStop=/usr/bin/docker-compose -f my.yml down -v
[Install]
WantedBy=multi-user.target
####################################################
systemctl start myservice
#####################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment