docker强制删除镜像

前言

在搭建k8s集群的时候,由于master主机需要一系列的组件,例如kube-apiserver, kube-proxy, kube-controller-manager, etcd等。

这些都是以docker容器形式运行的,所以需要提前拉取。

但是,由于某些原因吧,k8s没有拉起来。在检查的时候发现docker镜像出了问题。于是我就去删除它们,但是删除的时候也出了错误:

[root@node1 ~]# docker images
REPOSITORY                                                        TAG                 IMAGE ID            CREATED             SIZE
k8s.gcr.io/kube-apiserver                                         v1.22.1             f30469a2491a        9 days ago          128 MB
registry.aliyuncs.com/google_containers/kube-apiserver            v1.22.1             f30469a2491a        9 days ago          128 MB
k8s.gcr.io/kube-proxy                                             v1.22.1             36c4ebbc9d97        9 days ago          104 MB
k8s.gcr.io/kube-controller-manager                                v1.22.1             6e002eb89a88        9 days ago          122 MB
registry.aliyuncs.com/google_containers/kube-controller-manager   v1.22.1             6e002eb89a88        9 days ago          122 MB
k8s.gcr.io/kube-scheduler                                         v1.22.1             aca5ededae9c        9 days ago          52.7 MB
registry.aliyuncs.com/google_containers/kube-scheduler            v1.22.1             aca5ededae9c        9 days ago          52.7 MB
k8s.gcr.io/etcd                                                   3.5.0-0             004811815584        2 months ago        295 MB
registry.aliyuncs.com/google_containers/etcd                      3.5.0-0             004811815584        2 months ago        295 MB
k8s.gcr.io/coredns/coredns                                        v1.8.4              8d147537fb7d        3 months ago        47.6 MB
k8s.gcr.io/pause                                                  3.5                 ed210e3e4a5b        5 months ago        683 kB
registry.aliyuncs.com/google_containers/pause                     3.5                 ed210e3e4a5b        5 months ago        683 kB
[root@node1 ~]# docker rmi $(docker images -a -q)
Untagged: k8s.gcr.io/kube-proxy:v1.22.1
Untagged: k8s.gcr.io/kube-proxy@sha256:efcf1d5fb2fc95d28841f534f1385a4884230c7c876fb1b7cf66d2777ad6dc56
Deleted: sha256:36c4ebbc9d979f15a0316c6dde446c556250d397e2085375cfbaf2660272d912
Deleted: sha256:bc584c3df17637d0ea10b53014d68dc283c7d3313a9e6f067282fcf78273fc66
Deleted: sha256:48b90c7688a2c85d7081a437ecba5cb706fbaa98b09def0b206dbbe39e3af558
Untagged: k8s.gcr.io/coredns/coredns:v1.8.4
Untagged: k8s.gcr.io/coredns/coredns@sha256:6e5a02c21641597998b4be7cb5eb1e7b02c0d8d23cce4dd09f4682d463798890
Deleted: sha256:8d147537fb7d1ac8895da4d55a5e53621949981e2e6460976dae812f83d84a44
Deleted: sha256:22ff2e2a31dc2108bfa9a2f9c50bd5c154de33ea30b9fc9bbfe39f82a6e4bd7a
Deleted: sha256:225df95e717ceb672de0e45aa49f352eace21512240205972aca0fccc9612722
Error response from daemon: conflict: unable to delete f30469a2491a (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete f30469a2491a (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 6e002eb89a88 (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 6e002eb89a88 (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete aca5ededae9c (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete aca5ededae9c (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 004811815584 (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 004811815584 (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete ed210e3e4a5b (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete ed210e3e4a5b (must be forced) - image is referenced in multiple repositories

 

检查

经过查阅资料,我发现必须强制删除它们。

可以使用一下两种方法:

  1. 使用-f选项强制删除,即docker rmi -f image-id.
  2. 使用镜像的仓库路径来删除,即docker rmi repository:tag.

两种方法我都试了一下,发现方法1比方法2管用,方法2删除不成功,于是是用方法1来删除。

可是我有很多镜像,一个一个删就到明年了。于是我就是用awk来批量删除了。

[root@node1 ~]# for tag in $(docker images|awk '{print $3}')
> do
> docker rmi -f ${tag}
> done
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

 

后记

我很奇怪为什么这套环境不能搭建成功k8s服务(在其他环境都可以),可能确实是网络环境问题吧,嘎嘎嘎~

 

鸣谢

强制删除docker镜像

上一篇:【K8s故障排查】kubectl get componentstatuses 命令执行问题


下一篇:问题总结