list docker restart 容器失败:“已经在使用中”,但没有更多的 docker 镜像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42760216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
docker restart container failed: "already in use", but there's no more docker image
提问by Hind Forsum
I first got my nginx docker image:
我首先得到了我的 nginx docker 镜像:
docker pull nginx
Then I started it:
然后我开始了:
docker run -d -p 80:80 --name webserver nginx
Then I stopped it:
然后我停止了它:
docker stop webserver
Then I tried to restart it:
然后我尝试重新启动它:
$docker run -d -p 80:80 --name webserver nginx
docker: Error response from daemon: Conflict. The container name "/webserver" is already in use by container 036a0bcd196c5b23431dcd9876cac62082063bf62a492145dd8a55141f4dfd74. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.
Well, it's an error. But in fact there's nothing in container list now:
嗯,这是一个错误。但实际上容器列表中现在什么都没有:
docker container list
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Why I restart nginx image failed? How to fix it?
为什么我重启nginx镜像失败?如何解决?
回答by Rao
It is because
这是因为
- you have used
--name
switch. - container is stopped and not removed
- 你用过
--name
开关。 - 容器已停止且未移除
You find it stopped
你发现它停止了
docker ps -a
You can simply start it using below command:
您可以使用以下命令简单地启动它:
docker start webserver
EDIT: AlternativesIf you want to start the container with below command each time,
编辑:替代方案如果您想每次都使用以下命令启动容器,
docker run -d -p 80:80 --name webserver nginx
then use one of the following:
然后使用以下方法之一:
method 1:use --rm
switch i.e., container gets destroyed automatically as soon as it is stopped
方法一:使用--rm
switch,即容器一停止就自动销毁
docker run -d -p 80:80 --rm --name webserver nginx
method 2:remove it explicitly after stopping the container before starting the command that you are currently using.
方法2:在启动您当前使用的命令之前停止容器后显式删除它。
docker stop <container name>
docker rm <container name>
回答by Neeraj
As the error says.
正如错误所说。
You have to remove (or rename) that container to be able to reuse that name
您必须删除(或重命名)该容器才能重用该名称
This leaves you two options.
这给您留下了两个选择。
You may delete the container that is using the name "webserver" using the command
docker rm 036a0bcd196c5b23431dcd9876cac62082063bf62a492145dd8a55141f4dfd74
您可以使用以下命令删除使用名称“webserver”的容器
码头工人 rm 036a0bcd196c5b23431dcd9876cac62082063bf62a492145dd8a55141f4dfd74
and retry.
并重试。
- Or you may use a different name during the run command. This is not recommended, as you no longer need that docker.
- 或者您可以在运行命令期间使用不同的名称。不推荐这样做,因为您不再需要那个 docker。
It's better to remove the unwanted docker and reuse the name.
最好删除不需要的泊坞窗并重用名称。
回答by arielf
While the great answers are correct, they didn't actually solve the problem I was facing.
虽然很好的答案是正确的,但它们实际上并没有解决我面临的问题。
How To:
如何:
Safely automate starting of nameddocker container regardless of its prior state
无论其先前状态如何,都可以安全地自动启动命名的docker 容器
The solution is to wrap the docker run
command with an additional check and either do a run
or a restart
based on the result.
解决方案是docker run
用额外的检查包装命令,并根据结果执行 arun
或 a restart
。
This achieves both of my goals:
这实现了我的两个目标:
- Avoids the error
- Allows me to periodically update the image (say new build) and restart safely
- 避免错误
- 允许我定期更新映像(比如新构建)并安全重启
#!/bin/bash
# Adapt the following 3 parameters to your specific case
NAME=myname
IMAGE=myimage
RUN_OPTIONS='-d -p 8080:80'
ContainerID="$(docker ps --filter name="$NAME" -q)"
if [[ ! -z "$ContainerID" ]]; then
echo "$NAME already running as container $ContainerID: stopping ..."
docker stop "$ContainerID"
fi
echo "Starting $NAME ..."
exec docker run --rm --name "$NAME" $RUN_OPTIONS "$IMAGE"
Now I can run (or stop + start if already running) the $NAME
docker container in a idempotent way, without worrying about this possible failure.
现在我可以$NAME
以幂等的方式运行(或停止 + 启动,如果已经运行)docker 容器,而不必担心这种可能的故障。