Docker: Add a reload policy to a container that has already been created.

I see that Docker has added something called a policy reload to handle container restarts in the event of, for example, a reload.

Although this is very useful, I can see that the restart policy command only works with docker run , not docker start . So my question is:

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

+189
docker
Nov 10 '14 at 20:20
source share
4 answers

In recent versions of docker (starting from 1.11) you have the update command:

 docker update --restart=always <container> 
+472
May 27 '16 at 9:24
source share

There are two approaches to modify RestartPolicy:

  • Find out the container identifier, stop the entire docker service, change / var / lib / docker / containers / CONTAINER _ID / hostconfig.json, set RestartPolicy β†’ Name to "always" and start the docker service.
  • docker commit your container as a new image, stop and start the current container and start a new container with the image.
+46
Apr 24 '15 at 7:22
source share

Using --restart=always policy will handle restarting existing containers in the event of a reboot.

The problem is that if there are several containers with --restart=always when you start the image of a newer version, as discussed in Docker, how do I turn off the automatic restart of the container? ,

Attempting to automatically delete a container when it exists using the docker run --rm path selection option will also lead to problems with --restart=always because they conflict with each other .

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

Therefore, in this case, it is better to choose another option: --restart if the policy is not stopped .

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

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

This is similar to --restart=always , except that when the container stops (manually or otherwise) , it does not restart even after restarting the Docker daemon.

Thus, when using this --restart unless-stopped , to make sure that the restart works if it accidentally stops when you close the terminal, run it once in another terminal, as shown below:

 $ docker ps $ docker restart <container> 

Wait for the destruction process to complete in the previous shell, then close it and just exit ( do not exit ).
And once again check in the remaining terminal if the container is working:

 $ docker ps 

If it still works, you can safely restart the computer and check again that the application is restarting, and see that your docker is clean, without using multiple containers.

0
Mar 13 '19 at 8:08
source share

No. And, as a rule, you cannot edit the container after it is created (open port, host name, network settings) via Docker. You will need to recreate it using docker run .

As a rule, good practice is that your container is stateless, so it should not cause any problems. Take a look at volumes ( -v ) to help you with this.

-3
Nov 11 '14 at 1:45
source share



All Articles