How to start docker-compose up -d at system startup?

To autorun containers at the time of launch, I tried to add the command:

cd directory_has_docker-compose.yml && docker-compose up -d in /etc/rc.local.

but after rebooting the machine, the containers do not work.

How to start docker-compose up -d at system startup?

+71
docker docker-compose
source share
6 answers

When we use crontab or the deprecated /etc/rc.local , we need a delay (e.g. sleep 10 , depending on the machine) to ensure the availability of system services. Typically, systemd (or upstart ) is used to control which services start when the system boots. You can try using a similar configuration for this:

 # /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 to run without -d :

 # /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 to your docker project path. And enable the service to start automatically:

 systemctl enable docker-compose-app 
+88
source share

You can add:

 restart: always 

for each service you want to restart in the docker-compose.yml file

+59
source share

I tried restart: always , it works in some containers (e.g. php-fpm), but I ran into the problem that some containers (e.g. nginx) still do not restart after rebooting.

Solved a problem.

 crontab -e @reboot (sleep 30s ; cd directory_has_dockercomposeyml ; /usr/local/bin/docker-compose up -d )& 
+31
source share

If your docker.service turned on at system startup

 $ sudo systemctl enable docker 

and your services in your docker-compose.yml

 restart: always 

all services start when your system reboots if you run the command below only once

 docker-compose up -d 
+26
source share

Use restart: always in your docker creation file.

Docker-compose up -d will start the container from images again. Use docker-compose start to start stopped containers, it never starts new containers from images.

 nginx: restart: always image: nginx ports: - "80:80" - "443:443" links: - other_container:other_container 

You can also write code in a Docker file so that it is created first if it depends on other containers.

+13
source share

As a complement to user39544 answer another type of syntax for crontab -e :

 @reboot sleep 60 && /usr/local/bin/docker-compose -f /path_to_your_project/docker-compose.yml up -d 
+4
source share

All Articles