docker image tag為什么出現(xiàn)none的原因及解決
今天和小伙伴們,聊一個(gè)工作中遇到的小問題~
背景
公司項(xiàng)目是基于容器化架構(gòu)設(shè)計(jì),具體業(yè)務(wù)也拆成了多個(gè)微服務(wù)及對(duì)應(yīng)了多個(gè)docker image鏡像。研發(fā)環(huán)境中會(huì)頻繁進(jìn)行升級(jí)image鏡像,導(dǎo)致出現(xiàn)很多image為 <none> 命名的鏡像,這種也稱為懸空鏡像。
本篇文章將展示幾種現(xiàn)象會(huì)導(dǎo)致這種情況發(fā)生。
下文演示環(huán)境,版本信息如下
- Centos8
- docker server: 23.0.1
- docker client: 20.10.17
- containerd: 1.6.18
- runc version: 1.1.4
現(xiàn)象一
前提條件: 在同環(huán)境中兩個(gè)鏡像名稱:tag都相同,image層數(shù)據(jù)內(nèi)容不一致,覆蓋后則會(huì)出現(xiàn) <none> 標(biāo)志。
注意: 如果僅是鏡像名稱:tag相同則不會(huì)覆蓋,也不會(huì)出現(xiàn) <none> 標(biāo)志。 因?yàn)閕mage層數(shù)據(jù)沒有發(fā)生變化,docker認(rèn)為是同一個(gè)鏡像。
[root@k8s-host docker]# cat > Dockerfile << EOF > FROM alpine:latest > ADD test.txt /root > EOF [root@k8s-host docker]# echo 1 > test.txt [root@k8s-host docker]# docker build -t alpine1:v1.0 . [+] Building 0.1s (7/7) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 134B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/alpine:latest 0.0s => [internal] load build context 0.0s => => transferring context: 96B 0.0s => CACHED [1/2] FROM docker.io/library/alpine:latest 0.0s => [2/2] ADD test.txt /root 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:aa416b5c47216b392755ba2b4ad08f5531f3aa86c103744e261e49ce7a354021 0.0s => => naming to docker.io/library/alpine1:v1.0 0.0s [root@k8s-host docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine1 v1.0 aa416b5c4721 30 seconds ago 7.34MB
該鏡像是基于alpine:latest鏡像進(jìn)行二次編譯
Dockerfile中僅增加 test.txt 用來區(qū)分層內(nèi)容
FROM alpine:latest ADD test.txt /root
上述步驟中執(zhí)行 docker images 可以看到 alpine1:v1.0 已經(jīng)存在鏡像ID aa416b5c4721 的容器鏡像。
下面繼續(xù)編譯鏡像,名字與 alpine1:v1.0 相同, 僅僅將Dockerfile中 test.txt 文件內(nèi)容修改。
進(jìn)行第二次加載鏡像,此時(shí)系統(tǒng)已存在相同的鏡像名稱。
[root@k8s-host docker]# echo 2 > test.txt [root@k8s-host docker]# docker build -t alpine1:v1.0 . [+] Building 0.1s (7/7) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 134B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/alpine:latest 0.0s => [internal] load build context 0.0s => => transferring context: 96B 0.0s => CACHED [1/2] FROM docker.io/library/alpine:latest 0.0s => [2/2] ADD test.txt /root 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:e4159797272a190d20856475858bab23d11e64d6bdd9a57ca8a85ad3c3a17087 0.0s => => naming to docker.io/library/alpine1:v1.0 0.0s [root@k8s-host docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine1 v1.0 e4159797272a 3 seconds ago 7.34MB <none> <none> aa416b5c4721 6 minutes ago 7.34MB
原來的 alpine1:v1.0 鏡像ID為 aa416b5c4721 的docker鏡像目前屬于 none 標(biāo)記,這是因?yàn)橛辛诵碌溺R像名稱相同造成。雖然是none標(biāo)記,如果之前有引用的容器仍然可以使用。
注意:如果將 alpine1:v1.0 鏡像ID為 e4159797272a 的docker鏡像刪除, 下面的none標(biāo)記也不會(huì)發(fā)生變化。只有當(dāng)時(shí)的 Dockerfile中的 ADD 文件重新docker build -t alpine1:v1.0編譯,此處的 none 標(biāo)記才能顯示正常。
現(xiàn)象二
docker save 時(shí)鏡像沒有指定鏡像名稱:tag,而是使用的鏡像ID。
[root@k8s-host docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine1 v1.0 e4159797272a 3 seconds ago 7.34MB [root@k8s-host docker]# docker save e4159797272a | gzip > alpine1.docker [root@k8s-host docker]# docker rmi e4159797272a Untagged: alpine1:v1.0 Deleted: sha256:e4159797272a190d20856475858bab23d11e64d6bdd9a57ca8a85ad3c3a17087 [root@k8s-host docker]# docker load < alpine1.docker Loaded image ID: sha256:e4159797272a190d20856475858bab23d11e64d6bdd9a57ca8a85ad3c3a17087 [root@k8s-host docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> e4159797272a 21 minutes ago 7.34MB
操作中,將alpine1:v1.0鏡像沒有按照鏡像:tag方式 save,而是使用 docker save e4159797272a 鏡像ID格式保存, 僅僅微小的變化會(huì)導(dǎo)致鏡像加載時(shí)的效果不同。
當(dāng)執(zhí)行完 docker load < alpine1.docker 加載鏡像后發(fā)現(xiàn),此時(shí)的鏡像ID還是原來沒有變化,而鏡像名稱:TAG 都變成none。
注意:如果 docker save 導(dǎo)出鏡像時(shí)不指定 鏡像名稱:tag 這種方式,在docker load 加載鏡像時(shí)會(huì)丟失鏡像名稱和tag標(biāo)記。
現(xiàn)象三
正運(yùn)行的容器所引用的鏡像,將其強(qiáng)行刪除鏡像docker images會(huì)出現(xiàn)none標(biāo)記
[root@k8s-host docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine4 v1.0 7ea0f12a3321 7 hours ago 7.34MB [root@k8s-host docker]# docker rmi alpine4:v1.0 Error response from daemon: conflict: unable to remove repository reference "alpine4:v1.0" (must force) - container 945e9f47d833 is using its referenced image 7ea0f12a3321 [root@k8s-host docker]# docker rmi -f alpine4:v1.0 Untagged: alpine4:v1.0 [root@k8s-host docker]# docker ps -a | grep alpine 945e9f47d833 7ea0f12a3321 "/bin/sh -c 'tail -f…" 54 seconds ago Up 53 seconds alpine4 [root@k8s-host docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 7ea0f12a3321 7 hours ago 7.34MB
操作中執(zhí)行 docker rmi -f alpine4:v1.0 命令后,僅刪除了鏡像:tag,并沒有刪除鏡像數(shù)據(jù)層。
可以看到只有鏡像名稱:tag 變成了none,實(shí)際的鏡像還是存在的。因?yàn)槟壳?945e9f47d833 容器中引用到了 7ea0f12a3321 鏡像是無法使用 docker rmi -f 刪除的。
擴(kuò)展內(nèi)容
尤其在測(cè)試、研發(fā)環(huán)境中會(huì)出現(xiàn)很多 <none> 標(biāo)記的鏡像,占用較多資源。 在docker cli工具中可以使用如下命令來清理未使用的鏡像。
$ docker system prune
注意:該命令包含刪除所有未使用的容器、網(wǎng)絡(luò)、鏡像(懸空的和未引用的),以及卷(可選)。
也可單獨(dú)刪除所有無名的鏡像(懸空鏡像)
$ docker rmi $(docker images -f "dangling=true" -q)
到此這篇關(guān)于docker image tag為什么出現(xiàn)none的原因及解決的文章就介紹到這了,更多相關(guān)docker image tag none內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
docker網(wǎng)絡(luò),docker-compose?network問題
這篇文章主要介紹了docker網(wǎng)絡(luò),docker-compose?network問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Windows11安裝Docker Desktop教程的圖文教程
本文主要介紹一下Windows11安裝Docker Desktop教程的圖文教程,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10Docker 制作鏡像Dockerfile和commit操作
這篇文章主要介紹了Docker 制作鏡像Dockerfile和commit操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11No route to host兩個(gè)docker容器的服務(wù)訪問不通的解決
在CentOS服務(wù)器上使用Docker容器時(shí),當(dāng)容器之間的服務(wù)調(diào)用出現(xiàn)“Failed to establish a new connection: [Errno 113] No route to host”錯(cuò)誤,是因?yàn)槿萜鞯膱?bào)文源地址被防火墻攔截,解決方法有兩種:在防火墻上開放指定端口或關(guān)閉防火墻2025-02-02Docker搭建服務(wù)器監(jiān)控面板的實(shí)現(xiàn)示例
Docker服務(wù)器監(jiān)控面板是一種用于監(jiān)控容器運(yùn)行情況的工具,本文主要介紹了Docker搭建服務(wù)器監(jiān)控面板的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01