如何在restart =“始终”中重新启动现有的Docker容器模式? [英] How to restart an existing Docker container in restart="always" mode?
问题描述
当您最初从图像中运行Docker容器时,您可以指定选项:
When you initially run a Docker container from an image you can specify the option:
--restart="always"
这样可以确保由于某些原因停止运行,Docker守护程序始终重新启动该容器。所以你可以这样运行一个容器:
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>
此外,您还可以通过指定其容器ID来重新启动现有的Docker容器,即:
Also you can restart an existing Docker container by specifying its container ID, i.e.:
docker start <CONTAINER ID>
但是,我无法确定是否可以更改原来未运行的现有容器 - restart =always
选项,将其转换为将来始终重新启动。
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.
目前唯一的方法我可以认为这样做是将容器保存为新的映像,然后将该映像作为新的容器运行 - restart =always
选项。实际上是正确的做法吗?
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.
推荐答案
好的,所以要回答我自己的问题,似乎只能用重新启动同一个容器 - restart = always
,因为这是您首次运行容器时所必须执行的操作,而不是您启动现有容器时可以使用的参数。
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:
- 正如@ user2915097所说,你可以放弃原始容器(停止它,然后用
docker rm< CONTAINER ID>
删除它)以整理。然后从原始图像运行一个新的容器,这次指定了-restart = always
选项。 -
如果没有卷被使用,所以更改是容器的内部,您需要将容器提交到一个新的映像,然后从该映像运行一个新的容器。
- 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. 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>
如果使用卷,所有更改都限于卷,则可以运行第二个容器, code> - 卷 - 来自参数,而不必提交新版本的图像。即
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.
-
停靠站停止&CONTINERER 1 NAME>
-
docker run -d --restart = always --volumes-from< CONTAINER 1 NAME> ...< ORIGINAL IMAGE NAME>
docker stop <CONTAINER 1 NAME>
docker run -d --restart=always --volumes-from <CONTAINER 1 NAME> ... <ORIGINAL IMAGE NAME>
然后删除容器1将是安全的,如这个卷不会被删除,而另一个容器继续使用它们。
It would then be safe to delete Container 1, as the volumes will not be deleted whilst another container continues to use them.
我想还有第四种可能性;如果您使用卷(和)您知道容器中没有卷的更改,则必须使用(2)和(3)的组合)。
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).
这篇关于如何在restart =“始终”中重新启动现有的Docker容器模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!