docker鏡像


一、鏡像概念

前置操作拉取鏡像:

# 拉取鏡像
# 拉取最新版本的鏡像
[root@hqs ~]# docker pull ubuntu      
Using default tag: latest
latest: Pulling from library/ubuntu
7c3b88808835: Already exists 
Digest: sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

# 拉取指定版本鏡像
[root@hecs-hqs-01 ~]# docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
2e6e20c8e2e6: Pull complete 
0551a797c01d: Pull complete 
512123a864da: Pull complete 
Digest: sha256:60840958b25b5947b11d7a274274dc48ab32a2f5d18527f5dae2962b64269a3a
Status: Downloaded newer image for ubuntu:14.04
docker.io/library/ubuntu:14.04

二、鏡像查看

1、鏡像查看示例

# 語法
[root@bruce ~]# docker images -h
Flag shorthand -h has been deprecated, please use --help
Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]
List images    # 展示鏡像列表
Aliases:
  docker image ls, docker image list, docker images

Options:
  -a, --all             Show all images (default hides intermediate images)   # 顯示所有鏡像(默認隱藏中間鏡像)
      --digests         Show digests                        # 顯示鏡像的摘要值(由哈希函數sha256計算得出)
  -f, --filter filter   Filter output based on conditions provided     # 根據條件過濾輸出(dangling、label、since、before、reference)
      --format string   Format output using a custom template:          # 指定返回值的模板文件
                        'table':            Print output in table format with column headers (default)
                        'table TEMPLATE':   Print output in table format using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
      --no-trunc        Don t truncate output              # 完整輸出
  -q, --quiet           Only show image IDs                # 只顯示ID值

# 查看本地主機上的鏡像
[root@hqs ~]# docker images
鏡像倉庫            標簽                  鏡像ID              創建時間           鏡像大小
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              54c9d81cbb44        4 weeks ago         72.8MB
ubuntu              14.04               13b66b487594        11 months ago       197MB
hello-world         latest              feb5d9fea6a5        5 months ago        13.3kB

# 查看本地某個鏡像
# 加上--no-trunc顯示完整鏡像ID:UUID形式表示,64個的十六進制字符
# 實際上鏡像ID是鏡像的摘要值(Digest),是由哈希函數sha256對鏡像配置文件計算而來的
[root@hecs-hqs-01 ~]# docker images ubuntu --no-trunc
REPOSITORY          TAG                 IMAGE ID             CREATED             SIZE
ubuntu              latest           sha256:54c9d...f704f   4 weeks ago         72.8MB

# 加上--digests選項可以顯示鏡像摘要值
[root@hqs ~]# docker images ubuntu --digests
鏡像倉庫            標簽           鏡像的摘要值                 鏡像ID              創建時間           鏡像大小
REPOSITORY       TAG             DIGEST                    IMAGE ID            CREATED             SIZE
ubuntu           latest          sha256:8ae9b...3961f9   2b4cba85892a        4 days ago          72.8MB

# 同時輸出摘要值和完整鏡像id
[root@hqs ~]# docker images  ubuntu --digests --no-trunc
REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID                                                                  CREATED             SIZE
ubuntu              latest              sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9   sha256:2b4cba85892afc2ad8ce258a8e3d9daa4a1626ba380677cee93ef2338da442ab   4 days ago          72.8MB
ubuntu              14.04               sha256:60840958b25b5947b11d7a274274dc48ab32a2f5d18527f5dae2962b64269a3a   sha256:13b66b487594a1f2b75396013bc05d29d9f527852d96c5577cc4f187559875d0   11 months ago       197MB

