systemd and docker-compose


docker compose

A short how to on integrating docker-compose with systemd. Advantage of this is ,you can have your containers setup with docker-compose to start up on boot or get managed with systemd itself.


Create a new file docker-compose.service at /usr/lib/systemd/system/

Contents of the file should be as below.



# Remove old containers, images and volumes
ExecStartPre=/usr/bin/docker-compose -f your_docker_compose_file.yml down -v
ExecStartPre=/usr/bin/docker-compose -f your_docker_compose_file.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 your_docker_compose_file.yml up

# Compose down, remove containers and volumes
ExecStop=/usr/bin/docker-compose -f your_docker_compose_file.yml down -v


You can now manage your containers with the below commands

systemctl start docker-compose
systemctl stop docker-compose

Make sure the docker containers are up and available with

docker-compose ps

To start your containers setup with docker compose to start on system boot, enable it on systemd

systemctl enable docker-compose

Reference  ::