在上文中,已經安裝好docker-ce-18.06.3.ce-3.el7,接來下介紹鏡像基本操做和示範php
看到這裏的朋友,應該有嘗試過從互聯網上拉取鏡像;拉取完後的鏡像到底存儲在本地哪裏呢?如何去查看鏡像的屬性呢?nginx
以上文中拉取的tomcat鏡像爲例,以前介紹過優化Docker數據存儲目錄;這個目錄存儲了鏡像信息、容器信息、網絡信息、swarm、構建鏡像歷史日誌、鏡像元數據等信息,下面爲Docker數據存儲父目錄結構。redis
[root@doc03 app]# pwd /opt/app [root@doc03 app]# tree -L 1 Docker/ Docker/ ├── builder ├── buildkit ├── containerd ├── containers ├── image ├── network ├── overlay2 ├── plugins ├── runtimes ├── swarm ├── tmp ├── trust └── volumes
其中image目錄下存儲的是鏡像數據,裏面有3個特殊文件須要作說明docker
IMAGE ID是以這個文件名前12位字符命名。json
[root@doc03 sha256]# pwd /opt/app/Docker/image/overlay2/imagedb/content/sha256 [root@doc03 sha256]# ll total 16 -rw------- 1 root root 14458 Apr 5 14:23 040bdb29ab375db2a8de090070dcbee19d43f609385a934c3f2b423e515f57bb [root@doc03 sha256]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 040bdb29ab37 2 months ago 649MB
以前說道過,鏡像是由一層或多層組成,distribution目錄下存儲的就是依賴鏡像的信息。細心的小夥伴會發現,依賴鏡像文件對應拉取tomcat的"b9a857cbf04d: Pull complete"等鏡像,也是由前12字符字符命名tomcat
[root@doc03 sha256]# pwd /opt/app/Docker/image/overlay2/distribution/diffid-by-digest/sha256 [root@doc03 sha256]# ll total 40 -rw-r--r-- 1 root root 71 Apr 5 14:23 00612a99c7dc3f9e7f9b8d951893bd5db3c0021f8452d165ea698f41701fae06 -rw-r--r-- 1 root root 71 Apr 5 14:23 326f9601c512f9d28e46996c1af4500ddf8fcbb0f172a57c9010146ed64232af -rw-r--r-- 1 root root 71 Apr 5 14:22 3b9ca4f00c2e4896c65625d678544b764d7483dca9dcab92b62093db72f21d3e -rw-r--r-- 1 root root 71 Apr 5 14:22 511ef4338a0b97df212cc0048cfc64b1956d438a987351ea24acf4841801d5cc -rw-r--r-- 1 root root 71 Apr 5 14:22 661d3b55f65797df3cf7dc0ce8338bdc5d6ae69eeeac2a3228f2baa9fa518047 -rw-r--r-- 1 root root 71 Apr 5 14:22 667fd949ed9338da3c1e09f9b75408c214368c637c448e0fd839467f6f7717b5 -rw-r--r-- 1 root root 71 Apr 5 14:23 a56db448fefe2f4c9fe6121623d431bd522b7c6637058504e3e18a90bc159740 -rw-r--r-- 1 root root 71 Apr 5 14:22 b9a857cbf04d2c0d2f0f6b73e894b20a977a6d3b6edd9e27d080e03142954950 -rw-r--r-- 1 root root 71 Apr 5 14:23 c547db74f1e190988555d3a44be527af3a33ce62899fa5d18dfa48a12d9ba860 -rw-r--r-- 1 root root 71 Apr 5 14:22 d557ee20540b597f518df05bc6888778cfc92bf46040c701d4a622389feb6807
存儲每一個鏡像的摘要,全部鏡像的摘要都存儲在這一個文件中;若鏡像刪除,則此文件裏對應的摘要信息也會刪除bash
[root@doc03 overlay2]# pwd /opt/app/Docker/image/overlay2 [root@doc03 overlay2]# cat repositories.json {"Repositories":{"nginx":{"nginx:1-alpine":"sha256:629df02b47c8733258baf6663e308a86cd23f80247d35407022c35fd91a50ea3","nginx:1-alpine-perl":"sha256:331c9790e872ca93a29241137d175078874bc7cdba42744ccf60f7cd95d06f4c","nginx@sha256:3bf1ec64ddaf78e5307ea10075fccc402fa9ddbf10945dd41f39930592c8f19c":"sha256:331c9790e872ca93a29241137d175078874bc7cdba42744ccf60f7cd95d06f4c","nginx@sha256:c2ce58e024275728b00a554ac25628af25c54782865b3487b11c21cafb7fabda":"sha256:629df02b47c8733258baf6663e308a86cd23f80247d35407022c35fd91a50ea3"},"tomcat":{"tomcat:latest":"sha256:040bdb29ab375db2a8de090070dcbee19d43f609385a934c3f2b423e515f57bb","tomcat@sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305":"sha256:040bdb29ab375db2a8de090070dcbee19d43f609385a934c3f2b423e515f57bb"}}}
正常來講,只須要給出鏡像的名字和標籤,就能夠在官方中定位一個鏡像,採用":"分割網絡
拉取鏡像指令app
# 拉取某個倉庫的某個鏡像 docker pull <repository>:<tag> # 拉取某個倉庫下全部版本的鏡像 docker pull -a <repository>:<tag>
當不指名<tag>時,默認拉取最新版本鏡像,示例以下(倉庫、版本筆者隨意)
運維
# 從官方拉取redis最新版本鏡像 docker pull redis <==> docker pull redis:latest # 從官方拉取redis 3.2.8版本鏡像 docker pull redis:3.2.8 # 從我的倉庫tianciwang下拉取nginx V20.1.1版本鏡像 docker pull harbor/tianciwang/nginx:V20.1.1
一個鏡像能夠根據需求設定多個不一樣標籤,每一個鏡像也能夠對應本身獨有的標籤
查看已經拉取到本地的鏡像
docker images
這裏以nginx鏡像爲例(筆者下載2個鏡像後強行中止了)
root@doc03 ~]# docker pull -a nginx 1-alpine-perl: Pulling from library/nginx 801bfaa63ef2: Pull complete 14fa53e0f410: Pull complete 5b2b4d560b6b: Pull complete 788e9d3ddd79: Pull complete e6c5b8607d37: Pull complete Digest: sha256:3bf1ec64ddaf78e5307ea10075fccc402fa9ddbf10945dd41f39930592c8f19c 1-alpine: Pulling from library/nginx 801bfaa63ef2: Already exists b1242e25d284: Pull complete 7453d3e6b909: Pull complete 07ce7418c4f8: Pull complete e295e0624aa3: Pull complete Digest: sha256:c2ce58e024275728b00a554ac25628af25c54782865b3487b11c21cafb7fabda [root@doc03 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 040bdb29ab37 2 months ago 649MB nginx 1-alpine-perl 331c9790e872 3 months ago 56.6MB nginx 1-alpine 629df02b47c8 3 months ago 22.3MB
上述步驟中發生了幾件事情
● 首先從nginx倉庫總中拉取了2個鏡像,版本爲"1-alpine-perl"、"1-alpine"。
● 其次,"801bfaa63ef2"這個依賴鏡像都會去拉取,當"1-alpine-perl"拉取完畢後,"1-alpine"再次拉取時提示"Already exists";也就是說當拉取多個鏡像時,若是有相同依賴鏡像,則依賴只會拉取一次。
● 在上述示範中,nginx鏡像對應的版本分別是"1-alpine-perl"、"1-alpine",而非最新的"latest",也就是說最新版本"latest"並不是一個強制標籤,不保證是倉庫中最新的鏡像。
docker pull <repository>:<tag>
[root@doc03 ~]# docker images --digests REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE tomcat latest sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305 040bdb29ab37 2 months ago 649MB nginx 1-alpine-perl sha256:3bf1ec64ddaf78e5307ea10075fccc402fa9ddbf10945dd41f39930592c8f19c 331c9790e872 3 months ago 56.6MB nginx 1-alpine sha256:c2ce58e024275728b00a554ac25628af25c54782865b3487b11c21cafb7fabda 629df02b47c8 3 months ago 22.3MB [root@doc03 ~]# docker pull sha256:94cc1....9d1305
Docker提供了 "--filter"參數來過濾"docker ls"命令返回的鏡像列表內容。
Docker支持的過濾方法有如下幾種
● dangling: 布爾型值 true or false,僅返回懸掛鏡像(true),或者非懸掛鏡像(false)
補充: 那些 沒有標籤的鏡像被稱爲懸掛鏡像,在列表中展現爲<none>:<none>
● before: 須要鏡像名稱或IMAGE ID做爲參數,返回在以前被建立的所有鏡像
● since: 與before相似,不過返回的是指定鏡像以後建立的所有鏡像
● lable: 根據標註的名稱或者值,對鏡像進行過濾
● 其餘的過濾方式可使用 reference,方法相似"Linux"通配符,示範以下
[root@doc03 ~]# docker images --filter=reference="*:latest" REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 040bdb29ab37 2 months ago 649MB [root@doc03 ~]# docker images --filter=reference="*alpin*" REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1-alpine-perl 331c9790e872 3 months ago 56.6MB nginx 1-alpine 629df02b47c8 3 months ago 22.3MB
運維人員第一時間想到的是經過"awk"、"grep"來實現格式化輸出,可是Docker自身提供了"--format"參數來對輸出內容進行格式化。
[root@doc03 ~]# docker images --format "{{.Repository}} {{.Tag}}: {{.Size}}" tomcat latest: 649MB nginx 1-alpine-perl: 56.6MB nginx 1-alpine: 22.3MB
筆者有個問題,如何對"IMAGE ID"這種有特殊字符" "的字段進行格式化輸出。
Docker提供了"search"方式搜索,能夠經過"NAME"字段進行匹配過濾,而且基於返回內容中任意列的值進行過濾。
[root@doc03 ~]# docker search nginx NAME DESCRIPTION STARS AUTOMATED nginx Official build of Nginx. 14678 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2005 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 813 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 171 mailu/nginx Mailu nginx frontend 8 [OK]
須要注意,上面返回的鏡像中既有官方的也有非官方的,可使用"--filter" "is-official=true"進行過濾
[root@doc03 ~]# docker search nginx --filter "is-official=true" NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 14678 [OK]
[root@doc03 ~]# docker search nginx --filter "is-automated=true" NAME DESCRIPTION STARS AUTOMATED jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2005 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 813 [OK] tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 121 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 100 [OK]
默認狀況下,鏡像查詢只返回前25行結果,能夠經過"--limit"參數來限制展現的行數,最多爲100行。
當不在須要有個鏡像時,能夠經過"docker rmi"命令刪除。
刪除操做會在當前主機刪除該鏡像已經依賴的鏡像。這意味着當所有依賴改鏡像的鏡像都被刪除後,該鏡像纔會被刪除。
當被刪除的鏡像上存在運行狀態的容器,那麼刪除操做不被容許,在執行刪除鏡像操做以前,須要中止並刪除該鏡像相關的全部容器。
能夠根據"IMAGE ID"進行單個鏡像刪除或多個鏡像刪除(筆者在刪除前已經關停、刪除相關容器)
[root@doc03 overlay2]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 040bdb29ab37 2 months ago 649MB nginx 1-alpine-perl 331c9790e872 3 months ago 56.6MB nginx 1-alpine 629df02b47c8 3 months ago 22.3MB [root@doc03 overlay2]# docker rmi 040bdb29ab37 Untagged: tomcat:latest Untagged: tomcat@sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305 Deleted: sha256:040bdb29ab375db2a8de090070dcbee19d43f609385a934c3f2b423e515f57bb Deleted: sha256:03ac467e286d36301dba7daaec530f481074197d22d41bd48a32e5a2e3069573 Deleted: sha256:c56c3d4e36aac2060fdb29c95baf4b4d6916fe9a454751b77b8d89f27dd8dda4 Deleted: sha256:0f3911a87064866cd2aa7fd863afc1e02bdfa4856afa351e871f0e6a48cfd52b Deleted: sha256:7891c5716382337382836e0af1ea998bf77c1eed9c8e806c7aac5f489952f4ba Deleted: sha256:56dee2fc4110951264d4b134b303920c633aabbbb0264dc218518b3547320de2 Deleted: sha256:cd253bbc5a1ce457359958f4905b53a3f6f0ac167ba5065f68385600fdbae99e Deleted: sha256:03b0292dbc6978fe1077f948ac07472a8cac23fb4c3cf92c76f5eb62891e3d5a Deleted: sha256:19f2a825ed46bbf16a06d9d5185a0ac68196217f302e760abb4cbc3178b14cc1 Deleted: sha256:b4f0436e967b6b9b021d3ed900a5422da6d8fe8c17c51c7d7e4f9c105812f868 Deleted: sha256:4762552ad7d851a9901571428078281985074e5ddb806979dd7ad24748db4ca0
[root@doc03 overlay2]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1-alpine-perl 331c9790e872 3 months ago 56.6MB nginx 1-alpine 629df02b47c8 3 months ago 22.3MB [root@doc03 overlay2]# docker rmi $(docker images -q) Untagged: nginx:1-alpine-perl Untagged: nginx@sha256:3bf1ec64ddaf78e5307ea10075fccc402fa9ddbf10945dd41f39930592c8f19c Deleted: sha256:331c9790e872ca93a29241137d175078874bc7cdba42744ccf60f7cd95d06f4c Deleted: sha256:f8bb5252256d3e1f73a9add79f3808e7c82e2e656263c5e5167f18ce451c3ed9 Deleted: sha256:48fb228af85715312488a1aaad44a2fbbb4ba12b2ebf304e332fc351995099fa Deleted: sha256:66c4f586c95355b80665d617931a6b350f21e313db4fb3769d6cbc5e4785fa40 Deleted: sha256:4e46e0339c100da8d1b7041db8c7978f7271c9db5f646b8f1461de4c89099e8f Untagged: nginx:1-alpine Untagged: nginx@sha256:c2ce58e024275728b00a554ac25628af25c54782865b3487b11c21cafb7fabda Deleted: sha256:629df02b47c8733258baf6663e308a86cd23f80247d35407022c35fd91a50ea3 Deleted: sha256:41d2e92ec9d933298115cd01bae2d829a74fdab4817485c525825a1006a115bb Deleted: sha256:8c8641668583c076b6b0ab21acb272b9e693df25789c45efdd74e4f66c3249b3 Deleted: sha256:ba6fbd74fed2b1637de4430d2030389a419c0243a9f0b64443f2df032e46fa20 Deleted: sha256:9eb5ffb63a02018a7a90c5e26345e773faefe07eec78b155e6c994c2a35f97b9 Deleted: sha256:777b2c648970480f50f5b4d0af8f9a8ea798eea43dbcf40ce4a8c7118736bdcf [root@doc03 overlay2]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE
"docker images -q"展現全部鏡像的"IMAGE ID",能夠結合"--filter"過濾本身想要的"IMAGE ID"
● docker pull image
下載鏡像的命令。鏡像從遠程鏡像服務的倉庫中下載。默認狀況下回城Docker Hub的倉庫中拉取。 docker pull image:latest命令會拉取image最新版本,"-a"參數或拉取全部倉庫某個標籤的鏡像
● docker image
列出本地Docker主機上存儲的鏡像。能夠經過"--digests"參數來查看鏡像摘要
● docker inspect image
展現鏡像的細節,包括鏡像層數據和元數據
● docker rmi image
用於刪除鏡像。當鏡像存在關聯的容器,而且容器處於運行或中止狀態時,不容許刪除改鏡像