Docker-Compose Restart Policy

126,189

Solution 1

Version 2 supports restart policies, using the restart keyword, and should work fine for you if you don't need Swarm (which you said you don't need/want).

version: '2'
services:
  web:
    image: apache
    restart: always

https://docs.docker.com/compose/compose-file/compose-file-v2/#restart

Compose format version 3 has a parameter called restart_policy, but so far as I can tell from documentation it is only valid as part of deploy, which is only used when deploying to a Swarm. So version 3 is probably not useful in your case.

Solution 2

It looks like a gap in the documentation

In 3rd version, we can still use "restart" inside services same as before in v.2 (except for deploy into swarm)

version: '3'
services:
  my-service:
    restart: on-failure:5 

https://docs.docker.com/compose/compose-file/compose-file-v3/#restart_policy

Solution 3

Even if you're NOT in swarm mode, there is an option called --compatibility which will work with restart_policy, this will attempt to restart even if you're not deploying. The only glitch, is the sub-keys of 'delay' and 'window' will be ignored. Here is an example:

version: '3.7'
services:
  build:
    context: .
    dockerfile: Dockerfile
  container_name: example
  deploy:
    restart_policy:
      condition: on-failure
      max-attempts: 3

run this command:

docker-compose -f docker-compose.yml --compatability up
Share:
126,189
Ken J
Author by

Ken J

Updated on October 15, 2021

Comments

  • Ken J
    Ken J about 1 year

    I looked thru the docs for docker-compose and I see that Version 3 has a deploy restart policy but it's only for swarm. I tried setting restart_policy on my service but got this error:

    ERROR: The Compose file './docker-compose.yml' is invalid because:
    Unsupported config option for services.web: 'restart_policy'
    

    Is there any way to set a restart policy on services created using docker-compose outside of a swarm?

  • OneCricketeer
    OneCricketeer over 5 years
    Question was about version 3, try updating the answer? docs.docker.com/compose/compose-file/#/restartpolicy
  • OneCricketeer
    OneCricketeer over 5 years
    I think the error in the question was because a non version 3 compose file was used
  • Dan Lowe
    Dan Lowe over 5 years
    @cricket_007 AFAICT from docs, restart_policy is only valid inside deploy, which is only even used when doing Swarm. Since OP asked about not using Swarm, I feel my original answer should just stay as-is.
  • OneCricketeer
    OneCricketeer over 5 years
    Version 3 says it is recommended for all new compose files. restart was simply renamed, AFAICT
  • Dan Lowe
    Dan Lowe over 5 years
    @cricket_007 restart_policy is a sub-option of deploy, and deploy in docs says "This only takes effect when deploying to a swarm with docker stack deploy, and is ignored by docker-compose up and docker-compose run."
  • cdaringe
    cdaringe almost 5 years
    i don't see the :5 as a documented part of the API. did i miss it? can you clarify the intent of that?
  • riverhorse
    riverhorse over 4 years
    It seems they fixed that gap in the documentation. The :5 is still not documented, and apparently is the "maximum retry count". I used it like that: restart: unless-stopped:5 and got maximum retry count cannot be used with restart policy 'unless-stopped'. I put a 0 instead of 5 and it worked
  • Lokesh
    Lokesh about 4 years
    Sorry for this silly question but I didn't get what you meant by "except for deploy into swarm". Would restart: on-failure:5 work outside of swarm i.e. what I do docker-compose up in version 3?
  • samayo
    samayo about 4 years
    does the restart mean when you launch docker via docker-compose up?? it thought doing that always restarts the daemons
  • emersonwood over 3 years
    The maximum retry count, while undocumented, can be found in the source code here.
  • Wade Williams about 3 years
    From the documentation: Do not use this in production! docs.docker.com/compose/compose-file/compose-versioning/…
  • Hritik
    Hritik over 1 year
    Now version 3 also supports restart: github.com/compose-spec/compose-spec/blob/master/…
  • Magnetron
    Magnetron 10 months
    @riverhorse it appears the gap in the documentation was unfixed.
  • riverhorse
    riverhorse 10 months
    @Magnetron yeah, it seems they did unfix it.