# format指定返回值模板
# 輸出json格式
[root@bruce ~]# docker images --format json
{"Containers":"N/A","CreatedAt":"2021-10-16 08:37:47 +0800 CST","CreatedSince":"16 months ago","Digest":"\u003cnone\u003e","ID":"ba6acccedd29","Repository":"ubuntu","SharedSize":"N/A","Size":"72.8MB","Tag":"latest","UniqueSize":"N/A","VirtualSize":"72.77MB"}
{"Containers":"N/A","CreatedAt":"2021-09-16 02:20:05 +0800 CST","CreatedSince":"17 months ago","Digest":"\u003cnone\u003e","ID":"5d0da3dc9764","Repository":"centos","SharedSize":"N/A","Size":"231MB","Tag":"latest","UniqueSize":"N/A","VirtualSize":"231.2MB"}
# 輸出自定義模板格式
[root@bruce ~]# docker images --format "{{.Repository}}:{{.Tag}}"
ubuntu:latest
centos:latest
ubuntu:14.04
centos/httpd:latest

# 只顯示ID,方便批量操作
[root@bruce ~]# docker images -q
ba6acccedd29
5d0da3dc9764
13b66b487594
2cc07fbb5000

# 根據條件過濾輸出
# dangling:顯示標記為空的鏡像,值只有true和false
[root@bruce ~]# docker images --filter dangling=true
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@bruce ~]# docker images --filter dangling=false
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
ubuntu         latest    ba6acccedd29   16 months ago   72.8MB
# before:根據時間來進行過濾,某個鏡像構建時間之前的鏡像列表
[root@bruce ~]# docker images --filter before=5d0da3dc9764
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
ubuntu         14.04     13b66b487594   23 months ago   196MB
centos/httpd   latest    2cc07fbb5000   4 years ago     258MB
# since:跟before正好相反,某個鏡像構建之后的鏡像列表
[root@bruce ~]# docker images --filter since=5d0da3dc9764 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
ubuntu       latest    ba6acccedd29   16 months ago   72.8MB
# reference:這個是添加正則進行匹配
[root@bruce ~]# docker images --filter reference=*:* 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
ubuntu       latest    ba6acccedd29   16 months ago   72.8MB
centos       latest    5d0da3dc9764   17 months ago   231MB
ubuntu       14.04     13b66b487594   23 months ago   196MB
[root@bruce ~]# docker images --filter reference=ubuntu
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
ubuntu       latest    ba6acccedd29   16 months ago   72.8MB
ubuntu       14.04     13b66b487594   23 months ago   196MB
# label:根據標簽進行過濾,其中lable的值,是docker在編譯的時候配置的或者在Dockerfile中配置的  (需要案例!!!)
[root@bruce ~]# docker images --filter label=hqs
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

2、鏡像標識

鏡像可以通過鏡像ID、鏡像名稱(含標簽)、鏡像摘要值來標識或引用。

# 可以用鏡像倉庫名稱:標簽來標識鏡像
[root@hqs ~]# docker images  ubuntu:14.04 --digests
REPOSITORY          TAG                 DIGEST              IMAGE ID            CREATED             SIZE
ubuntu              14.04               <none>              13b66b487594        11 months ago       197MB
[root@localhost ~]# docker rmi ubuntu:14.04

# 可以用鏡像id來標識鏡像
[root@hqs docker-hello]# docker rmi 2b4cba85892a
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9
Deleted: sha256:2b4cba85892afc2ad8ce258a8e3d9daa4a1626ba380677cee93ef2338da442ab
Deleted: sha256:68a85fa9d77ecac87de23805c4be8766bda08a86787e324036cbcf84b62640fa

# 鏡像可以通過摘要值作為內容尋址標識符,格式為:倉庫名稱@摘要
[root@hqs ~]# docker images ubuntu@sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              <none>              2b4cba85892a        4 days ago          72.8MB
# 嘗試刪除這種方式標識的鏡像,只能刪除摘要值
[root@localhost ~]# docker rmi ubuntu@sha256:9a0bdde4188b896a372804be2384015e90e3f84906b750c1a53539b585fbbe7f
Untagged: ubuntu@sha256:9a0bdde4188b896a372804be2384015e90e3f84906b750c1a53539b585fbbe7f
[root@localhost ~]# docker images --digests
REPOSITORY   TAG       DIGEST    IMAGE ID       CREATED       SIZE
ubuntu       latest    <none>    58db3edaf2be   3 weeks ago   77.8MB

三、鏡像描述文件Dockerfile

