Docker: Add a restart policy to a container that w

2019-01-16 05:06发布

问题:

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?

回答1:

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

docker update --restart=always <container>


回答2:

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.


回答3:

No. And more generally, you can't edit a container once it is created (exposed port, hostname, network settings) via Docker. You would need to recreate it with docker run.

It is usually good practice to have your container stateless, so it should not cause any issue. Take a look at volumes (-v) to help you achieve this.



标签: docker