docker可以使用rmi命令去删除镜像。查看docker rmi命令的帮助信息,如下:
[root@localhost ~]# docker rmi --help Usage: docker rmi [OPTIONS] IMAGE [IMAGE...] Remove one or more images Options: -f, --force Force removal of the image --no-prune Do not delete untagged parents
移除一个或多个镜像。其中:
-f, --force 强制删除镜像
--no-prune 不删除未标记的父镜像
实例:删除版本为1.4.21的memcached镜像。如下:
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 89481b5d9082 5 days ago 506MB redis latest 598a6f110d01 5 days ago 118MB ubuntu latest 4c108a37151f 4 weeks ago 64.2MB centos 6 d0957ffdf8a2 4 months ago 194MB centos latest 9f38484d220f 4 months ago 202MB memcached 1.4.21 622897abf0a4 4 years ago 130MB [root@localhost ~]# docker rmi memcached:1.4.21 Untagged: memcached:1.4.21 Untagged: memcached@sha256:b3bc535e6d264df171df096602c96cdd0621b5e91a38701c230e771424a584c0 Deleted: sha256:622897abf0a4c6aa9f56d8b97cdc0b20cf160557cd0ace2c5b2d9ed6b55fcf1c Deleted: sha256:20dfcac3234e5033fbd0bc36d982abbab4bbc27ba490ac8be503448524c38c21 Deleted: sha256:b9d7216959acbf36959b844aa75b045dca2a607793158cf48f51af38a593c830 Deleted: sha256:cd561c67f7426f35f8c06c9cb66fef39be25b7169a0ef7a9a31d568fc1540d79 Deleted: sha256:cbf4b7d14f4381bb7300a849382c34078ef00b02b75a481de626c01520414e17 Deleted: sha256:2ecb55e3604df4d7327fd2f5c52f65f31911a6493c8c11aa725a86101f447a7c Deleted: sha256:1fe9f4d7dd42b053c904a4c75023ca3f884ac7fef236430f2a1e0b4d3045bf79 Deleted: sha256:827230f6372457287ba19020bfb5758a833aff3147d3a8bc8d5987fc0ed28227 Deleted: sha256:ae48d4d543a01eaba5f5efa593d27b20166e37292aa544c47c013cdbe205b443 Deleted: sha256:a6aaaca4fe718c40bb4dea9a6072fcbf912880c107e06a501d8aede499b1b58e Deleted: sha256:691aabced0923e8d4c8ed7b2881d1cdd84f86b862a79d1607012573255b79486 Deleted: sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef
假如你删除的是一个已经正在运行的镜像。此时,将抛出错误信息,如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e19978a0bc60 memcached:1.4.21 "memcached" 41 seconds ago Up 41 seconds 11211/tcp compassionate_elgamal [root@localhost ~]# docker rmi memcached:1.4.21 Error response from daemon: conflict: unable to remove repository reference "memcached:1.4.21" (must force) - container e19978a0bc60 is using its referenced image 622897abf0a4
提示我们使用 -f 强制删除镜像。如下:
[root@localhost ~]# docker rmi memcached:1.4.21 -f Untagged: memcached:1.4.21 Untagged: memcached@sha256:b3bc535e6d264df171df096602c96cdd0621b5e91a38701c230e771424a584c0
一次删除多个镜像(多个镜像之间使用空格分隔),如下:
[root@localhost ~]# docker rmi -f centos:6 ubuntu redis Untagged: centos:6 Untagged: centos@sha256:dec8f471302de43f4cfcf82f56d99a5227b5ea1aa6d02fa56344986e1f4610e7 Deleted: sha256:d0957ffdf8a2ea8c8925903862b65a1b6850dbb019f88d45e927d3d5a3fa0c31 Deleted: sha256:af6bf1987c2eb07d73f33836b0d8fd825d7c785273526b077e46780e8b4b2ae9 Untagged: ubuntu:latest Untagged: ubuntu@sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c Deleted: sha256:4c108a37151f54439950335c409802e948883e00c93fdb751d206c9a9674c1f6 Untagged: redis:latest Untagged: redis@sha256:8888f6cd2509062a377e903e17777b4a6d59c92769f6807f034fa345da9eebcf Deleted: sha256:598a6f110d01ba2d8086011cf70571b4a52a532e25189a70042171ed9d700b50
如果有很多镜像,逐一删除也很麻烦,我们可以批量删除。如下:
[root@localhost ~]# docker rmi $(docker images -q) -f Untagged: tomcat:latest Untagged: tomcat@sha256:ddfd0fe3d5b77191b6d8b99b35a9e10c1192d2d0fa5dea5064367300b723d8af Deleted: sha256:89481b5d9082b9bd76a71239670f1000265635f1f1bbd690fca4707671f9753c Deleted: sha256:5dc0fb7fbc3c596bac1d8677a3c451ea1bea8814108f35b47d48900d5d2ef1e9 Deleted: sha256:30fdd94c060b2d6f39e54e8929a4f1dd0864343ff87e6ce6d119ad6e2b60c79f Deleted: sha256:11d326def8b49f5abffe7187e9866fc23d023f2a39ab67da7dd0f732c727c16d Deleted: sha256:6ee8274630de773cbf54fb62e4ab200109f8f1a26e7a8a91bc98196a6ba3f239 Deleted: sha256:b23cfa351628eed4bed4a5d461a81ed32af66c3daebdb45763c110f0a4c2892e Deleted: sha256:ddee1f79c299d9818245b4f6446237105c6687af1915ae97bb283f604fff182b Deleted: sha256:3ed2a85fd2cbb4d32f9be22c9f9d57d7691f64f6275e1067c850c446d5ab47d9 Deleted: sha256:a7fb515b82afb13dc97ba73d90f6543bfc149d9b2060f5b52e15300b26b5e0f1 Deleted: sha256:2588c3b123d0790c6e569fdce63f8d93bd1387973ac74a3b438f738121b4e2e7 Deleted: sha256:e2a8a00a83b20c88b81952f81e6cfc2e2dd5aa7f00a23b067e6342c70602a567 Untagged: centos:latest Untagged: centos@sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c Deleted: sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1
上面删除所有memcached的镜像,如果是“docker rmi $(docker images -q)”将删除所有的镜像。