286

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?

Enrique Moreno Tent
  • 24,127
  • 34
  • 104
  • 189
  • You should change the accepted answer to @Yale Huang's one. The accepted answer is incorrect with current versions of docker. I understand the answer might have helped you back then but now it's misleading. – Stepan Vavra Jun 13 '16 at 08:24

3 Answers3

728

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

docker update --restart=always <container>
Pau Ruŀlan Ferragut
  • 7,536
  • 2
  • 12
  • 5
  • 11
    It looks like this was added in Docker 1.11.0. – phansen Jun 01 '16 at 14:29
  • @phansen: [Indeed it was](https://github.com/docker/docker/pull/19116). – TachyonVortex Sep 21 '16 at 10:38
  • 4
    Should be the accepted answer with the caveat that it works as of Docker 1.11. – JohnDoe Jan 10 '17 at 06:59
  • 7
    with docker 17.09 I had to run `docker update --restart always ` https://docs.docker.com/engine/admin/start-containers-automatically/ – HarlemSquirrel Nov 28 '17 at 13:36
  • `sudo docker ps | cut -f1 -d' ' | tail -n +2 | xargs sudo docker update --restart=always` -- this will update all running containers. You might want to compare the output to docker ps to make sure the tail part is still skipping the appropriate amount of lines and not excluding the first container. – scaryman Jun 11 '18 at 13:42
  • `docker ps -q` gives you just the IDs (each per line), so just use ithat in front of `| xargs docker update --restart=always` – Azder Jun 26 '18 at 14:22
  • 6
    `docker update` accepts multiple container IDs, so no need for `xargs` at all, just do `docker update --restart=always $(docker ps -q)`. – markusk Jan 06 '19 at 21:47
  • Use: `docker update --restart=no ` to prevent from restarting again. – Melroy van den Berg Jul 28 '19 at 02:27
  • This is helpful. I have been recreating my containers this whole time (`docker-compose down; docker-compose up -d`)!!!! – leeman24 Nov 22 '19 at 15:28
  • It should be noted that if you're using docker-compose, you must still put `restart: always` in your docker-compose.yml file in order for the restart policy to persist. `docker update` only applies to a particular running container, and `docker-compose up` works by deleting the old container (including the restart policy you set with `docker update`) and creating a fresh one with the restart policy in `docker-compose.yml`. – Mac Chaffee Apr 27 '23 at 14:45
50

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.
ks1322
  • 33,961
  • 14
  • 109
  • 164
Yale Huang
  • 653
  • 5
  • 4
  • 2
    stop container, edit, start container. works like a charm. I don't know why editing is disabled. – mist Feb 28 '16 at 10:03
  • Worked for me on an old server with docker 1.10 (don’t ask ). Sadly `docker update` allows to modify restart policy only on 1.11+ – Igor Kupczyński Nov 25 '19 at 23:00
2

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.

test30
  • 3,496
  • 34
  • 26
eQ19
  • 9,880
  • 3
  • 65
  • 77