Docker-Compose Restart Policy
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
Ken J
Updated on October 15, 2021Comments
-
Ken J about 1 yearI 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 over 5 yearsQuestion was about version 3, try updating the answer? docs.docker.com/compose/compose-file/#/restartpolicy -
OneCricketeer over 5 yearsI think the error in the question was because a non version 3 compose file was used -
Dan Lowe over 5 years@cricket_007 AFAICT from docs,restart_policyis only valid insidedeploy, 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 over 5 yearsVersion 3 says it is recommended for all new compose files.restartwas simply renamed, AFAICT -
Dan Lowe over 5 years@cricket_007restart_policyis a sub-option ofdeploy, anddeployin docs says "This only takes effect when deploying to a swarm withdocker stack deploy, and is ignored bydocker-compose upanddocker-compose run." -
cdaringe almost 5 yearsi don't see the:5as a documented part of the API. did i miss it? can you clarify the intent of that? -
riverhorse over 4 yearsIt 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:5and got maximum retry count cannot be used with restart policy 'unless-stopped'. I put a 0 instead of 5 and it worked -
Lokesh about 4 yearsSorry for this silly question but I didn't get what you meant by "except for deploy into swarm". Wouldrestart: on-failure:5work outside of swarm i.e. what I dodocker-compose upin version3? -
samayo about 4 yearsdoes the restart mean when you launch docker via docker-compose up?? it thought doing that always restarts the daemons -
emersonwood over 3 yearsThe maximum retry count, while undocumented, can be found in the source code here.
-
Wade Williams about 3 yearsFrom the documentation: Do not use this in production! docs.docker.com/compose/compose-file/compose-versioning/…
-
Hritik over 1 yearNow version 3 also supportsrestart: github.com/compose-spec/compose-spec/blob/master/… -
Magnetron 10 months@riverhorse it appears the gap in the documentation was unfixed. -
riverhorse 10 months@Magnetron yeah, it seems they did unfix it.