How to restart an existing Docker container in restart="always" mode?

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.


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).

Tags:

Docker