第3章 使用Docker镜像


镜像(image)是Docker三大核心概念中最为重要的,自Docker诞生之日起“镜像”就是相关社区最为热门的关键词。

Docker运行容器前需要本地存在对应的镜像。如果镜像没有保存在本地。Docker会尝试从默认镜像仓库下载(默认使用Docker Hub 公共注册服务器中的仓库),用户也可以通过配置,使用自定义镜像仓库。

本章将介绍围绕镜像这一核心概念的具体操作,包括如何使用pull命令从Docker Hub仓库中下载镜像到本地,如何查看本地已有的镜像信息和管理镜像标签,如何在远端仓库使用search命令进行搜索和过渡,如何删除镜像标签和镜像文件,如何创建用户定制的镜像并且保存为外部文件。最后,还介绍如何往Docker Hub仓库中推送自己的镜像。

ubuntu安装docker hub加速器(安装完成后重启服务)

[root@localhost ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://4eb8d41a.m.daocloud.io

Centos7安装docker hub加速器(安装完成后重启服务)

[root@localhost ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://4eb8d41a.m.daocloud.io

3.1 获取镜像

镜像是运行容器的前提。官方的Docker Hub网站已经提供了数万个镜像供大家下载。

可以使用docker pull 命令直接从Docker Hub 镜像源来下载镜像。该命令人格式为docker pull NAME[:TAG].其中,NAME是镜像仓库的名称(用来区分镜像),TAG是镜像的标签(往往用来表示版本信息)。通常情况下,描述一个镜像需要包括“名称+标签”信息。

例如,获取一个Ubuntu 14.04系统的基础镜像可以使用如下命令:

[root@localhost ~]# docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
c2c80a08aa8c: Pull complete 
6ace04d7a4a2: Pull complete 
f03114bcfb25: Pull complete 
99df43987812: Pull complete 
9c646cd4d155: Pull complete 
Digest: sha256:b92dc7814b2656da61a52a50020443223445fdc2caf1ea0c51fa38381d5608ad
Status: Downloaded newer image for ubuntu:14.04

对于Docker镜像来说,如果不显示指定TAG,则默认会选择latest标签,这会下载仓库中最新版本镜像。

下面的例子将从Docker Hub的Ubuntu仓库下载一个最新的Ubuntu操作系统的镜像。

[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
d3938036b19c: Pull complete 
a9b30c108bda: Pull complete 
67de21feec18: Pull complete 
817da545be2b: Pull complete 
d967c497ce23: Pull complete 
Digest: sha256:4a9b7d27f6e52f390ac5aa9c206ea60d2654c77f035f987c2616b15d5b010a53
Status: Downloaded newer image for ubuntu:latest

该命令实际上下载的就是ubuntu:latest镜像。

下载过程中可以看出,镜像文件一般由若干层(layer)组成,d3938036b19c这样的串是层的唯一id(实际上完整的id包括256比特,由64个十六制字符组成)。使用docker pull命令下载时会获取并输出镜像的各层信息。当不同的镜像也包括相同的层时,本地仅存储层的一份内容,减小了需要存储的空间。

读者可能会想到,在使用不同的镜像仓库服务器的情况下,可能会出现镜像重名的情况。

严格地讲,镜像的仓库名称不还就该添加仓库地址(registry,注册服务器)作为前缀,只是我们默认使用的是Docker Hub服务,该前缀可以忽略。

例如,docker pull ubuntu:14.4 命令相当于docker pull registry.hub.docker.com/ubuntu:14.04命令,即默认的注册服务器Docker Hub Registry中的ubuntu仓库来下载标记为14.04的镜像。

如果从非官方的仓库下载,则需要在仓库名称前指定完整的仓库地址。例如从网易蜂巢的镜像源来下载ubuntu:14.04

[root@localhost ~]# docker pull hub.c.163.com/public/ubuntu:14.04

pull 子命令支持的选项主要包括:

-a,--all-tags=ture|false:是否获取仓库中的所有镜像,默认为否。

下载镜像到本地后,可随时使用该镜像了,例如利用该镜像创建一个容器,在其中运行bash应用,执行ping localhost命令:

[root@localhost ~]# docker run -it ubuntu:14.04 bash
root@68c131aeb11c:/# ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.051 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.052 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.047 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.045 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.042 ms
64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.044 ms
64 bytes from localhost (127.0.0.1): icmp_seq=8 ttl=64 time=0.048 ms
64 bytes from localhost (127.0.0.1): icmp_seq=9 ttl=64 time=0.044 ms
64 bytes from localhost (127.0.0.1): icmp_seq=10 ttl=64 time=0.052 ms
64 bytes from localhost (127.0.0.1): icmp_seq=11 ttl=64 time=0.047 ms
^C
--- localhost ping statistics ---
11 packets transmitted, 11 received, 0% packet loss, time 10004ms
rtt min/avg/max/mdev = 0.042/0.048/0.065/0.010 ms
root@68c131aeb11c:/# exit
exit

3.2 查看镜像信息

1.使用images命令列镜像

使用docker images命令可以列出本地主机上已有镜像的基本信息。

例如,下面的命令列出了上一小节中下载的镜像信息:

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               c9d990395902        13 days ago         113MB
ubuntu              latest              c9d990395902        13 days ago         113MB
ubuntu              14.04               3b853789146f        13 days ago         223MB
centos              latest              e934aafc2206        2 weeks ago         199MB

在列出的信息中,可以看到以下几个字段信息。

        来自于哪个仓库,比如ubuntu仓库用来保存ubuntu系列的基础镜像;

        镜像的标签信息,比如14.04、latest用来标注不同版本信息。标签只是标记,并不能标识镜像内容;

        镜像的ID(唯一标识镜像),如ubunu:latest和ubuntu:16.04镜像的ID都是c9d99039902,说明它们目前实际上指向同一个镜像;

        创建时间,说明镜像最后的更新时间;

        镜像大小,优秀的镜像往往体积都较小。 

        其中镜像的ID信息十分重要,它唯一标识了镜像。在使用镜像ID的时候,一般可以使用该ID的前若干个字符组成的可区分串来替代完整的ID。

        TAG信息用来标记来自同一个仓库的不同镜像。例如ubuntu仓库中有多个镜像,通过TAG 信息来区分发行版本,包括10.04、12.04、12.10、13.04、14.04、等标签。

        镜像大小信息只是表示该 镜像的逻辑体积大小,实际上由于和相同的镜像层本地只会存储一份,物理上占用的存储空间小于各镜像的逻辑体积之和。

        images子命令主要支持如下选项,用户可以自行进行尝试。

            -a,--all=true|false;列出所有的镜像文件(包括临时文件),默认为否;

            --digests=ture|false:列出镜像的数字摘要值,默认为否;

            -f,--filter=[]:过滤列出的镜像,如dangling=ture只显示没有被使用的镜像;也可指定带有竹特定标注的镜像等;

            --format="TEMPLATE":控制输出格式,如.ID代表ID 信息,.Repository代表仓库信息;

            --no-trunc=true|false:对输出结果中太长的部分是否进行截断,如镜像的ID信息,默认为是;

            --q,--quit=true|false:仅输出ID信息,默认为否。

 参数

https://www.centos.bz/2017/01/docker-images-list-images/

2.使用tag命令添加镜像标签

为了方便在后续工作中使用特定镜像,还可以使用docker tag命令来为本地镜像任意添加新的标签。例如添加一个新的myubuntu:latest镜像标签:

[root@localhost ~]# docker tag ubuntu:latest myubuntu:latest

再次使用docker images列出本地主机上镜像信息,可以看到多了一个拥有myubuntu:latest标签的镜像:

[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED                  SIZE
myubuntu                      latest              c9d990395902        Less than a second ago   113MB
ubuntu                        latest              c9d990395902        Less than a second ago   113MB
ubuntu                        14.04               3b853789146f        Less than a second ago   223MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago              237MB

之后,用户就可以直接使用myubuntu:latest来表示这个镜像了。

细心的读者可能注意到,这些myubuntu:latest镜像的ID跟Ubuntu:latest完全一致。它们实际上指向同一个镜像文件,只是别名不同而已。docker tag命令添加的标签实际上起到了类似链接的作用。

3. 使用inspect命令查看详细信息

 使用docker inspect 命令可以获取该镜像的详细信息,包括制作者、适应架构、各层的数字摘要等:

[root@localhost ~]# docker inspect ubuntu:14.04
[
    {
        "Id": "sha256:3b853789146f1f249f204a7f29573263ef3486571acb72d7fd8f13056450b807",
        "RepoTags": [
            "ubuntu:14.04"
        ],
        "RepoDigests": [
            "ubuntu@sha256:b92dc7814b2656da61a52a50020443223445fdc2caf1ea0c51fa38381d5608ad"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2018-04-12T18:39:03.499982576Z",
        "Container": "2d0cd4fe487960364da0f88452adc57cc3df0360dff2b46f7776b3fca8e39544",
        "ContainerConfig": {
            "Hostname": "2d0cd4fe4879",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:ccf2cdb1a24b1744ca41cfc0ed974475a610aa269944257e06ac0604a34d5751",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "17.06.2-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:ccf2cdb1a24b1744ca41cfc0ed974475a610aa269944257e06ac0604a34d5751",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 222755204,
        "VirtualSize": 222755204,
        "GraphDriver": {
            "Data": {
                "DeviceId": "6",
                "DeviceName": "docker-253:0-1273013-36143482f9d1927630b7545ea8b2f91481213a399d1e8c63697ca432b88fa471",
                "DeviceSize": "10737418240"
            },
            "Name": "devicemapper"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:187107f7c00ed031ec24864d2c3166b8b88cfa237ca2cc93bef921327b8332b9",
                "sha256:d21679e013a956176e3c4bd7a9d5305b778b23e6c8944d70606fd948ba909452",
                "sha256:82c6dcb49cf02b04319f4d1f4978a96fe5de645489779ab95b37433820b405c2",
[root@localhost ~]# docker inspect -f {{".Architecture"}} ubuntu:14.04

"sha256:572716f657bf4d1615b9e3353771c18fefe5c8aa4a994ddfb2f5d96830bacf19", "sha256:82d75c74f1edc585d3ed6692343f975aa37ad98d0ca6e3f6d2bca22b0499097c" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } }]

返回的是一个JSON格式的消息,如果我们只要其中一项内容时,可以使用参数-f来指定,例如,获取镜像的Architechure;

[root@localhost ~]# docker inspect -f {{".Architecture"}} ubuntu:14.04            amd64

4 使用history 命令查看镜像历史

既然镜像文件由多个层组成,那么怎么知道各个层的内容具体是什么呢?这时候可以使用history子命令,该命令将列出各层的创建信息。

例如,查看ubuntu14.04镜像的创建过程,可以使用如下命令:

[root@localhost ~]# docker history  ubuntu:14.04 
\IMAGE               CREATED                  CREATED BY                                      SIZE                COMMENT
3b853789146f        Less than a second ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           Less than a second ago   /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           Less than a second ago   /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$…   2.76kB              
<missing>           Less than a second ago   /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
<missing>           Less than a second ago   /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   195kB               
<missing>           Less than a second ago   /bin/sh -c #(nop) ADD file:6eca4ad395f3f9986…   223MB    

注意过长的命令被自动截断了,可以使用前面提到的--no-trunc选项来输了完整命令。


3 搜寻镜像

搜索所有评价为10的带 nginx的镜像

[root@localhost ~]# docker search --filter=stars=10 nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        8341                [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1325                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   547                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   357                                     [OK]
kong                                                   Open-source Microservice & API Management la…   182                 [OK]                
webdevops/php-nginx                                    Nginx with PHP-FPM                              100                                     [OK]
kitematic/hello-world-nginx                            A light-weight nginx container that demonstr…   97                                      
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   50                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      48                                      [OK]
linuxserver/nginx                                      An Nginx container, brought to you by LinuxS…   34                                      
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          34                                      [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   19                                      [OK]
nginxdemos/nginx-ingress                               NGINX Ingress Controller for Kubernetes . Th…   10                                 

搜索所有自动创建的带 nginx的镜像

root@localhost ~]# docker search --filter=is-automated=true nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1325                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   547                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   357                                     [OK]
webdevops/php-nginx                                    Nginx with PHP-FPM                              100                                     [OK]
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   50                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      48                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          34                                      [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   19                                      [OK]
wodby/drupal-nginx                                     Nginx for Drupal container image                9                                       [OK]
blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          8                                       [OK]
webdevops/nginx                                        Nginx container                                 8                                       [OK]
nginxdemos/hello                                       NGINX webserver that serves a simple page co…   6                                       [OK]
1science/nginx                                         Nginx Docker images that include Consul Temp…   4                                       [OK]
pebbletech/nginx-proxy                                 nginx-proxy sets up a container running ngin…   2                                       [OK]
behance/docker-nginx                                   Provides base OS, patches and stable nginx f…   2                                       [OK]
travix/nginx                                           NGinx reverse proxy                             1                                       [OK]
toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on same…   1                                       [OK]
mailu/nginx                                            Mailu nginx frontend                            0                                       [OK]

搜索所有自动创建的评价为 10的带nginx关键字的镜像。

[root@localhost ~]# docker search --filter=is-automated=true --filter=stars=10 nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1325                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   547                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   357                                     [OK]
webdevops/php-nginx                                    Nginx with PHP-FPM                              100                                     [OK]
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   50                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      48                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          34                                      [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   19                                      [OK]

可以看到返回了很多包含关键字的镜像,其中包括镜像名字、描述、星级(表示该镜像的受欢迎程度)、是否官方创建、是否自动创建等。

默认的输出结果将按照星级评价进行排序。

3.4 删除镜像

1.使用标签删除镜像

        使用docker rmi命令可以删除镜像、命令格式为docker rmi IMAGE [IMAGE...],其中IMAGE可以为标签或ID。

        例如,要删除掉myubuntu:latest镜像,可以使用如下命令:

hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago         237MB
[root@localhost ~]# docker rmi myubuntu:latest
Untagged: myubuntu:latest
[root@localhost ~]# 

        读者可能会担心,本地的ubuntu:latest镜像是否会受此命令的影响。无需担心,当同一镜像拥有多个标签的时候,docker rim命令只是删除该镜像多个标签中的指定标签而已,并不影响镜像文件。因此上述操作相当于只是删除了镜像c9d990395902的一个标签而已。

        为了保险起见,再次查看本地的镜像,发现ubuntu:latest镜像(准确的说是c9d990395902镜像)仍然存在:

[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
ubuntu                        latest              c9d990395902        2 weeks ago         113MB
ubuntu                        14.04               3b853789146f        2 weeks ago         223MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago         237MB
    但当镜像只剩下一个标签的时候就要小心了,此时再使用docker rmi命令会彻底删除镜像。

     例如删除标签为ubuntu:14.04的镜像,由于该镜像没有额外的标签指向它,执行docker rmi 命令,可以看出它会删除这个镜像文件的所有层:

[root@localhost ~]# docker rmi ubuntu:14.04
Untagged: ubuntu:14.04
Untagged: ubuntu@sha256:b92dc7814b2656da61a52a50020443223445fdc2caf1ea0c51fa38381d5608ad
Deleted: sha256:3b853789146f1f249f204a7f29573263ef3486571acb72d7fd8f13056450b807
Deleted: sha256:a617bd7a310b090a2009887c88f91f1cd0e3c62024f05de9d080974da8cd303f
Deleted: sha256:3764e41990df0e1d74002f6e5c9216b3db80cef7cca581b215ec8d976762b0eb
Deleted: sha256:20724bef2f46162aab34c318c5de84580d2b010244d53228e5cb18b3ee545fd1
Deleted: sha256:f163978d2447e776307caaba226b83d07c8af497d9b3e59778ba6ddb663a7e8d
Deleted: sha256:187107f7c00ed031ec24864d2c3166b8b88cfa237ca2cc93bef921327b8332b9

2. 使用镜像ID删除镜像

        当使用docker rmi命令,并且后面跟上镜像ID(也可以是能进行区分的部分ID串前缀)时,会先尝试删除所有指向该镜像的标签,然后删除该镜像文件本身。

       注意,当有该镜像创建的容器存在时,镜像文件默认是无法被删除的,例如,先利用ubuntu:14.04镜像创建一个简单的容器来输出一段话:

[root@localhost ~]# docker run ubuntu:14.04 echo 'hello ! I am here!'
hello ! I am here!

        使用docker ps -a 命令可以看到本机上存在的所有容器:

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                          PORTS               NAMES
6def19445af7        ubuntu:14.04        "echo 'hello ! I am …"   About a minute ago   Exited (0) About a minute ago                       distracted_mcclintock

可以看到,后台存在一个退出状态的容器,是刚基于ubuntu:14.04镜像创建的。

试图删除该镜像,Docker会提示有容器下在运行,无法删除:

[root@localhost ~]# docker rmi ubuntu:14.04
Error response from daemon: conflict: unable to remove repository reference "ubuntu:14.04" (must force) - container 6def19445af7 is using its referenced image 8cef1fa16c77
[root@localhost ~]# 

        如果想要强行删除镜像,可以使用-f参数。

[root@localhost ~]# docker rmi -f ubuntu:14.04
Untagged: ubuntu:14.04
Untagged: ubuntu@sha256:b8855dc848e2622653ab557d1ce2f4c34218a9380cceaa51ced85c5f3c8eb201
Deleted: sha256:8cef1fa16c778d84d2261587cbe812071df064f5a33a6e67f328ca6ada1dcd71
注意:通常并不推荐使用-f参数来强制删除一个存在容器依赖的镜像。正确的做法是,先删除依赖该镜像的的有容器,再来删除镜像。首先删除容器
[root@localhost ~]# docker rm 6de

再使用ID来删除镜像,此时会正常打印出删除的各层信息:

[root@localhost ~]# docker rmi 8ce
Untagged: ubuntu:14.04
Untagged: ubuntu@sha256:b8855dc848e2622653ab557d1ce2f4c34218a9380cceaa51ced85c5f3c8eb201
Deleted: sha256:8cef1fa16c778d84d2261587cbe812071df064f5a33a6e67f328ca6ada1dcd71
Deleted: sha256:a7168b0600cd49d3d8b6dd15583ef5917d678ec1554ded9061553e473266406c
Deleted: sha256:ba3370f24d522c621c7f3aa4368876db17667d6a10bbacdba37c4ae2630e71b4
Deleted: sha256:e160be4e04261ce04c3548ad593bf443fd77d87b6d785e3c0c06273210a47bb5
Deleted: sha256:c6dcfe39ad37612ab847f4058b8faaf6707e85a481412efd3fee0a011dd9d7e0
Deleted: sha256:05b0f7f2a81723fd647744a7340477ef9619f5ddeba3f2ca039dac3dd143cd59

3.5 创建镜像

        创建镜像的方法主要有三种:基于已有镜像的内容创建、基于本地模板导入,基于Dockerfile创建。

        本李将重点介绍前两种方法。最后一种基于Dockerfile创建的方法将在后续章节专门予以详细介绍。

        1.基于已有镜像的容器创建

            该方法主要是使用docker commit命令。命令格式为docker commit [OPTIONS] CONTAINER [REPOSTORY][:TAG],主要选项包括:

            -a,--author="":作者信息;

             -c,--change=[]:提交的时候执行Dockerfile指令;

             -m,--message="":提交消息;

             -p,--pause=true:提交时暂停容器运行。

        下面将演示如何使用该命令创建一个新镜像。首先,启动一个镜像,并在其中进行修改操作,例如创建一个test文件,之后退出:

[root@localhost ~]# docker run -it ubuntu:14.04 /bin/bash
root@3c1548aa4064:/# touch test
root@3c1548aa4064:/# exit
exit

启动容器的ID为:3c1548aa4064。

此时该容器跟原ubuntu:14.04镜像相比,已经发生了改变,可以使用docker commit命令提交为一个新的镜像。提交时可以使用ID或名称来指定容器:

[root@localhost ~]# docker commit -m "Added a new file" -a "Docker tjjingpan" 3c1548aa4064 test:0.1  
sha256:13f1383c79c4e32d43bd82f76a672ade9d8cbbb9413635ff5ea8918d21dbe4f4
顺利的话,会返回新创建的镜像的 ID信息, 例如:
sha256:13f1383c79c4e32d43bd82f76a672ade9d8cbbb9413635ff5ea8918d21dbe4f4

此时查看本地镜像列表,会发出新创建的镜像已经存在了:

[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
test                          0.1                 13f1383c79c4        2 minutes ago       223MB
ubuntu                        14.04               8cef1fa16c77        4 days ago          223MB
ubuntu                        latest              c9d990395902        2 weeks ago         113MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago         237MB

2.基于本地模板导入

用户也可以直接从一个操作系统模板文件导入一个镜像,主要使用docker import命令。命令格式为docker import [OPTION] file | URL | [REPOSITORY][:TAG]。

要直接导入一个镜像,可以使用OpenVZ提供的模板来创建,或者用其他已导出的镜像模板来创建。OPENVZ模板的下载地址为:

https://download.openvz.org/template/precreated/

例如,下载了ubuntu-14.04模板压缩包,之后使用以下命令导入:

[root@localhost ~]# cat ubuntu-14.04-x86_64-minimal.tar.gz | docker import - myubuntu:14.04
sha256:315c4f14aaaaf010067aa9cc88459e34b59899079c1fd4d58ac1078e136f5e88

然后查看新导入的镜像,会发现它已经在本地存在了:

[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
myubuntu                      14.04               315c4f14aaaa        About an hour ago   215MB
test                          0.1                 13f1383c79c4        About an hour ago   223MB
ubuntu                        14.04               8cef1fa16c77        4 days ago          223MB
ubuntu                        latest              c9d990395902        2 weeks ago         113MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago         237MB

3.6 存出和载入镜像

        用户可以使用docker save 和docker load命令存出和载入镜像。

    1.存出镜像

        如果要导出镜像到本地文件,可以使用docker dave命令。例如,导出本地的ubuntu14.04镜像为文件ubuntu_14.04.tar,如下所示:

[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
myubuntu                      14.04               315c4f14aaaa        About an hour ago   215MB
test                          0.1                 13f1383c79c4        About an hour ago   223MB
ubuntu                        14.04               8cef1fa16c77        4 days ago          223MB
ubuntu                        latest              c9d990395902        2 weeks ago         113MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago         237MB
[root@localhost ~]# docker save -o ubuntu_14.04.tar ubuntu:14.04
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  ubuntu_14.04.tar  ubuntu-14.04-x86_64-minimal.tar.gz

        之后,用户就可以通过复制ubuntu_14.04.tar文件将该该镜像分享他人。

2.载入镜像

        可以使用docker load将导出的tar文件再导入到本地镜像库,例如从文件ubuntu_14.04.tar导入镜像到本地镜像列表,如下所示:

[root@localhost ~]# docker load --input ubuntu_14.04.tar 
Loaded image: ubuntu:14.04
[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
test                          0.1                 13f1383c79c4        About an hour ago   223MB
ubuntu                        14.04               8cef1fa16c77        4 days ago          223MB
ubuntu                        latest              c9d990395902        2 weeks ago         113MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago         237MB

或:

[root@localhost ~]# docker load < ubuntu_14.04.tar 
05b0f7f2a817: Loading layer [==================================================>]  231.8MB/231.8MB
0c3819952093: Loading layer [==================================================>]  209.9kB/209.9kB
14fa4a9494bf: Loading layer [==================================================>]  15.36kB/15.36kB
b33859b66bfd: Loading layer [==================================================>]  5.632kB/5.632kB
4622c8e1bdc0: Loading layer [==================================================>]  3.072kB/3.072kB
Loaded image: ubuntu:14.04
[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
ubuntu                        14.04               8cef1fa16c77        4 days ago          223MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        2 years ago         237MB

这将导入镜像及其相关的元数据信息(包括标签等)。导入成功后,可以使用docker images命令进行查看。

3.7 上传镜像

    可以使用docker push命令上传镜像到仓库,默认上传到Docker Hub官方仓库(需要登录)。命令格式为:

docker push NAME[:TAG] | [REGISTRY_HOST][:REGISTRY_PORT]/[NAME][:TAG]

用户在Docker Hub网站注册后可以上传自制的镜像。例如用户user上传本地的test:latest镜像,可以先添加新的标签user/test:laatest,然后用docker push命令上传镜像。

3.8 本章小结

    本章具体介绍了围绕Docker镜像的一系列重要命令操作,包括获取、查看、搜索、删除、创建、存出和载入、上传等。

    镜像是使用Docker的前提,也是最基本的资源。所以平时的Docker使用中,要注意积累自己定制的镜像文件,并将自己创建的高质量镜像分享到社区中。

    在后续的章节中,笔者将介绍更多的对镜像进行操作的场景。

猜你喜欢

转载自blog.csdn.net/tjjingpan/article/details/80081647
今日推荐