271

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?

1
  • 8
    use --restart always or --restart unless-stopped or in docker-compose.yml use restart: always --> Ref. But maybe not worked on some containers! Jul 18, 2018 at 13:06

9 Answers 9

291

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
15
  • 2
    Is there a simple way to test if it works without restarting raspberry?
    – dmigo
    Feb 25, 2018 at 23:13
  • 6
    @dmigo systemctl start docker-compose-app and systemctl status docker-compose-app are what you're looking for I think.
    – HectorJ
    Jun 28, 2018 at 14:57
  • 1
    @dmigo: test startup of your service with: service docker-compose-app start, status with service docker-compose-app status, stop with service docker-compose-app stop
    – BarryPye
    Apr 10, 2020 at 14:41
  • 2
    Best answer IMHO. @OlegBelostotsky could you provide explanation about why the non-detached version adds further directives?Restart=on-failure; StartLimitIntervalSec=60; StartLimitBurst=3 Sep 3, 2021 at 9:36
  • 1
    Remark: with the change of docker compose form the soon deprecated standalone version (docker-compose) to the now preferred and long-term supported plugin version (docker compose) the syntax of the binary in ExecStart and ExecStop changes. Info about docker compose versions: docs.docker.com/compose/install/linux Apr 3 at 18:12
265

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
4
  • 4
    Why "restart: always" and not "restart: unless-stopped"? Both should do the work I would say
    – rastov
    Sep 1, 2020 at 13:32
  • 1
    @rastov both of them are similar but in unless-stopped when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.
    – vatandoost
    Sep 2, 2020 at 4:51
  • 1
    "if you run below command only once" - but, the point of a process manager like systemd or init.d is to remove the human in the loop. To get the human out, one needs @oleg-belostotsky's tactic.
    – Lonnie
    Jan 7, 2021 at 22:01
  • 2
    @Lonnie the run once is equivalent to adding the crontab. basically start the compose once, and restart: always will take care of the rest.
    – Adarsha
    Aug 13 at 3:42
246

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

13
  • 22
    Keep in mind that they have to be running when a reboot occurs, so don't manually stop them before the reboot.
    – Tom
    Jul 22, 2017 at 17:05
  • 1
    some services such as Nginx not starts with this option even. Jul 18, 2018 at 13:41
  • 37
    This is the proper answer to the question. There is a designed way to restart containers, why get into cron jobs and other ways to reinvent the wheel. Aug 9, 2018 at 20:03
  • 32
    @TahaRehmanSiddiqui Note that restart: always has some serious bugs: host mounts will not be attached on reboot for example. In my opinion, it's better to reinvent the wheel, if the existing wheel is square.
    – okdewit
    Jun 8, 2019 at 18:39
  • 1
    Why "restart: always" and not "restart: unless-stopped"? Both should do the work I would say
    – rastov
    Sep 1, 2020 at 13:32
49

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.

3
  • 9
    You may not want to use always, but maybe unless-stopped. Other options are on-failure and no. This is known as a restart policy.
    – Paul
    Nov 6, 2019 at 15:39
  • Just adding that line should work? Is there a tutorial on how to enable this whole solution? Thanks! Dec 21, 2020 at 11:38
  • After adding the line restart: always, run docker-compose down and then docker-compose up -d again to make it effective. docker-compose start/restart won't reflect the change in yaml.
    – Terry Lam
    Dec 23, 2022 at 6:12
45

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 )&
4
  • 13
    You should be suspicuous of bare sleeps as they introduce non-deterministic behavior: martinfowler.com/articles/… Oct 18, 2017 at 15:30
  • @giorgiosironi sleep is fine in this case. Container start-up must be able to handle nondeterministic behaviour anyway.
    – z0r
    Dec 7, 2017 at 2:37
  • 8
    It's also introducing up to 30 seconds of latency that may not be needed. Dec 7, 2017 at 15:14
  • 5
    @z0r sleep is not fine! Sleep may "work" but any startup sequence should be deterministic. Linux services use dependencies to ensure things like the network are available etc. before they start. You should do the same.
    – colm.anseo
    Apr 8, 2020 at 11:04
13
restart: unless-stopped

docker-compose.yml

3
  • Please add more details to your answer, otherwise it will not be considered to be a good answer.
    – Ouss
    Feb 21, 2022 at 12:50
  • you can deduce from the most popular answer that this is saying it's a refinement on that answer, to allow for users to manually stop
    – aehlke
    Feb 22, 2022 at 23:54
  • 4
    One can always manually stop a container. The difference between always and unless-stopped is that containers won't start on bootup with unless-stopped if the container was manually stopped. If you manually stop a restart: always container it will remain stopped until the next boot. (or rather, the next time the docker daemon restarts). One can't really deduce this information from the most popular answer. 1. the most popular answer is currently a systemd service file answer. 2. the next most popular answer doesn't provide these details. One has to read the manpage for docker-run
    – bobpaul
    Sep 21, 2022 at 16:09
9

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
1
  • This worked for me in Mar 2018 on a RPi3 running Raspian. I ran crontab -e as user pi, with pi a member of group docker... Mar 1, 2018 at 22:46
1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.