Docker所用的Dockerfile文件用來描述鏡像,定義了如何構建Docker鏡像。
Dockerfile是一個文本文件,包含了要構建鏡像的所有命令。
Docker通過讀取Dockerfile中的指令自動構建鏡像。

1、創建hello-world鏡像案例

# 1、找一個目錄創建目錄
[root@hqs hqs]# mkdir docker-hello

# 2、進入這個目錄后創建Dockerfile文件:
vi Dockerfile
FROM scratch
COPY hello /
CMD ["/hello"]     # 注意:CMD后要空格

# 3、再創建hello.c文件
[root@hqs hqs]# vi hello.c
#include<stdio.h>
void main (){
    printf("hello docker\n");
}

# 4、編譯hello文件
# 首先安裝gcc、glibc-static
yum install -y gcc
yum install -y glibc-static
# 進行編譯
[root@hqs docker-hello]# gcc --static hello.c -o hello
[root@hqs docker-hello]# ls
Dockerfile  hello  hello.c

# 4、基於dockerfile創建鏡像
# Dockerfile文件執行build命令來構建鏡像
[root@hqs docker-hello]# docker build -t my-hello . 
Sending build context to Docker daemon  865.3kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : COPY hello /
 ---> 32b0ccadf376
Step 3/3 : CMD ["/hello"]
 ---> Running in fc65eb581596
Removing intermediate container fc65eb581596
 ---> 27f012ef630b
Successfully built 27f012ef630b
Successfully tagged my-hello:latest

# 5、查看鏡像
[root@hqs docker-hello]# docker images          
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-hello            latest              27f012ef630b        44 seconds ago      861kB

# 6、運行鏡像
[root@hqs docker-hello]# docker run my-hello
hello docker

2、dockerfile語法初解

父鏡像(Parent Image):該鏡像的Dockerfile文件中由FROM指定的鏡像。后續指令都應用到這個父鏡像中。
基礎鏡像(Base Image):不依賴其他鏡像,從零開始構建的鏡像。如:未提供FROM指令或提供FROM scratch指令所構建的鏡像。

FROM scratch       # FROM命令定義基礎鏡像,scratch是空白鏡像,表示從零開始構建
COPY hello /       # 將文件復制到鏡像根目錄
CMD ["/hello"]     # 鏡像啟動容器時執行/hello這個可執行文件

四、基於聯合文件系統的鏡像分層

五、Docker鏡像操作命令

docker新版本提供了一個統一的鏡像操作命令:docker image.
操作和傳統的鏡像操作docker子命令相對應,個別語法不一樣。

1、拉取鏡像

從鏡像倉庫中下載鏡像到本地,一般是保存在/var/lib/docker目錄

[root@hqs /]# docker image pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
# 傳統寫法
[root@hecs-hqs-01 ~]# docker pull ubuntu:14.04
[root@hecs-hqs-01 ~]# docker pull ubuntu

2、顯示本地鏡像列表

# 查看語法
[root@hqs ~]# docker images --help
Usage:  docker images [OPTIONS選項] [REPOSITORY倉庫[:TAG標簽]]      # 查看語法只能用鏡像倉庫或者鏡像倉庫+標簽
List images
Options:
  -a, --all             Show all images (default hides intermediate images)     # 列出本地所有的鏡像(含中間鏡像)
      --digests         Show digests                    # 顯示鏡像摘要值
  -f, --filter filter   Filter output based on conditions provided              # 顯示過濾的鏡像
      --format string   Pretty-print images using a Go template
      --no-trunc        Dont truncate output            # 不截斷輸出(顯示完整的鏡像id)
  -q, --quiet           Only show numeric IDs           # 只顯示鏡像ID

# 查看本地鏡像案例
[root@hqs docker-hello]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-hello        latest              7131e3b64d77        42 minutes ago      861kB
ubuntu              latest              2b4cba85892a        5 days ago          72.8MB
[root@hqs docker-hello]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-hello        latest              7131e3b64d77        42 minutes ago      861kB
ubuntu              latest              2b4cba85892a        5 days ago          72.8MB

