Docker學習之鏡像

Docker 運行容器前須要本地存在對應的鏡像,若是本地不存在該鏡像,Docker 會從鏡像倉庫下載該鏡像。python


獲取鏡像nginx

# docker pull ubuntu:16.04redis


運行 # docker run -it ubuntu:16.04 bashdocker

-i 交互式操做 -t 創建終端 bash 進入交互式shellshell

root@653d9e152ccb:/# id
uid=0(root) gid=0(root) groups=0(root)

root@653d9e152ccb:/# cat /etc/os-release 
NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.4 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"


鏡像體積,image ls 列出來的大小是下載本地展開的大小,和docker hub不一樣。ubuntu

查看鏡像,容器,數據卷佔用的實際空間bash

# docker system dfide

QQ截圖20180511170536.jpg


虛懸鏡像ui

倉庫名、標籤均爲 <none> 的鏡像。這類無標籤鏡像也被稱爲 虛懸鏡像(dangling image)spa

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

<none>             <none>             d92ac4d9c31e        24 hours ago        151MB


使用下面命令專門顯示虛懸鏡像

# docker image ls -f dangling=true


刪除虛懸鏡像

# docker image prune



中間層鏡像

這些無標籤鏡像不該該刪除,不然會致使上層鏡像由於依賴丟失而出錯。實際上,這些鏡像也不必刪除,相同的層只會存一遍,而這些鏡像是別的鏡像的依賴。只要刪除那些依賴它們的鏡像後,這些依賴的中間層鏡像也會被連帶刪除。

# docker image ls -a

QQ截圖20180511172401.jpg


列出部分鏡像

docker image ls ubuntu      #根據倉庫名列出

docker image ls ubuntu:16.04      #列出特定某個鏡像,指定倉庫名和標籤

# docker image ls -f since=nginx     # -f --filter 過濾器參數 查看nginx以後創建的image

# docker image ls -f before=nginx    #查看nginx以前創建的image

QQ截圖20180514094840.jpg


以特定格式顯示

docker image ls -q #只列出image ID


使用Go模板語法,篩選列

# docker image ls --format "{{.ID}}: {{.Repository}}"      #列出鏡像ID和倉庫名

fe0f24bc6366: friendhello

46c7cc28562c: nginx-fun

46ba956c5967: python

ae513a47849c: nginx

0b1edfbffd27: ubuntu


# docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"      #表格等距顯示,自定義顯示列及位置;

QQ截圖20180514095722.jpg



刪除本地鏡像

# docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

friendhello         latest              fe0f24bc6366        3 days ago          151MB

python              2.7-slim            46ba956c5967        8 days ago          140MB

nginx               latest              ae513a47849c        13 days ago         109MB

ubuntu              16.04               0b1edfbffd27        2 weeks ago         113MB


用ID 鏡像名  摘要刪除鏡像

docker image rm ae5 #鏡像ID前3個字符便可

docker image rm nginx #<倉庫名>:<標籤>

docker image rm nginx@摘要

# docker image ls --digests #顯示摘要

REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE

nginx               latest              sha256:0fb320e2a1b1620b4905facb3447e3d84ad36da0b2c8aa8fe3a5a81d1187b884   ae513a47849c        13 days ago         109MB

# docker image rm nginx@sha256:0fb320e2a1b1620b4905facb3447e3d84ad36da0b2c8aa8fe3a5a81d1187b884

Untagged: nginx@sha256:0fb320e2a1b1620b4905facb3447e3d84ad36da0b2c8aa8fe3a5a81d1187b884


docker image ls配合刪除鏡像

# docker image rm $(docker image ls -q redis) #刪除全部倉庫名爲redis的鏡像

# docker image rm $(docker image ls -q -f before=nginx) #刪除全部nginx以前的鏡像

相關文章
相關標籤/搜索