[Unit] Description=%i service with docker compose managed by Ansible Requires=docker.service After=docker.service [Service] Restart=always WorkingDirectory=/opt/%i # Remove old containers, images and volumes ExecStartPre=/usr/bin/docker-compose down -v ExecStartPre=/usr/bin/docker-compose rm -fv ExecStartPre=-/bin/bash -c 'docker volume ls -qf "name=%i_" | xargs docker volume rm' ExecStartPre=-/bin/bash -c 'docker network ls -qf "name=%i_" | xargs docker network rm' ExecStartPre=-/bin/bash -c 'docker ps -aqf "name=%i_*" | xargs docker rm' # Compose up ExecStart=/usr/bin/docker-compose up # Compose down, remove containers and volumes ExecStop=/usr/bin/docker-compose down -v [Install] WantedBy=multi-user.target