# -f選項可以通過dangling的布爾值列出無標簽的鏡像(?沒有輸出)
# -f 選項還可以匹配鏡像名、鏡像ID、鏡像DIGEST標識符
[root@hqs /]# docker images -f dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

# -f before=(<鏡像名>[:標簽]|<鏡像ID>|<鏡像digest>)   過濾出指定鏡像之前創建的
# -f since=(<鏡像名>[:標簽]|<鏡像ID>|<鏡像digest>)   過濾出指定鏡像之后創建的
# 使用鏡像名加標簽標識
[root@hqs docker-hello]# docker images -f before=ubuntu:latest
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos/httpd        latest              2cc07fbb5000        3 years ago         258MB
centos/httpd        version1.0          2cc07fbb5000        3 years ago         258MB
# 使用鏡像ID標識
[root@hqs docker-hello]# docker images -f before=2b4cba85892a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos/httpd        latest              2cc07fbb5000        3 years ago         258MB
centos/httpd        version1.0          2cc07fbb5000        3 years ago         258MB
# 使用digest值標識
[root@hqs docker-hello]# docker images -f since=golang@sha256:c72fa9afc50b3303e8044cf28fb358b48032a548e1825819420fd40155a131cb
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-hello        latest              709f66f2ceec        35 minutes ago      861kB
ubuntu              latest              2b4cba85892a        6 days ago          72.8MB

# 通過shell命令替換docker images命令實現鏡像的批量操作(重點)
[root@hqs /]# docker images -q
27f012ef630b
2b4cba85892a
5d0da3dc9764
13b66b487594
[root@hqs /]# docker rmi $(docker images centos -q)
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59

3、設置鏡像標簽

標簽:描述鏡像的版本信息,可以使用docker tag為鏡像添加標簽,即為鏡像命名。
鏡像倉庫可以有多個標簽。一個鏡像也可以有多個標簽。
如果一個鏡像有多個標簽,只有當最后一個標簽被刪除時,才真正刪除鏡像。

# 基本語法
# 創建標簽讓目標鏡像關聯到源鏡像
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker tag  源鏡像[:標簽]  目標鏡像[:標簽]

# 下載centos/httpd鏡像
[root@hqs ~]# docker pull centos/httpd
Using default tag: latest
latest: Pulling from centos/httpd
a02a4930cb5d: Pull complete 
628eaef4a9e0: Pull complete 
20c0ca1c0cd5: Pull complete 
30cf2fb1a57e: Pull complete 
Digest: sha256:26c6674463ff3b8529874b17f8bb55d21a0dcf86e025eafb3c9eeee15ee4f369
Status: Downloaded newer image for centos/httpd:latest
docker.io/centos/httpd:latest

