Reputation: 5416
I would like to automatically restart a container (not run an image) when it stops running.
All the examples I see mention docker run
but I just want to restart a previously created container.
I tried adding the following to the service definition in the yml file, but it didn't do anything:
restart: always
Upvotes: 5
Views: 41407
Reputation: 20196
I suppose restart: always
didn't do anything because you need to run docker-compose up
so that it will make a new container with this policy in place. With restart: always
container will be restarted in any case when it is not running. That is not only after failure but after reboot as well.
Using restart policy is the recommended way to solve this problem but it requires you to recreate the container once to apply the policy. If you can't tolerate even one recreation (there are ways to keep the data: see docker cp
, docker commit
), you may get along with a cron script:
* * * * * docker ps | grep -q container_name || docker start container_name
Once in a minute this will check if the container is up and restart it if it is not.
Snippet for powershell:
if ( docker ps | Select-String -quiet container_name ) {} else { docker start container_name }
Upvotes: 13
Reputation: 220
can you try this docker_restart_policy

version: "3.9"
services:
redis:
image: redis:alpine
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
Upvotes: 5