Docker: Add a restart policy to a container that w

2019-01-16 04:32发布

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?

标签: docker
3条回答
ゆ 、 Hurt°
2楼-- · 2019-01-16 05:07

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楼-- · 2019-01-16 05:24

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.

查看更多
来,给爷笑一个
4楼-- · 2019-01-16 05:26

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

docker update --restart=always <container>
查看更多
登录 后发表回答