55

When you initially run a Docker container from an image you can specify the option:

--restart="always"

This ensures that the container is always restarted by the Docker daemon if for some reason it stops. So you could run a container like so:

docker run --restart="always" <IMAGE>

Also you can restart an existing Docker container by specifying its container ID, i.e.:

docker start <CONTAINER ID>

However I can't determine if it's possible to change an existing container, that originally was not run with the --restart="always" option, to convert it to always restart in future.

Currently the only way I can think to do this is to save the container as a new image and then run that image as a new container with the --restart="always" option. Would this in fact be the correct way to do this?

EDIT: What I perhaps didn't make clear enough originally is that I am thinking about the situation where there have been changes in the container since it was originally run, which need to be persisted. So just running a new container from the original image would not be sufficient.

Richard Corfield
  • 2,489
  • 3
  • 21
  • 24
  • 1
    More or less duplicates: https://stackoverflow.com/questions/26852321/docker-add-a-restart-policy-to-a-container-that-was-already-created – Tomasz Gandor Jan 10 '19 at 07:58

4 Answers4

109

We now have docker update, which allows changing the restart policy of a running container.

docker update --restart=always <CONTAINER ID>

There are three other options:

  • no (default)
  • on-failure
  • unless-stopped

Please refer to the link for details.

Frank Wong
  • 1,718
  • 2
  • 13
  • 12
15

Ok, so to answer my own question, it seems that it's not possible just to restart the same container with --restart=always, because that's something you have to do when you run a container for the first time and not a parameter that you can use when you start an existing container.

There are three possible work-arounds to this:

  1. As @user2915097 stated, you can abandon the original container (stopping it and then deleting it with docker rm <CONTAINER ID>to tidy up). Then just run a new container from the original image specifying the -restart=always option this time.
  2. If no volumes were used, so the changes are internal to the container, you need to commit the container to a new image and then run a new container from that image.

    docker commit <CONTAINER ID> <NEW IMAGE NAME>

    docker run -d --restart=always ... <NEW IMAGE NAME>

  3. If volumes were used and all changes are restricted to the volumes, then you can run a second container with the --volumes-from parameter without having to commit a new version of the image. i.e.

    • docker stop <CONTAINER 1 NAME>
    • docker run -d --restart=always --volumes-from <CONTAINER 1 NAME> ... <ORIGINAL IMAGE NAME>

    It would then be safe to delete Container 1, as the volumes will not be deleted whilst another container continues to use them.

I guess there is a fourth possibility too; if you used a volume(s) and you know that there have been changes to the container that aren't on the volume, then you'll have to use a combination of (2) and (3).

Richard Corfield
  • 2,489
  • 3
  • 21
  • 24
3

Update: This worked to enable restart. But setting it back to no and it gets reset back to always and the container starts again! :( I'm going to leave this answer here in case someone figures out how this really works. I must be close!

Folks, I've found the most hacky solution that gets around copying containers etc.

vi /var/lib/docker/containers/$(docker inspect -f '{{ .Id }}' $ContainerID)/hostconfig.json

Search for "RestartPolicy". Set it to "no", "always" etc

Maybe someone could wrap that up in a script!?

Anyway, that piece of json along with the config.json would allow you to modify all sorts of things that you missed when creating your container.

hookenz
  • 36,432
  • 45
  • 177
  • 286
  • A word of warning to anyone who tries this, A lot of Docker's state management operates with the assumption that it's the only thing modifying the contents of /var/lib/docker. Be very careful when making changes here. – Kevan Ahlquist Sep 17 '15 at 00:09
  • 1
    Ah yeah, depends what changes are made and what files are kept open. To be on the safe side feel free to stop your docker engine. I hope they add the ability to make changes in subsequent releases. It's crazy not having the ability manipulate the restart policy. – hookenz Sep 17 '15 at 00:55
  • 1
    A new set feature may be added soon. https://github.com/docker/docker/pull/15078 – hookenz Sep 17 '15 at 02:26
0

extract from http://www.brandpending.com/blog/2014/11/21/setting-and-re-setting-the-restart-behaviour-of-a-docker-container

So let say you want to change the restart policy of this container from always to on-failure. To do this, you need to stop the container, remove it and re-run it with the new restart policy.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user2915097
  • 30,758
  • 6
  • 57
  • 59
  • But the problem with this is you would lose any changes in the original container because you've deleted it and created a new one from scratch. I'm pretty sure that committing a new image and running a new container from that would be better, if there have been any changes in the original container that need to be persisted. – Richard Corfield Apr 13 '15 at 11:53
  • So we are back to your commit – user2915097 Apr 13 '15 at 11:57
  • @rdc you should try to avoid having changes in containers because of things like this - either keep it in a volume or a DB of some sort. – Adrian Mouat Apr 13 '15 at 13:22
  • This answer in no longer relevant, check out @Frank Wong's answer – bluesummers Nov 08 '17 at 08:10