Docker: Add a restart policy to a container that was already created

Docker

Docker Problem Overview


I see that Docker has added something called restarting policies to handle restart of containers in case of, for instance, reboot.

While this is very useful, I see that the restart policy command just work with docker run and not docker start. So my question is:

Is there any way to add restarting policies to a container that was already created in the past?

Docker Solutions


Solution 1 - Docker

In recent versions of docker (as of 1.11) you have an update command:

docker update --restart=always <container>

Solution 2 - Docker

There're two approaches to modify RestartPolicy:

  • Find out the container ID, stop the whole docker service, modify /var/lib/docker/containers/CONTAINER_ID/hostconfig.json, set RestartPolicy -> Name to "always", and start docker service.
  • docker commit your container as a new image, stop & rm the current container, and start a new container with the image.

Solution 3 - Docker

Using --restart=always policy will handle restart of existing containers in case of reboot.

The problem is that if there are multiple containers with --restart=always when you run image of a newer version as discussed in docker - how do you disable auto-restart on a container?.

> Trying to automatically remove the container when it exist by put > option docker run --rm will also problem with the --restart=always > policy since they are conflicting each others.

$ docker run --rm --restart always <image>
Conflicting options: --restart and --rm

So in this case it is better to choose another option: --restart unless-stopped policy.

$ docker run --rm --restart unless-stopped <image>

This policy will not conflicting the docker run --rm but as explained in docker documentation:

> It similar to --restart=always, except that when the container is stopped > (manually or otherwise), it is not restarted even after Docker daemon > restarts.

So when using this --restart unless-stopped policy, to ensure the restarting is working in case it stop by accident when you close the terminal, do once in another terminal as below:

$ docker ps
$ docker restart <container>

Wait until the killing process end in the previous shell, then close it and just leave (don't do exit).
And check again in the remained terminal if the container is still running:

$ docker ps

If it is still running the you can safely reboot and check again that the application is restarting and see your docker is clean without unused of multiple containers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEnrique Moreno TentView Question on Stackoverflow
Solution 1 - DockerPau Ruŀlan FerragutView Answer on Stackoverflow
Solution 2 - DockerYale HuangView Answer on Stackoverflow
Solution 3 - DockereQ19View Answer on Stackoverflow