1、查看本地鏡像
命令:docker images
,列出本地宿主機上的鏡像。
(1)命令說明
# 查看本地鏡像
[root@192 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 9 days ago 13.3kB
表頭說明:
REPOSITORY
:表示鏡像的倉庫源(名稱)。TAG
:鏡像的標簽(版本)。IMAGE ID
:鏡像的ID。CREATED
:鏡像的創建時間。SIZE
:鏡像的大小。
說明:
latest
表示最新版本。- 同一倉庫源可以有多個
TAG
,代表這個倉庫源的不同個版本,我們使用REPOSITORY:TAG
來定義不同的鏡像。
如果你不指定一個鏡像的版本標簽,例如使用Ubuntu鏡像,Docker將默認使用ubuntu:latest
鏡像。
(2)選項說明
-a
:列出本地所有的鏡像(含中間映像層)。(常用)-q
:只顯示鏡像ID。(常用)--digests
:顯示鏡像的摘要信息(就是備注說明)。--no-trunc
:顯示完整的鏡像信息。
舉例:
[root@192 docker]# docker images -q --no-trunc
sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
2、搜素倉庫鏡像
命令:docker search
,去Docker官方鏡像倉庫去搜索需要的鏡像,地址https://hub.docker.com/。
(1)通過網頁搜索鏡像
我們可以直接在網頁上訪問Docker官方鏡像倉庫https://hub.docker.com/進行搜索。
輸入mysql
進行查詢:
我們點擊進入第一個鏡像,可以看到對於該鏡像的一些詳細介紹和說明。
(2)通過命令搜索鏡像
# search命令使用方式
docker search [OPTIONS] 鏡像名字
示例:執行命令docker search mysql
這和我們通過網頁搜索到的結果是一樣的。
STARS
:點贊或喜歡的意思,代表流行率。OFFICIAL
:表示Docker官方發布。AUTOMATED
:支持自動構建(先理解到這里就可以)。
OPTIONS選項說明:
名稱,簡寫 | 描述 |
---|---|
--filter , -f |
根據提供的條件,顯示過濾后的搜索結果。 |
--format |
使用Go模板進行漂亮的打印搜索(格式化)。 |
--limit |
顯示最多搜索結果數,默認25。 |
--no-trunc |
顯示完整的鏡像描述,默認值為Fasle。 |
提示:老版本的
--stars
和--automated
選項都棄用了。對於選項的使用可以使用
--help
命令或者去官方文檔中去學習,非常的方便,寫的很詳細。
示例:顯示MySQL鏡像STARS
大於3000的鏡像。
[root@192 docker]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10612 [OK]
mariadb MariaDB Server is a high performing open sou… 3979 [OK]
3、下載鏡像
把Docker hub中的鏡像,下載到本地。當然如果我們配置可國內的鏡像地址,會從國內鏡像倉庫進行下載。
命令:docker pull 鏡像名字 [:TAG]
,(pull拉取)
(1)下載最新版本的鏡像
例如:下載MySQL鏡像。
命令:docker pull mysql
提示:
docker pull mysql
等價於docker pull mysql:latest
示例:
[root@192 ~]# docker pull mysql
Using default tag: latest # 如果不寫tag,默認就是latest,也就是默認下載最新版本。
latest: Pulling from library/mysql
a076a628af6f: Pull complete # 分層下載,docker iamge的核心,聯合文件系統
f6c208f3f991: Pull complete # 這里不過多解釋,后面會詳細說明。
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真實下載地址
說明:
docker pull mysql
等同於docker.io/library/mysql:latest
。
這里我們查看一下本地的鏡像,就多可一個MySQL鏡像。
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 9 days ago 13.3kB
mysql latest c8562eaf9d81 7 weeks ago 546MB
提示:
search
搜索的時候是從Docker官方鏡像倉庫進行搜索,而拉取進行的時候,如果我們設置了國內鏡像倉庫地址,是從國內鏡像倉庫進行拉取鏡像,這點要注意。
(2)下載指定版本的鏡像
命令:docker pull 鏡像名字 [:TAG]
可以在網站上Docker hub的官網中https://hub.docker.com/,搜索相應的鏡像,然后點擊進入查看鏡像的倉庫源所支持的TAG版本。
例如以MySQL為例,如下圖:
示例:
# 指定鏡像版本下載
[root@192 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists # 聯合文件系統,已存在的就不下載了。
f6c208f3f991: Already exists # 不多說。
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
我們在來查看一下本地鏡像。
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 9 days ago 13.3kB
mysql 5.7 a70d36bc331a 7 weeks ago 449MB
mysql latest c8562eaf9d81 7 weeks ago 546MB
4、刪除鏡像
命令:docker rmi ID或唯一鏡像名字
(1)刪除單個本地鏡像
刪除hello-world
鏡像。
[root@192 ~]# docker rmi hello-world # 等同於docker rmi hello-world:latest
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container e0ec28c5891b is using its referenced image d1165f221234
我們可以看到hello-world
鏡像沒有刪除成功,守護進程daemon
告訴我們,無法刪除存儲庫“hello-world”
鏡像(必須強制),容器e0ec28c5891b
正在使用其引用的d1165f221234
鏡像。
我們加入一個-f
選項進行強制刪除即可。
[root@192 ~]# docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
我們來查看一下本地鏡像,可以看到hello-world
鏡像已被刪除。
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a70d36bc331a 7 weeks ago 449MB
mysql latest c8562eaf9d81 7 weeks ago 546MB
(2)刪除多個本地鏡像
命令:docker rmi -f 鏡像名1:TAG 鏡像名2:TAG ...
先來查看一下本地鏡像。
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a70d36bc331a 7 weeks ago 449MB
mysql latest c8562eaf9d81 7 weeks ago 546MB
nginx 1.18 b9e1dc12387a 2 months ago 133MB
nginx latest f6d0b4767a6c 2 months ago 133MB
hello-world latest bf756fb1ae65 14 months ago 13.3kB
刪除nginx
和hello-world
鏡像。
[root@192 ~]# docker rmi -f hello-world nginx nginx:1.18
Untagged: hello-world:latest
Untagged: hello-world@sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
...# 省略
Untagged: nginx:latest
Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
...
Untagged: nginx:1.18
Untagged: nginx@sha256:ebd0fd56eb30543a9195280eb81af2a9a8e6143496accd6a217c14b06acd1419
...
查看本地鏡像。
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a70d36bc331a 7 weeks ago 449MB
mysql latest c8562eaf9d81 7 weeks ago 546MB
(3)刪除所有本地鏡像
命令:docker rmi -f $(docker images -qa)
我們先來查看本地鏡像,只顯示鏡像的ID信息。
[root@192 ~]# docker images -qa
a70d36bc331a
c8562eaf9d81
f6d0b4767a6c
bf756fb1ae65
可以看到本地鏡像倉庫中有4個鏡像。
把本地的鏡像全部刪除。
[root@192 ~]# docker rmi -f $(docker images -qa)
Untagged: mysql:5.7
Untagged: mysql@sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
...# 省略
Untagged: mysql:latest
Untagged: mysql@sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
...
Untagged: nginx:latest
Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
...
Untagged: hello-world:latest
Untagged: hello-world@sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
...
再來查看本地鏡像,可以看到一個都沒有了。
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
提示:推薦使用鏡像ID刪除鏡像。