Docker从入门到精通_03 Docker镜像管理全攻略:从查看到删除,一文掌握所有操作技巧一 查看镜像/检索镜像1.1 查看镜像1.2 查看所有镜像(包含中间层次镜像)1.3 只查看镜像ID信息1.4 检索本地是否包含httpd相关镜像二 搜索docker镜像仓库镜像三 获取镜像(镜像仓库)3.1 获取latest版本3.2 获取tag版本3.3 根据摘要信息拉取3.4 添加tag信息四 删除镜像4.1 删除镜像名称httpd 2.24.2 删除镜像名称httpd 2.44.3 删除镜像名称hello-world五 强制删除镜像5.1 强制删除镜像方法一5.2 强制删除镜像方法二六 删除所有镜像/强制删除所有镜像6.1 删除所有镜像方法一6.2 删除所有镜像方法二6.3 强制删除所有镜像方法一6.4 强制删除所有镜像方法二
Docker从入门到精通_03 Docker镜像管理全攻略:从查看到删除,一文掌握所有操作技巧
一 查看镜像/检索镜像
1.1 查看镜像
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4 6fd77d7e5eb7 2 months ago 167MB
httpd latest 6fd77d7e5eb7 2 months ago 167MB
hello-world latest d2c94e258dcb 8 months ago 13.3kB
httpd 2.2 e06c3dbbfe23 5 years ago 171MB
[superman@docker ~]$
1.2 查看所有镜像(包含中间层次镜像)
[superman@docker ~]$ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4 6fd77d7e5eb7 2 months ago 167MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
httpd 2.2 e06c3dbbfe23 5 years ago 171MB
[superman@docker ~]$
1.3 只查看镜像ID信息
[superman@docker ~]$ docker images -q
6fd77d7e5eb7
dabbfbe0c57b
feb5d9fea6a5
e06c3dbbfe23
[superman@docker ~]$
1.4 检索本地是否包含httpd相关镜像
[superman@docker ~]$ docker images httpd
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4 6fd77d7e5eb7 2 months ago 167MB
httpd latest dabbfbe0c57b 2 years ago 144MB
httpd 2.2 e06c3dbbfe23 5 years ago 171MB
[superman@docker ~]$
二 搜索docker镜像仓库镜像
web - docker-hubdocker search {image-name}
[superman@docker ~]$ docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 4627 [OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 5
paketobuildpacks/httpd 0
vulhub/httpd 0
jitesoft/httpd Apache httpd on Alpine linux. 0
openquantumsafe/httpd Demo of post-quantum cryptography in Apache … 6
wodby/httpd 0
dockette/httpdump 0
betterweb/httpd 0
dockette/apache Apache / HTTPD 1 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 46
manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
centos/httpd-24-centos8 3
dockerpinata/httpd 1
19022021/httpd-connection_test This httpd image will test the connectivity … 0
httpdocker/kubia 0
publici/httpd httpd:latest 1 [OK]
centos/httpd 36 [OK]
e2eteam/httpd 0
manasip/httpd 0
httpdss/archerysec ArcherySec repository 0 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
patrickha/httpd-err 0
hypoport/httpd-cgi httpd-cgi 2 [OK]
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
[superman@docker ~]$
三 获取镜像(镜像仓库)
3.1 获取latest版本
[superman@docker ~]$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Already exists
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[superman@docker ~]$
3.2 获取tag版本
[superman@docker ~]$ docker pull nginx:1.9.6
1.9.6: Pulling from library/nginx
[DEPRECATION NOTICE] Docker Image Format v1, and Docker Image manifest version 2, schema 1 support will be removed in an upcoming release. Suggest the author of docker.io/library/nginx:1.9.6 to upgrade the image to the OCI Format, or Docker Image manifest v2, schema 2. More information at https://docs.docker.com/go/deprecated-image-specs/
237d5fcd25cf: Pull complete
a3ed95caeb02: Pull complete
d716b6676509: Pull complete
7317143ee399: Pull complete
cd437aeec285: Pull complete
460a06051bae: Pull complete
a09d61f383e8: Pull complete
Digest: sha256:55aa237fd10b203d2160b65c33b444c8dd94ebdbe121f3ff0e05daa496301c70
Status: Downloaded newer image for nginx:1.9.6
docker.io/library/nginx:1.9.6
[superman@docker ~]$
3.3 根据摘要信息拉取
[superman@docker ~]$ docker pull nginx@DIGEST
下面先在Docker主机上查看nginx镜像摘要信息,再删除nginx:latest镜像,然后根据摘要再次拉取nginx:latest镜像。
1.执行docker image ls --digests nginx命令查看在本地查看镜像摘要-基于其内容的密码散列值。这里观察到DIGEST值为sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31。
[superman@docker ~]$ docker image ls --digests nginx
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
nginx latest sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 605c77e624dd 2 years ago 141MB
nginx 1.9.6 sha256:55aa237fd10b203d2160b65c33b444c8dd94ebdbe121f3ff0e05daa496301c70 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
2.执行docker image rm nginx:latest命令删除nginx:latest镜像。
[superman@docker ~]$ docker image rm nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
[superman@docker ~]$
3.执行docker image ls命令查看镜像是否成功删除,这里观察到,已成功删除。
[superman@docker ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4 6fd77d7e5eb7 2 months ago 167MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
httpd 2.2 e06c3dbbfe23 5 years ago 171MB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
4.执行docker image pull nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31命令根据摘要再次拉取nginx:latest镜像。
[superman@docker ~]$ docker image pull nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
docker.io/library/nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31: Pulling from library/nginx
a2abf6c4d29d: Already exists
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
docker.io/library/nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
[superman@docker ~]$
3.4 添加tag信息
$ docker tag
示例:
# 查看本地镜像
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
httpd 2.2 e06c3dbbfe23 6 years ago 171MB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
# tag为默认
[superman@docker ~]$ docker tag 6fd77d7e5eb7 apache2.4
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
httpd 2.4 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
httpd 2.2 e06c3dbbfe23 6 years ago 171MB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
# tag为指定
[superman@docker ~]$ docker tag 6fd77d7e5eb7 apache:2.4
[superman@docker ~]$
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
httpd 2.4 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
httpd 2.2 e06c3dbbfe23 6 years ago 171MB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
四 删除镜像
4.1 删除镜像名称httpd 2.2
[superman@docker ~]$ docker image rm httpd:2.2
示例:
# 查看本地镜像
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
httpd 2.4 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
httpd 2.2 e06c3dbbfe23 6 years ago 171MB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
# 删除httpd:2.2镜像
[superman@docker ~]$ docker image rm httpd:2.2
Untagged: httpd:2.2
Untagged: httpd@sha256:9784d70c8ea466fabd52b0bc8cde84980324f9612380d22fbad2151df9a430eb
Deleted: sha256:e06c3dbbfe239c6fca50b6ab6935b3122930fa2eea2136979e5b46ad77ecb685
Deleted: sha256:aefd81e57d336a46bb1eb4c4626a3f971759e0e9651e348d5e83b301614b4522
Deleted: sha256:b85fe9130aef01e870e6d0a108fd003cf803d5da7fe479513a21de0643d7ecae
Deleted: sha256:40154da5ddb3570d82395b9c632880baca5f12ad21ea8c699f3897cd5ed9b237
Deleted: sha256:f5863aa45770c31d7555287f821930e41bc466b97464733ccf980f16393d99d9
Deleted: sha256:4bcdffd70da292293d059d2435c7056711fab2655f8b74f48ad0abe042b63687
[superman@docker ~]$
# 查看本地镜像
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4 6fd77d7e5eb7 3 months ago 167MB
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
4.2 删除镜像名称httpd 2.4
[superman@docker ~]$ docker rmi httpd:2.4
示例:
# 查看本地镜像
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4 6fd77d7e5eb7 3 months ago 167MB
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
# 删除httpd:2.4镜像
[superman@docker ~]$ docker rmi httpd:2.4
Untagged: httpd:2.4
Untagged: httpd@sha256:f0a93744d8006e6f7ee5086c9ddccdcfa33d1091f15269a00547b4c382459c1f
[superman@docker ~]$
# 查看本地镜像
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
4.3 删除镜像名称hello-world
[superman@docker ~]$ docker image rm hello-world
示例:
[superman@docker ~]$ docker image rm hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container bb254edc42e8 is using its referenced image feb5d9fea6a5
删除镜像之前,应该先删除掉该镜像所运行的所有容器。
五 强制删除镜像
5.1 强制删除镜像方法一
[superman@docker ~]$ docker image rm -f hello-world
示例:
# 查看本地镜像
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
# 删除hello-world镜像
[superman@docker ~]$ docker image rm hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container ba8da0ee761b is using its referenced image feb5d9fea6a5
[superman@docker ~]$
# 查看所有的docker容器
[superman@docker ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba8da0ee761b hello-world:latest "/hello" 3 weeks ago Exited (0) 3 weeks ago brave_wiles
b762aaefa6c8 hello-world "/hello" 3 weeks ago Exited (0) 3 weeks ago dazzling_mendeleev
3c59d7d7d6f5 6fd77d7e5eb7 "httpd-foreground" 3 weeks ago Exited (0) 3 weeks ago exciting_pare
[superman@docker ~]$
# 强制删除hello-world镜像
[superman@docker ~]$ docker image rm -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
[superman@docker ~]$
# 查看本地镜像
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
5.2 强制删除镜像方法二
[superman@docker ~]$ docker rmi -f hello-world
六 删除所有镜像/强制删除所有镜像
6.1 删除所有镜像方法一
[superman@docker ~]$ docker image rm $(docker images -aq)
示例:
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
nginx 605c77e624dd 2 years ago 141MB
httpd latest dabbfbe0c57b 2 years ago 144MB
nginx 1.9.6 a9f920c44e20 8 years ago 133MB
[superman@docker ~]$
[superman@docker ~]$ docker image rm $(docker images -aq)
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Untagged: httpd:latest
Untagged: httpd@sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Deleted: sha256:dabbfbe0c57b6e5cd4bc089818d3f664acfad496dc741c9a501e72d15e803b34
Deleted: sha256:0e16a5a61bcb4e6b2bb2d746c2d6789d6c0b66198208b831f74b52198d744189
Deleted: sha256:f79670638074ff7fd293e753c11ea2ca0a2d92ab516d2f6b0bac3f4c6fed5d86
Deleted: sha256:189d55cdd18e4501032bb700a511c2d69c82fd75f1b619b5218ea6870e71e4aa
Deleted: sha256:cb038ed3e490a8c0f195cf135ac0d27dd8d3872598b1cb858c2666f2dae95a61
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
Untagged: nginx:1.9.6
Untagged: nginx@sha256:55aa237fd10b203d2160b65c33b444c8dd94ebdbe121f3ff0e05daa496301c70
Deleted: sha256:a9f920c44e20b08fc7d43bc9b4d17394d54861428a05bfea1917809a743473af
Deleted: sha256:aa269ac4f43f8540f0c1b99e8a7cb9198a103d39d55b05e0d98b6b0539a9d817
Deleted: sha256:fb59a5ed70d6a02e57050ee4d9f74b63818ecb2f870d173baacfb9e4cf9d98ad
Deleted: sha256:bb81706daf253aead91bce81c03444d156d0122c0782d664394ad48611d61d99
Deleted: sha256:5e1b239c287048913f6ba65717ccaad419088147b51879a884830824ad4d30cc
Deleted: sha256:2d40ee9e461bce5a01569d4e422cb8d8acdaf4672157df4fc99772f807c81359
Deleted: sha256:39443349f3781dcced3407c7aa341fbd3df9078ce824405cc377f52c11bd4d0b
Deleted: sha256:d35809a6e0450b491264b56ee934a7f7f8f651fb6724a9c40c76227439815094
Deleted: sha256:7a30f78ceaba3ed7148a41bdd558c0189bd9a2e74b244b6486022bc8af0244b2
Deleted: sha256:c71dfebadec7259801725ea4a77ea6958678c25bb5e5e80f50e9c89b2d7656e6
Deleted: sha256:aedb1b3b3b6e70ae4a342dfdcea874495b9d095ed6ba8eb4bc08f90ad9e83125
Deleted: sha256:05903cd969529ea56beec880bbeb7e90f1bdc281882f1cf3755760e41b181409
Deleted: sha256:d124781fc06a73b05a8644958397994bae668aba2f06f397fe1387c676b0d86f
Error response from daemon: conflict: unable to delete 6fd77d7e5eb7 (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 6fd77d7e5eb7 (must be forced) - image is referenced in multiple repositories
[superman@docker ~]$
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
[superman@docker ~]$
6.2 删除所有镜像方法二
[superman@docker ~]$ docker rmi $(docker images -aq)
6.3 强制删除所有镜像方法一
[superman@docker ~]$ docker image rm -f $(docker images -aq)
示例:
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache2.4 latest 6fd77d7e5eb7 3 months ago 167MB
apache 2.4 6fd77d7e5eb7 3 months ago 167MB
[superman@docker ~]$
[superman@docker ~]$ docker image rm -f $(docker images -aq)
Untagged: apache2.4:latest
Untagged: apache:2.4
Deleted: sha256:6fd77d7e5eb732dacab601d4556c04a6c312928fb8989fe3b0a47d82db772441
Error response from daemon: No such image: 6fd77d7e5eb7:latest
[superman@docker ~]$
[superman@docker ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[superman@docker ~]$
6.4 强制删除所有镜像方法二
[superman@docker ~]$ docker rmi -f $(docker images -aq)
? 点赞,你的认可是我创作的动力!
⭐️ 收藏,你的青睐是我努力的方向!
✏️ 评论,你的意见是我进步的财富!
PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我吧!
