Docker Community Forums

Share and learn in the Docker community.

How to handle server reboot when using docker-compose?

(Patakijv) #1

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?

(Jornh) #2

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:

Further on setting up a production environment:

(Patakijv) #3
  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.

(Jornh) #4

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'
    build: .
    restart: always         # <--- my addition 
      - db
      - redis
    image: redis
    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.