点击学习开源企业 Docker 镜像仓库 Harbor 软件 教程。
在 Docker 中,使用 docker image rm 命令删除一个或多个镜像。
语法如下:
用法:docker image rm [OPTIONS] IMAGE [IMAGE...] 删除一个或多个镜像 别名: docker image rm、docker image remove、docker rmi 选项: -f、--force 强制删除镜像 --no-prune 不删除未标记的父节点
假设你要删除一个名为nginx:1.19的镜像,可以使用以下命令:
root@hxstrive:~# docker image rm nginx:1.19 Untagged: nginx:1.19 Untagged: nginx@sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2 Deleted: sha256:f0b8a9a541369db503ff3b9d4fa6de561b300f7363920c2bff4577c6c24c5cf6 Deleted: sha256:60f61ee7da08c2a5c5f6a76c1f2926f50ba1d01d8ec4af9afb8fdcd3d97ef6f9 Deleted: sha256:affa58c5a9d1d907c11d8589d4e08d2dc8e4e6b71b141269405a2e67d0a8b011 Deleted: sha256:6b1533d42f38a9c55cad97d4e01c03756ab82b61798b6c4f4bc9122093bb6ebd Deleted: sha256:5c3e94c8305f2a4158258725fe33d2451842c13a97c76f02042a7a7e0aa3799a Deleted: sha256:adda6567aeaa86913f56f0e4647032e1d9347bd63ed98a320f904c71df2637c1 Deleted: sha256:02c055ef67f5904019f43a41ea5f099996d8e7633749b6e606c400526b2c4b33
或者使用镜像 ID 来删除,假设镜像 ID 为 c2c45d506085,可以执行:
root@hxstrive:~# docker image rm c2c45d506085 Untagged: nginx:1.18 Untagged: nginx@sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0 Deleted: sha256:c2c45d506085d300b72a6d4b10e3dce104228080a2cf095fc38333afe237e2be Deleted: sha256:43d6c481a041dbcc1d8ea9c565b1b692bcb28da3414683c316703c669c012ebc Deleted: sha256:defebc732c194dd0b5b39e20c4d014896ce120207f5dfdb41ed6696b0e8224d6 Deleted: sha256:4ea0f2550407442f808812429981c0b62d8dd6a531db8a412640293a1faf8f3c Deleted: sha256:778ca58cf39b8fa0776ade88562750a035a24ec5afb7dc4ab2aa892b2c09769d Deleted: sha256:7e718b9c0c8c2e6420fe9c4d1d551088e314fe923dce4b2caf75891d82fb227d
可以同时删除多个镜像,只需在命令中列出多个镜像的名称或 ID,用空格分隔。
例如,删除tomcat:latest和ubuntu:latest两个镜像:
root@hxstrive:~# docker image rm tomcat ubuntu Untagged: tomcat:latest Untagged: tomcat@sha256:c2f5450b66e82f44bcf9e90ba64bee6175e641cd5d03ae0dde64267b3cfed8d9 Deleted: sha256:70dd81c409f9170c9b4a3f43ba825e86470e4493577a4c9153b91e5a2d5ed82e Deleted: sha256:856b0bca1e236e94d42938b5a91da4a4d643fabe39c2c106dc893e17b718c062 Deleted: sha256:94a819b97c531e343e4cf174587f6d91bf843ff57c305bb48475766f7687abfc Deleted: sha256:89212e0216e8178b2207f696cec7c0989dfd74e558be4efe511615c4115a185f Deleted: sha256:6f2318b3ea0b1d2df776e2d9c6861c5adb2dccd3becca9e667c37ead41559852 Deleted: sha256:5ef77ffa9e536b8fe590a2c934fa52ecb925abcaae90b059e3872083a2a19d35 Deleted: sha256:ee3b59ef26f538ddb2cc6e16eb84173b6d2518378f280c8f4e36d5215cfddb17 Deleted: sha256:a04b04ae84fb580e35a2529048a847e62cc09eba63c39076804a2a4baaed9c73 Deleted: sha256:b868c9ba10cbd428f15bcdc53691fea7f544a2d7a137721f8ee061b12bbe33ba Untagged: ubuntu:latest Untagged: ubuntu@sha256:8a37d68f4f73ebf3d4efafbcf66379bf3728902a8038616808f04e34a9ab63ee Deleted: sha256:edbfe74c41f8a3501ce542e137cf28ea04dd03e6df8c9d66519b6ad761c2598a
或者使用镜像 ID:
docker image rm 123456789abc def012345678
如果某个镜像正在被使用或者有其他问题导致无法直接删除,可以使用-f(或--force)参数强制删除。例如:
# 使用 nginx:1.20 镜像启动一个容器 root@hxstrive:~# docker run -d -p 8080:80 nginx:1.20 eda147abad68c82cf7c30bea4c75d70f604269b6b9d6c95259b39d75ee5d5d52 # 删除镜像 nginx:1.20 失败 root@hxstrive:~# docker image rm nginx:1.20 Error response from daemon: conflict: unable to remove repository reference "nginx:1.20" (must force) - container eda147abad68 is using its referenced image 0584b370e957 # 强制删除镜像 root@hxstrive:~# docker image rm -f nginx:1.20 Untagged: nginx:1.20 Untagged: nginx@sha256:38f8c1d9613f3f42e7969c3b1dd5c3277e635d4576713e6453c6193e66270a6d # 查看镜像 root@hxstrive:~# docker images | grep nginx nginx latest 900dca2a61f5 2 months ago 188MB nginx 1.21.6 0e901e68141f 2 years ago 142MB
🌈 注意:强制删除可能会导致数据丢失或其他问题,所以在使用时要谨慎。
点击学习开源企业 Docker 镜像仓库 Harbor 软件 教程。