# 為由鏡像ID標識的鏡像加標簽
[root@hqs ~]# docker tag 2cc07fbb5000 centos/httpd:version1.0
[root@hqs ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos/httpd        latest              2cc07fbb5000        3 years ago         258MB
centos/httpd        version1.0          2cc07fbb5000        3 years ago         258MB

# 為由倉庫名稱標識的鏡像加上標簽
[root@hqs docker-hello]# docker tag centos/httpd  centos/httpd:version2.0
[root@hqs docker-hello]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos/httpd        latest              2cc07fbb5000        3 years ago         258MB
centos/httpd        version1.0          2cc07fbb5000        3 years ago         258MB
centos/httpd        version2.0          2cc07fbb5000        3 years ago         258MB

# 為由倉庫名稱和鏡像ID組合標識的鏡像加上標簽   存疑
[root@hqs ~]# docker tag centos/httpd:latest centos/httpd:version3.0.test      
[root@hqs ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos/httpd        latest              2cc07fbb5000        3 years ago         258MB
centos/httpd        version1.0          2cc07fbb5000        3 years ago         258MB
centos/httpd        version2.0          2cc07fbb5000        3 years ago         258MB
centos/httpd        version3.0.test     2cc07fbb5000        3 years ago
[root@bruce my-hello]# docker tag hello-world:latest hqs/hello-world:version4.0.test
[root@bruce my-hello]# docker images
REPOSITORY                    TAG               IMAGE ID       CREATED          SIZE
hqs/hello-world               version4.0.test   f21f18b39c4d   27 minutes ago   865kB

# 指定一個注冊服務器的主機名(可能包含端口)    存疑
[root@hqs ~]# docker tag 2cc07fbb5000  aliyun:5000/centos/httpd:version4.0      
[root@hqs ~]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
aliyun:5000/centos/httpd   version4.0          2cc07fbb5000        3 years ago         258MB
[root@bruce my-hello]# docker tag f21f18b39c4d myhost:5000/hqs/hello-world:version5.0
[root@bruce my-hello]# docker images
REPOSITORY                    TAG               IMAGE ID       CREATED          SIZE
myhost:5000/hqs/hello-world   version5.0        f21f18b39c4d   27 minutes ago   865kB

4、查看鏡像詳細信息

docker inspect 命令查看Docker對象(鏡像、容器、任務)的詳細信息。

  • 默認以JSON數組格式輸出結果。
  • 可以使用 -f(--format) 選項指定特定內容輸出
# 語法
Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects  返回詳細docker對象信息
Options:
  -f, --format string   Format the output using the given Go template        # 格式化輸出
  -s, --size            Display total file sizes if the type is container    # 容器類型顯示全部文件大小
      --type string     Return JSON for specified type                       # 返回json格式信息

[root@hqs ~]# docker inspect centos/httpd 
[
    {
        "Id": "sha256:2cc07fbb5000234e85b7ef63b6253f397491959af2a24251b6ae20c207beb814",
        ...
        "Metadata": {
            "LastTagTime": "2022-03-10T02:35:56.032309948+08:00"
        }
    }
]

# 獲取鏡像的體系結構
[root@hqs ~]# docker inspect --format='{{.Architecture}}' centos/httpd
amd64

# 獲取鏡像的根文件系統信息
[root@hqs ~]# docker inspect --format='{{.RootFS}}' centos/httpd                 
{layers [sha256:071d8bd765171080d01682844524be57ac9883e53079b6ac66707e192ea25956 sha256:d15c61d3ecdaa582e75e2966792238c0325578ec9e0d2a1ed3183995345323d6 sha256:7c937d8a9f4f7a9761c292a6c7bcff1cc10384985282fa4fa5a04bfdb155bc90 sha256:920640105caf8a18f7db0df2638b863cbc73c2e0668d0559c5f9c93c474e8879] }
# 用json格式輸出
[root@hqs ~]# docker inspect --format='{{json .RootFS}}' centos/httpd    # 注意json后空格
{"Type":"layers","Layers":["sha256:071d8bd765171080d01682844524be57ac9883e53079b6ac66707e192ea25956","sha256:d15c61d3ecdaa582e75e2966792238c0325578ec9e0d2a1ed3183995345323d6","sha256:7c937d8a9f4f7a9761c292a6c7bcff1cc10384985282fa4fa5a04bfdb155bc90","sha256:920640105caf8a18f7db0df2638b863cbc73c2e0668d0559c5f9c93c474e8879"]}

# grep過濾沒有-f好用
[root@hqs ~]# docker inspect golang | grep Architecture
        "Architecture": "amd64",
[root@hqs ~]# docker inspect golang | grep RootFS
        "RootFS": {

5、查看鏡像的構建歷史

可以使用 docker history 來查看鏡像的構建歷史,即 DockerFile的執行過程。
每一層鏡像都相當於一個子鏡像。

# 語法
Usage:  docker history [OPTIONS] IMAGE
Show the history of an image
Options:
      --format string   Pretty-print images using a Go template
  -H, --human           Print sizes and dates in human readable format (default true)
      --no-trunc        Dont truncate output
  -q, --quiet           Only show numeric IDs

[root@hqs docker-hello]# docker history docker-hello
IMAGE               CREATED             CREATED BY   (構建操作命令)                      SIZE                COMMENT
709f66f2ceec        3 minutes ago       /bin/sh -c #(nop)  CMD ["/hello"]               0B                  
e5dd9d7e1e8f        3 minutes ago       /bin/sh -c #(nop) COPY file:437c07ddb3f63bb1…   861kB   

[root@hqs docker-hello]# docker history ubuntu      
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
2b4cba85892a        5 days ago          /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           5 days ago          /bin/sh -c #(nop) ADD file:8a50ad78a668527e9…   72.8MB        

[root@hqs docker-hello]# docker history ubuntu:14.04
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
13b66b487594        11 months ago       /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           11 months ago       /bin/sh -c mkdir -p /run/systemd && echo do…   7B                  
<missing>           11 months ago       /bin/sh -c [ -z "$(apt-get indextargets)" ]     0B                  
<missing>           11 months ago       /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   195kB               
<missing>           11 months ago       /bin/sh -c #(nop) ADD file:276b5d943a4d284f8…   196MB   

# format格式化輸出案例
[root@localhost ~]# docker image history --format {{.CreatedBy}}  ubuntu:14.04
/bin/sh -c #(nop)  CMD ["/bin/bash"]
/bin/sh -c mkdir -p /run/systemd && echo 'do…
/bin/sh -c [ -z "$(apt-get indextargets)" ]
/bin/sh -c set -xe   && echo '#!/bin/sh' > /…
/bin/sh -c #(nop) ADD file:276b5d943a4d284f8…

# -q只顯示id
[root@localhost ~]# docker image history -q  ubuntu:14.04
13b66b487594
<missing>
<missing>
<missing>
<missing>

輸出結果中 CREATED BY 列顯示的是每一層的構建操作命令。如果顯示不全,可以使用 --no-trunc 選項來顯示完整的操作命令。

[root@hqs docker-hello]# docker history ubuntu:14.04 --no-trunc

另外,執行該命令時,有時會輸出 <missing> 行,這表明響應的層在其他系統已經構建了,並且已經不可用嗎,可以忽略。

6、查找鏡像

在命令行中可以使用 docker search 命令搜索docker hub中的鏡像。

# 語法
Usage:  docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Dont truncate output

# 示例
[root@hqs docker-hello]# docker search mysql
鏡像倉庫(源)名稱                 描述                                            星數(點贊收藏)     是否官方發布         自動化 
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                            MySQL is a widely used, open-source relation…   12227               [OK]                
mariadb                          MariaDB Server is a high performing open sou…   4693                [OK]                
mysql/mysql-server               Optimized MySQL Server Docker images. Create…   907                 [OK]

# limit限制數量
[root@bruce ~]# docker search --limit 5  ubuntu 
NAME                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu               Ubuntu is a Debian-based Linux operating sys…   15650     [OK]       
websphere-liberty    WebSphere Liberty multi-architecture images …   292       [OK]       
open-liberty         Open Liberty multi-architecture images based…   58        [OK]       
neurodebian          NeuroDebian provides neuroscience research s…   98        [OK]       
ubuntu-debootstrap   DEPRECATED; use "ubuntu" instead                50        [OK]

# no-trunc不截斷顯示
[root@bruce ~]# docker search --limit 2 --no-trunc  ubuntu
NAME                DESCRIPTION                                                               STARS     OFFICIAL   AUTOMATED
ubuntu              Ubuntu is a Debian-based Linux operating system based on free software.   15650     [OK]       
websphere-liberty   WebSphere Liberty multi-architecture images based on Ubuntu 18.04         292       [OK] 

# filter篩選輸出
# 只搜索官方鏡像
[root@bruce ~]# docker search --filter "is-official=true"  ubuntu                  
NAME                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu               Ubuntu is a Debian-based Linux operating sys…   15650     [OK]       
websphere-liberty    WebSphere Liberty multi-architecture images …   292       [OK]
# 搜索指定大於多少星的鏡像
[root@bruce ~]# docker search --filter stars=10000  ubuntu
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu    Ubuntu is a Debian-based Linux operating sys…   15650     [OK] 

# format格式化輸出
# 鏡像名:.Name
# 鏡像描述:.Description
# star數量:.StarCount
# 官方鏡像:.IsOfficial
# 自動構建:.IsAutomated
[root@bruce ~]# docker search --format '{{.Name}}' ubuntu
ubuntu
websphere-liberty
open-liberty
neurodebian
[root@bruce ~]# docker search --format '{{.Name}}-{{.StarCount}}' ubuntu
ubuntu-15650
websphere-liberty-292
open-liberty-58
[root@bruce ~]# docker search --format 'table {{.Name}}\t{{.StarCount}}\t{{.IsAutomated}}\t{{.IsOfficial}}' ubuntu   
NAME                             STARS     AUTOMATED   OFFICIAL
ubuntu                           15650                 [OK]
websphere-liberty                292                   [OK]
open-liberty                     58                    [OK]
neurodebian                      98                    [OK]
ubuntu-debootstrap               50                    [OK]
ubuntu-upstart                   112                   [OK]

7、刪除本地鏡像

使用鏡像的ID、標簽、鏡像摘要標識符來指定刪除本地鏡像。
如果一個鏡像有多個標簽,則當最后一個標簽被刪除時,鏡像才真正刪除。

# docker子命令語法
Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
  -f, --force      Force removal of the image         # 強制刪除
      --no-prune   Do not delete untagged parents     # 不刪除沒有標簽的父鏡像

# docker image 子命令語法
Usage:  docker image rm [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Aliases: rm, rmi, remove
Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

# 鏡像ID標識
[root@hqs docker-hello]# docker rmi 2cc07fbb5000

# 鏡像標簽標識
[root@hqs docker-hello]# docker rmi ubuntu:14.04

# 鏡像摘要標識
[root@hqs docker-hello]# docker image rm ubuntu@sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9 

# 特別案例:鏡像摘要標識,先刪了摘要再刪了鏡像
[root@hqs ~]# docker images --digests
REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
golang              latest              sha256:c72fa9afc50b3303e8044cf28fb358b48032a548e1825819420fd40155a131cb   276895edf967        2 months ago        941MB
centos              latest              sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177   5d0da3dc9764        5 months ago        231MB
[root@hqs ~]# docker image rm golang@sha256:c72fa9afc50b3303e8044cf28fb358b48032a548e1825819420fd40155a131cb
Untagged: golang@sha256:c72fa9afc50b3303e8044cf28fb358b48032a548e1825819420fd40155a131cb
[root@hqs ~]# docker images --digests  -a
REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
golang              latest              <none>                                                                    276895edf967        2 months ago        941MB
centos              latest              sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177   5d0da3dc9764        5 months ago        231MB
[root@hqs ~]# docker image rm golang:latest
Untagged: golang:latest
Deleted: sha256:276895edf9673267f47528e8a99401f2d2947f6c9c00490f773d5ed8f559bef2
Deleted: sha256:f9925574d34663c6f0f2135512cd1e7b94490279982657a6a40fb0693ce9df41
Deleted: sha256:5ba934ce54ed16893dd8cae2c36bdcc25f9cb1a4d51dba9cbedda4b4f1bbf53f
Deleted: sha256:3a9da346a75c7c4cdacacd945f87d73b964a07007c4e5e8f9435c367176ceeb9
Deleted: sha256:cbce712ed17923285239f9d9c0528984aef065b7413d68a0290e2c8eecc98f4a
Deleted: sha256:aa56d037ee5925ebf11127c3e1f617874c4ce8bae6b6af7d132b7f7a4a606e6f
Deleted: sha256:97e5f44efb543d466c5847602654a8cb22c9466b61d04988d47ec44b197ea874
Deleted: sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce

# 強制刪除————容器還在,強制刪除鏡像
[root@bruce ~]# docker image rm --force ubuntu:latest
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
[root@bruce ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
ubuntu         14.04     13b66b487594   23 months ago   196MB
centos/httpd   latest    2cc07fbb5000   4 years ago     258MB
[root@bruce ~]# docker ps -a    
CONTAINER ID   IMAGE          COMMAND   CREATED              STATUS                          PORTS     NAMES
4357949b994d   ba6acccedd29   "bash"    About a minute ago   Exited (0) About a minute ago             xenodochial_wright

# no-prune不刪除沒有標簽的父鏡像————僅刪除latest標簽的鏡像
[root@bruce ~]# docker pull hello-world
[root@bruce ~]# docker tag feb5d9fea6a5 hello-world:version1.0
[root@bruce ~]# docker image rm --no-prune hello-world
Untagged: hello-world:latest
[root@bruce ~]# docker images
REPOSITORY     TAG          IMAGE ID       CREATED         SIZE
hello-world    version1.0   feb5d9fea6a5   17 months ago   13.3kB
ubuntu         14.04        13b66b487594   23 months ago   196MB
centos/httpd   latest       2cc07fbb5000   4 years ago     258MB
[root@bruce ~]# docker image rm --no-prune ubuntu
Error response from daemon: No such image: ubuntu:latest

8、驗證鏡像分層結構(構建鏡像)

驗證基於 Dockerfile 文件的鏡像分層。

# 1、構建項目目錄,存放Dockerfile和相關文件
[root@hqs ~]# mkdir /home/hqs/imglayers && cd /home/hqs/imglayers

# 2、建立app子目錄和py文件
[root@hqs imglayers]# mkdir app && cd app
[root@hqs app]# vi app.py
#!/usr/bin/python
print("Hello,   World!@!!!!!!")

# 3、編輯Dockerfile文件
[root@hqs app]# cd ..
[root@hqs imglayers]# vi Dockerfile
FROM ubuntu:16.04
COPY ./app /app
RUN apt-get -y update && apt-get install -y python
CMD python /app/app.py

# 4、構建鏡像
[root@hqs imglayers]# docker build -t="imglayers-test" .

# 5、查看鏡像的分層信息
[root@hqs imglayers]# docker history imglayers-test
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
9ef4ec73ba1e        8 minutes ago       /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "pyth…   0B                  
aaec11036262        8 minutes ago       /bin/sh -c apt-get -y update && apt-get inst…   64.1MB              
c53a6ee7c2be        11 minutes ago      /bin/sh -c #(nop) COPY dir:c47add2988178f681…   48B                 
b6f507652425        6 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           6 months ago        /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           6 months ago        /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
<missing>           6 months ago        /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B                
<missing>           6 months ago        /bin/sh -c #(nop) ADD file:11b425d4c08e81a3e…   135MB              

# 6、運行鏡像
[root@hqs imglayers]# docker run imglayers-test
Hello,  World!@!!!!!!

9、導出鏡像

使用docker save 將指定鏡像保存成 tar 歸檔文件。

將一個或多個鏡像保存到歸檔文件中。

# 語法
[root@localhost ~]# docker save --help
Usage:  docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
  -o, --output string   Write to a file, instead of STDOUT             # 輸出到指定文件

# 案例
[root@localhost ~]# docker save -o  ubuntu-test.tar   ubuntu:latest
[root@localhost ~]# du -sh ubuntu-test.tar 
72M	ubuntu-test.tar

10、裝載鏡像

使用docker load 從歸檔文件或標准輸入裝載鏡像。

# 語法
[root@localhost ~]# docker load --help
Usage:  docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
  -i, --input string   Read from tar archive file, instead of STDIN      # 讀取指定歸檔文件
  -q, --quiet          Suppress the load output                          # 禁止讀入輸出

# 案例
[root@localhost ~]# docker load -i registry_latest.tar 
d9ff549177a9: Loading layer  4.671MB/4.671MB
f641ef7a37ad: Loading layer  1.587MB/1.587MB
d5974ddb5a45: Loading layer  20.08MB/20.08MB
5bbc5831d696: Loading layer  3.584kB/3.584kB
73d61bf022fd: Loading layer  2.048kB/2.048kB
Loaded image: registry:latest
[root@localhost ~]# docker images
REPOSITORY            TAG              IMAGE ID       CREATED         SIZE
registry              latest           f32a97de94e1   3 years ago     25.8MB


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM