我想在Docker中删除容器,但是当你想删除时发生了一个错误

在移除容器之前,我的下一步是查看现有容器的列表

sts@Yudi:~/docker$ sudo docker ps -as

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES                  SIZE
78479ffeba5c        ubuntu              "/bin/bash"         42 hours ago        Exited (0) 42 hours ago                       sharp_wescoff          81 B (virtual 187.7 MB)
0bd2b54678c7        training/webapp     "python app.py"     5 days ago          Exited (0) 5 days ago                         backstabbing_ritchie   0 B (virtual 323.7 MB)
0adbc74a3803        training/webapp     "python app.py"     5 days ago          Exited (143) 5 days ago                       drunk_feynman          0 B (virtual 323.7 MB)

一个我想删除列表,即“training / webapp” 但是出现了一个错误

sts@Yudi:~/docker$ sudo docker rmi training/webapp
Error response from daemon: conflict: unable to remove repository reference "training/webapp" (must force) - container 0bd2b54678c7 is using its referenced image 54bb4e8718e8
Error: failed to remove images: [training/webapp]

容器是否在映像中运行?

请帮助


可以使用-f选项强制删除容器。

Sudo docker rmi -f training/webapp

你可以在删除之前使用sudo docker stop training/webapp停止容器


首先,删除容器名称

$ sudo docker rm backstabbing_ritchie

结果

$ sudo docker rm backstabbing_ritchie
  backstabbing_ritchie

删除第二部分,该部分列在需要删除的容器上

$ sudo docker rm drunk_feynman 
  drunk_feynman

其次,移除容器

$ sudo docker rmi training/webapp

结果

$ sudo docker rmi training/webapp  
  Untagged: training/webapp:latest
  Deleted: 54bb4e8718e8600d78a5d7c62208c2f13c8caf0e4fe73d2bc0e474e93659c0b5
  Deleted: f74dd040041eb4c032d3025fe38ea85de8075992bdce6789b694a44b20feb8de
  Deleted: 7cbae69141977b99c44dc6957b032ad50c1379124d62b7d7d05ab7329b42348e
  Deleted: abb991a4ed5e4cde2d9964aec4cccbe0015ba9cc9838b696e7a32e1ddf4a49bd
  Deleted: 1952e3bf3d7e8e6a9b1e23bd4142e3c42ff7f4b7925122189704323593fd54ac
  Deleted: f95ebd363bf27a7546deced7a41a4099334e37a3d2901fa3817e62bb1ade183f
  Deleted: 20dd0c75901396d41a7b64d551ff04952084cc3947e66c67bae35759c80da338
  Deleted: 2505b734adda3720799dde5004302f5edb3f2a2ff71438f6488b530b728ba666
  Deleted: 2ee0b8f351f753f78f1178000ae37616eb5bf241d4ef041b612d58e1fd2aefdc
  Deleted: 2ce633e3e9c9bd9e8fe7ade5984d7656ec3fc3994f05a97d5490190ef95bce8d
  Deleted: 98b15185dba7f85308eb0e21196956bba653cf142b36dc08059b3468a01bf35d
  Deleted: 515565c29c940355ec886c992231c6019a6cffa17ff1d2abdfc844867c9080c5
  Deleted: 2880a3395eded9b748c94d27767e1e202f8d7cb06f1e40e18d1b1c77687aef77

检查容器

  $ sudo docker ps -as
  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES                  SIZE
  78479ffeba5c        ubuntu              "/bin/bash"         43 hours ago        Exited (0) 43 hours ago                       sharp_wescoff          81 B (virtual 187.7 MB)

如果你启动了多个docker容器,使用这个

$ docker rm $(docker ps -aq)

它将删除“ps -aq”命令中列出的所有当前docker。

来源:https://github.com/docker/docker/issues/12487上的aaam


docker镜像和docker容器是有区别的。检查这个SO问题。

简而言之,容器是映像的可运行实例。这就是为什么如果映像中有正在运行的容器,则不能删除映像的原因。您只需要首先删除容器。

docker ps -a                # Lists containers (and tells you which images they are spun from)

docker images               # Lists images 

docker rm <container_id>    # Removes a stopped container

docker rm -f <container_id> # Forces the removal of a running container (uses SIGKILL)

docker rmi <image_id>       # Removes an image 
                            # Will fail if there is a running instance of that image i.e. container

docker rmi -f <image_id>    # Forces removal of image even if it is referenced in multiple repositories, 
                            # i.e. same image id given multiple names/tags 
                            # Will still fail if there is a docker container referencing image

Docker 1.13+更新[自2017年1月起]

在Docker 1.13中,我们重新组合了每个命令,使其位于与其交互的逻辑对象之下

基本上,上面的命令也可以重写,写得更清楚:

docker container ls -a
docker image ls
docker container rm <container_id>
docker image rm <image_id>

此外,如果你想删除所有你可以使用:

docker system prune -a

警告!这将删除:

所有停止的集装箱 所有网络不被至少一个容器使用 所有未使用的图像 所有构建缓存


Noticed this is a 2-years old question, but still want to share my workaround for this particular question: Firstly, run docker container ls -a to list all the containers you have and pinpoint the want you want to delete. Secondly, delete the one with command docker container rm <CONTAINER ID> (If the container is currently running, you should stop it first, run docker container stop <CONTAINER ID> to gracefully stop the specified container, if it does not stop it for whatever the reason is, alternatively you can run docker container kill <CONTAINER ID> to force shutdown of the specified container). Thirdly, remove the container by running docker container rm <CONTAINER ID>. Lastly you can run docker image ls -a to view all the images and delete the one you want to by running docker image rm <hash>.


如果你想清理docker映像和容器

注意:这会冲掉所有东西

停止所有容器

docker stop $(docker ps -a -q)

移除所有容器

docker rm $(docker ps -a -q)

删除所有图片

docker rmi -f $(docker images -a -q)

删除docker映像>

列出所有容器

docker container ls

列出所有图片

docker image ls

根据容器id停止容器

docker container stop <container_id>

根据容器id删除容器

docker container rm <container_id>

如果不想停止和移除,可以强制移除

docker container rm -f <container_id>

删除图片

docker image rm <image_id>

完成了!


只删除与特定映像关联的容器

docker ps -a | grep training/webapp | cut -d ' ' -f 1 | xargs docker rm

Ps -a:列出所有容器 Grep training/webapp:过滤掉所有从training/webapp图像开始的容器 Cut -d ' ' -f 1:只列出容器id(第一个字段用空格分隔) Xargs docker rm:将容器id列表输出给docker rm命令移除容器


列出所有docker映像:

docker images

列出所有现存的docker容器:

docker ps -a

删除所有正在使用要删除的映像的目标容器:

docker rm <container-id>

删除目标图像:

docker rmi <image-name:image-tag or image-id>

在docker中删除“网络未找到”

检查我们无法删除的网络

docker network inspect [<id> or <name>]

断开网络

docker network disconnect -f [<networkID> or <networkName>] [<endpointName> or <endpointId>]

删除未使用的网络

docker network prune

1 .停止运行容器:

docker stop <container-id>

2-Remove容器

docker rm <container-id>

3 .删除docker图像

docker rmi <image-id>

这两个命令简单地解决了我的问题。

停止特定的容器。

docker stop <container_id>

强制删除映像,即使它在多个存储库中被引用。

docker rmi -f <container_id>