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
posted @ 2022-03-08 15:40  休耕  阅读(2811)  评论(0编辑  收藏  举报