How to handle server reboot when using docker-compose?

When using docker run for a single container, I understand that I can assign a restart policy to make sure the container is restarted by docker when the server reboots.

What is the equivalent practice when using docker-compose for all services defined by the docker-compose.yml?

If I stand up several services as containers using docker-compose, what is controlling the restart/reboot condition for everything as a whole and individually?

1 Like

Have been wondering about this myself. Turns out after a few false starts Google is our friend, and that it’s simply part of the compose file syntax:

https://docs.docker.com/v1.7/compose/yml/#working-dir-entrypoint-user-hostname-domainname-mem-limit-privileged-restart-stdin-open-tty-cpu-shares-cpuset-read-only

Further on setting up a production environment:

  1. That does answer on a per service/container, sort of 
 that part of the documentation doesn’t give a clear example of where to use those parameters. It just lists the parameters as if they are floating around in the file “somewhere”.

and more importantly

  1. That doesn’t explain how to have the whole definition in the compose restart itself in the same way as when docker-compose up was run originally. There are things that defined only in the docker-compose.yml file. How are those handled?

For one example:

What is controlling the depends_on references to make sure the services/containers that are depended on start first and then the ones that depend on start after but not before - as is defined in the docker-compose.yml but is not defined for the container on its own.

On 1) I would expect the restart: to be nested under the service/container you want to set it for. So the example given in the depends_on section of the compose reference could be:

version: '2'
services:
  web:
    build: .
    restart: always         # <--- my addition 
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

Does that not work for you if you test out a similar addition in your environment?

Regarding 2) I would simply expect declarations as depends_on: to work just as it did before you add restart: So in the example given above a natural consequence after a restart would be that the dependent services db and redis will be started before web. Something similar should implicitly be true for volumes and networks your services refer to.

Sorry to necro this thread, but I needed an answer to this very question.

After trying this, I found I had to add restart: always to the redis: section as well to make sure it came up. With just depends_on: redis and restart: always in the web: section, the apache container would restart after a reboot, but redis wouldn’t.

1 Like

No worries. I was wondering where to put that as wall. If I understand you correctly, you have to put “restart: always” in each service that you want to start on system boot?

I wasn’t sure if you could make a single “global” entry in the file that would applied to all of the services or not.

For example, the top of my file looks like this before I add restart: always to it


version: “3.6”
services:
web:

web parameters
database:

database parameters

Then with the restart: always parameter


version: “3.6”
restart: always ← here at top level?
services:
restart: always ← or here at services level ?
web:

web parameters
restart: always ← or perhaps in each service, here?

database:

database parameters
restart: always ← or perhaps in each service, here?

With docker-compose, the “restart:” node is a direct child node of a specific service, a sibling of “image:”
For Swarm stack deploymets, the “restart_policy:” node is a child node of “deploy:”, which itself is a sibling of “image:”

The problem with the restart policy at the service level is that docker itself is unaware of the depends_on between services that only appears in the docker-compose.yml file. docker-compose up will start services in dependency order and can wait on services to be healthy before starting their dependent services. But on reboot, every service that is marked for restart will be immediately started without waiting for their dependency services to be healthy first.
Is there a way to address that? It seems incomplete to have depends_on functionality if it only works in an explicit up call but not on reboot.

3 Likes

This is exactly what I am looking for. Is there any solution for this yet?