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.
Using --restart unless-stopped tells Docker to always restart the command, no matter what the exit code of the command was. This way, you can have your application check its health, and if necessary use exit(1) or something similar to shutdown.
always. Always restart the container regardless of the exit status. When you specify always , the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
Use a restart policy Similar to always , except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts. The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted.
no Do not automatically restart the container. (the default) on-failure Restart the container if it exits due to an error, which manifests as a non-zero exit code. always Always restart the container if it stops.
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:
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:
docker rm <CONTAINER ID>
to tidy up). Then just run a new container from the original image specifying the -restart=always
option this time.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>
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With