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?
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:
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
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:
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.
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â
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.
Hey, more than a year later, is there an answer to this?
Iâm havin the exact same problem, Iâm trying to start two containers after one another importing variables via the docker compose file and I need to restart them after a reboot/power outageâŠ
Docker Compose is a kind of client for Docker. Features that Docker doesnât support are used for developement and in some cases to make sure the first started container can initialize data which will be mounted by another container as well. So when you first start the container, the order of the containers are more important. I never used the depends_on feature with healthchecks as the applications should handle when a dependency is not available. If the app fails because it canât access an endpoint and the container restarts, it can try again. Or endpoint checking can be added in a loop in the entrypoint before the application starts and run until the endpoint is ready. This works regardless of how where your containers are running (Kubernetes, Swarm, Compose).