systemd and docker-compose

Overview::

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.

Howto::

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

Contents of the file should be as below.

[Unit]
Description=docker-compose
Requires=docker.service
After=docker.service

[Service]
Restart=always

# 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

[Install]
WantedBy=multi-user.target

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  ::
https://github.com/docker/compose/issues/4266

Leave a Reply

Your email address will not be published. Required fields are marked *