user39544
user39544

Reputation: 3767

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

To let the containers autostart at startup point, I tried to add the command:

cd directory_has_docker-compose.yml && docker-compose up -d

in /etc/rc.local

but then after I rebooted the machine, the containers did not work.

How can I run docker-compose up -d at system start up?

Upvotes: 315

Views: 376577

Answers (10)

Arunas Bart
Arunas Bart

Reputation: 2678

There are two parts which involved to (re)start compose services at startup.

  1. Docker it self. Check if docker service is enabled and started:
systemctl enable docker
systemctl start docker
systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; preset: enabled)
     Active: active (running) since Tue 202y-mm-dd 22:40:26 GMT; 48min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 147 (dockerd)
      Tasks: 38
     Memory: 79.8M
        CPU: 4.155s
     CGroup: /system.slice/docker.service
             ├─147 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  1. docker-compose.yml service manifest.

Depending on your requirements, and situation, you should add to service declaration, e.g.:

services:
  db:
    image: mariadb:11.4
    restart: unless-stopped
  redis:
    image: altall/garnet:latest
    restart: unless-stopped
  web:
    build: .
    restart: unless-stopped
    depends_on:
      - db
      - redis

in this case services will be automatically started on reboot, or failure, but in case of maintenance, state will be preserved over reboots. For example temporary stopping with

docker compose stop

doing node maintenance (including reboots) and later resume with

docker compose start

Or another option, you can define

    restart: always

in this case, docker will try to automatically resume/restart service on docker service start or container failed, no matter was it stopped manually before or not.

More info. Docker docs say:

restart Flag Description

  • no Don't automatically restart the container. (Default)

  • on-failure[:max-retries] Restart the container if it exits due to an error, which manifests as a non-zero exit code. Optionally, limit the number of times the Docker daemon attempts to restart the container using the :max-retries option. The on-failure policy only prompts a restart if the container exits with a failure. It doesn't restart the container if the daemon restarts.

  • always Always restart the container if it stops. If it's manually stopped, it's restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in restart policy details)

  • unless-stopped Similar to always, except that when the container is stopped (manually or otherwise), it isn't restarted even after Docker daemon restarts.

Upvotes: 3

vatandoost
vatandoost

Reputation: 3737

If your docker.service enabled on system startup

$ sudo systemctl enable docker

and your services in your docker-compose.yml has

restart: always

all of the services run when you reboot your system if you run below command only once

docker compose up -d

Upvotes: 341

Colt
Colt

Reputation: 1

Worked for me(slight variant):

crontab -e then pick nano editor, if not yet selected. at the bottom of file add as one line from @reboot to &:

@reboot (sleep 10s ; cd /usr/local/searxng-docker ; /usr/bin/docker-compose up -d )&

Based upon a common location of "docker-compose" and the
"searxng-docker" folder. It forces the 2 commands "cd to
the actual searxng-docker folder" and run the container startup "docker-compose up -d" command.

Upvotes: 0

Abdourahmane FALL
Abdourahmane FALL

Reputation: 1917

To use restart policies, Docker provides the following options:

no: Containers won't restart automatically.

on-failure[:max-retries]: Restart the container if it exits with a non-zero exit code, and provide a maximum number of attempts for the Docker daemon to restart the container.

always: Always restart the container if it stops.

unless-stopped: Always restart the container unless it was stopped arbitrarily, or by the Docker daemon.

Upvotes: 1

Vaseem007
Vaseem007

Reputation: 2531

Use restart: always in your docker-compose.yaml file.

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

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

Also you can write the code up in the docker file so that it gets created first, if it has the dependency of other containers.

Upvotes: 51

Oleg Belostotsky
Oleg Belostotsky

Reputation: 7408

When we use crontab or the deprecated /etc/rc.local file, we need a delay (e.g. sleep 10, depending on the machine) to make sure that system services are available. Usually, systemd (or upstart) is used to manage which services start when the system boots. You can try use the 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 run without the -d flag:

# /etc/systemd/system/docker-compose-app.service

[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
StartLimitIntervalSec=60

[Service]
WorkingDirectory=/srv/docker
ExecStart=/usr/local/bin/docker-compose up
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
Restart=on-failure
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

Change the WorkingDirectory parameter with your dockerized project path. And enable the service to start automatically:

systemctl enable docker-compose-app

Upvotes: 311

Gerson Diniz
Gerson Diniz

Reputation: 1279

restart: unless-stopped

docker-compose.yml

Upvotes: 14

MaxiReglisse
MaxiReglisse

Reputation: 3336

You should be able to add:

restart: always 

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

See: https://github.com/compose-spec/compose-spec/blob/master/spec.md#restart

Upvotes: 257

TitanFighter
TitanFighter

Reputation: 5074

As an addition to user39544's answer, one more type of syntax for crontab -e:

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

Upvotes: 9

user39544
user39544

Reputation: 3767

I tried restart: always, it works at some containers(like php-fpm), but i faced the problem that some containers(like nginx) is still not restarting after reboot.

Solved the problem.

crontab -e

@reboot (sleep 30s ; cd directory_has_dockercomposeyml ; /usr/local/bin/docker-compose up -d )&

Upvotes: 47

Related Questions