专注于Docker、Container等技术的交流和分享
挖掘最有价值的资讯动态、教程、国内外优秀资源

从Docker零基础到懂一点实践教程(四)

Docker镜像与仓库(一)

查看和删除镜像

Docker镜像是一种使用联合加载技术实现的层叠的只读文件系统,它是容器构建的基石。Docker的镜像存储在/var/lib/docker目录下。通过docker info命令可以查看到Docker使用的存储驱动和存储的位置。

schen@scvmu01:~$ docker info
Containers: 7
 Running: 0
 Paused: 0
 Stopped: 7
Images: 1
Server Version: 1.12.1
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 19
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: null overlay host bridge
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: apparmor seccomp
Kernel Version: 4.4.0-36-generic
Operating System: Ubuntu 16.04 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 992.4 MiB
Name: scvmu01.shichen.org
ID: VVPW:WOIW:TCPI:YDUG:J67N:5DHU:WSWW:RMMN:64FK:KNHO:NPPH:ATSL
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Insecure Registries:
 127.0.0.0/8
schen@scvmu01:~$ 
schen@scvmu01:~$ sudo ls -la /var/lib/docker/aufs/
total 20
drwx------  5 root root 4096 Aug 30 22:27 .
drwx--x--x 10 root root 4096 Aug 30 22:27 ..
drwx------ 21 root root 4096 Sep  1 22:06 diff
drwx------  2 root root 4096 Sep  1 22:06 layers
drwx------ 21 root root 4096 Sep  1 22:06 mnt
schen@scvmu01:~$ 
schen@scvmu01:~$ sudo ls -la /var/lib/docker/aufs/mnt
total 84
drwx------ 21 root root 4096 Sep  1 22:06 .
drwx------  5 root root 4096 Aug 30 22:27 ..
drwxr-xr-x  2 root root 4096 Aug 31 20:40 1e15fc06bd2fd1e73b4a005862893281844b591c65e481d53ca5b1450d148b18
drwxr-xr-x  2 root root 4096 Aug 31 20:40 1e15fc06bd2fd1e73b4a005862893281844b591c65e481d53ca5b1450d148b18-init
drwxr-xr-x  2 root root 4096 Aug 30 22:42 1eb964a1fcf933a53166d0dc6686e94f702c2ebdb01f7e04c770696b288e7060
drwxr-xr-x  2 root root 4096 Sep  1 22:06 23e023a65314bba94bebc2b2959ffae289cd2059f8e58e4cf28aad5affedbf0c
drwxr-xr-x  2 root root 4096 Sep  1 22:06 23e023a65314bba94bebc2b2959ffae289cd2059f8e58e4cf28aad5affedbf0c-init
drwxr-xr-x  2 root root 4096 Aug 31 21:37 3b23d661ca8979d6da5dfc5a5c87eb3aacfb43e53ae524aed8c79741b25da5e8
drwxr-xr-x  2 root root 4096 Aug 31 21:37 3b23d661ca8979d6da5dfc5a5c87eb3aacfb43e53ae524aed8c79741b25da5e8-init
drwxr-xr-x  2 root root 4096 Aug 31 22:08 8e3311ac85799cb4139c59ab2ae80c0b2bc6c27d351cd362a67749933ad4f349
drwxr-xr-x  2 root root 4096 Aug 31 22:08 8e3311ac85799cb4139c59ab2ae80c0b2bc6c27d351cd362a67749933ad4f349-init
drwxr-xr-x  2 root root 4096 Aug 30 22:42 901e24caec5dfdf532a51b4f59664b6819df0fcc388477818e7fe4656d4704a0
drwxr-xr-x  2 root root 4096 Aug 31 22:09 91275f7560238e26c4cf6ed7a09ec4574dc28edef764a38634a1b6e0d6f847fe
drwxr-xr-x  2 root root 4096 Aug 31 22:09 91275f7560238e26c4cf6ed7a09ec4574dc28edef764a38634a1b6e0d6f847fe-init
drwxr-xr-x  2 root root 4096 Aug 30 22:42 a10221226a162e020ae141a4da3a839af8b6cff8035d12531560e7ffeaad8022
drwxr-xr-x  2 root root 4096 Aug 31 20:37 aaedd0f09104e8bfcb757b35f73e17dc37969b37add4e36b30fc2abc5f6e4e04
drwxr-xr-x  2 root root 4096 Aug 31 20:37 aaedd0f09104e8bfcb757b35f73e17dc37969b37add4e36b30fc2abc5f6e4e04-init
drwxr-xr-x  2 root root 4096 Aug 30 22:42 d21b30190c94efbf4cfbaafdabe01c289fc77b3131d141d5a9bcc28d750d8b79
drwxr-xr-x  2 root root 4096 Aug 30 22:42 ed2f2039f8fdbf2fa8eff0f745c7b532be5519e8ee41533d214bddc4b94bfc19
drwxr-xr-x  2 root root 4096 Aug 31 22:07 f826245b6b5a4ad52e0e041ba293dce74435c80e0399bb48dc3aa63b3c03b856
drwxr-xr-x  2 root root 4096 Aug 31 22:07 f826245b6b5a4ad52e0e041ba293dce74435c80e0399bb48dc3aa63b3c03b856-init
schen@scvmu01:~$ 

列出镜像

$ docker images [OPTIONS] [REPOSITORY[:TAG]]

我们可以使用docker images命令查看镜像列表。-a选项帮我们列出所有的镜像,包括那些在默认情况下不显示的中间层镜像,-f选项可以为我们提供过滤条件,-q选项只显示镜像的唯一ID,而使用--no-trunc选项可以让命令为我们输出没有截断的镜像ID。

schen@scvmu01:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               bd3d4369aebc        6 days ago          126.6 MB
ubuntu              latest              bd3d4369aebc        6 days ago          126.6 MB
ubuntu              xenial              bd3d4369aebc        6 days ago          126.6 MB
ubuntu              14.04               4a725d3b3b1c        6 days ago          188 MB
ubuntu              trusty              4a725d3b3b1c        6 days ago          188 MB
centos              centos7             970633036444        4 weeks ago         196.7 MB
centos              latest              970633036444        4 weeks ago         196.7 MB
schen@scvmu01:~$ 
schen@scvmu01:~$ docker images -q
bd3d4369aebc
bd3d4369aebc
bd3d4369aebc
4a725d3b3b1c
4a725d3b3b1c
970633036444
970633036444
schen@scvmu01:~$ 
schen@scvmu01:~$ docker images -q --no-trunc
sha256:bd3d4369aebc4945be269859df0e15b1d32fefa2645f5699037d7d8c6b415a10
sha256:bd3d4369aebc4945be269859df0e15b1d32fefa2645f5699037d7d8c6b415a10
sha256:bd3d4369aebc4945be269859df0e15b1d32fefa2645f5699037d7d8c6b415a10
sha256:4a725d3b3b1cc18c8cbd05358ffbbfedfe1eb947f58061e5858f08e2899731ee
sha256:4a725d3b3b1cc18c8cbd05358ffbbfedfe1eb947f58061e5858f08e2899731ee
sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c
sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c
schen@scvmu01:~$ 

镜像标签和仓库

在刚才的docker images命令输出结果中,我们看到两个比较关键的概念:仓库和标签。

这里的仓库(“REPOSITORY”)指的是包含一系列关联的镜像的集合,例如“ubuntu”就是一个很大的仓库,它里面的镜像对应了Ubuntu的不同版本。这与之前介绍过的Docker组件中的仓库(“REGISTRY”)有着本质的区别,后者提供的是Docker镜像的存储服务。而在组织存储服务时,一个“REGISTRY”通常包含多个“REPOSITORY”,而一个“REPOSITORY”通常又包含多个“IMAGE”。

在仓库中不同的镜像是以标签进行区分的,而一个仓库名加上一个标签名,就构成了一个完整的镜像名,而这个镜像名则对应了一个镜像以及它的唯一ID。还记得使用docker run命令时,我们并没有指定镜像ID,而是直接使用了仓库名“ubuntu”,这是因为Docker会使用“latest”作为默认的标签名查找镜像。

在仓库中,你可能会注意到多个标签对应同一个镜像ID的情况,这是因为我们可以根据不同的需求,为同一个镜像打上不同的标签。例如“ubuntu:trusty”和“ubuntu:14.04”实际上指的是同一个镜像。

查看镜像

$ docker inspect [OPTIONS] CONTAINER|IMAGE|TASK [CONTAINER|IMAGE|TASK...]

我们不仅可以使用docker inspect命令查看容器的详细信息,还可以用它来查看镜像和任务的详细信息。

schen@scvmu01:~$ docker inspect ubuntu:latest
[
    {
        "Id": "sha256:bd3d4369aebc4945be269859df0e15b1d32fefa2645f5699037d7d8c6b415a10",
        "RepoTags": [
            "ubuntu:latest"
        ],
        "RepoDigests": [
            "ubuntu@sha256:f4691c96e6bbaa99d99ebafd9af1b68ace2aa2128ae95a60369c506dd6e6f6ab"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2016-08-26T18:50:27.629952966Z",
        "Container": "1ff47ae2a4ea4e3ca166eb799d9e4a59c953787577dc2e892b19b544ad290727",
        "ContainerConfig": {
            "Hostname": "3934ed318998",
            "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:16f57b8272173310ae88c29b8dcc337624e1d00054d0279309eccb6d40793766",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "1.10.3",
        "Author": "",
        "Config": {
            "Hostname": "3934ed318998",
            "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:16f57b8272173310ae88c29b8dcc337624e1d00054d0279309eccb6d40793766",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 126634686,
        "VirtualSize": 126634686,
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:c8a75145fcc4e1a66cd86b3cbbe14da1a37894129005e461a43875a094b93412",
                "sha256:c6f2b330b60c7c32642c47871b28aab110a7214ed6aac305dd03f70b95cdc610",
                "sha256:055757a19384c8afff0e79db7bb84fd481d3a9565d78962c7f368d5ac5984998",
                "sha256:48373480614b79e5c1b0a080807fa8ffaea12695f548406ea77feb5074e195e3",
                "sha256:0cad5e07ba339f87eb58850252a0ad00e104bae4cfc66b376265e16c32a0aae9"
            ]
        }
    }
]
schen@scvmu01:~$ 

删除镜像

$ docker rmi [OPTIONS] IMAGE [IMAGE...]

我们可以使用docker rmi命令来删除一个镜像,-f选项用来强制删除,而--no-prune选项会保留被删除镜像中未打标签的父镜像。

schen@scvmu01:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               bd3d4369aebc        6 days ago          126.6 MB
ubuntu              latest              bd3d4369aebc        6 days ago          126.6 MB
ubuntu              xenial              bd3d4369aebc        6 days ago          126.6 MB
ubuntu              14.04               4a725d3b3b1c        6 days ago          188 MB
ubuntu              trusty              4a725d3b3b1c        6 days ago          188 MB
centos              centos7             970633036444        4 weeks ago         196.7 MB
centos              latest              970633036444        4 weeks ago         196.7 MB
schen@scvmu01:~$ 
schen@scvmu01:~$ docker rmi ubuntu:16.04
Untagged: ubuntu:16.04
schen@scvmu01:~$ 
schen@scvmu01:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              bd3d4369aebc        6 days ago          126.6 MB
ubuntu              xenial              bd3d4369aebc        6 days ago          126.6 MB
ubuntu              14.04               4a725d3b3b1c        6 days ago          188 MB
ubuntu              trusty              4a725d3b3b1c        6 days ago          188 MB
centos              centos7             970633036444        4 weeks ago         196.7 MB
centos              latest              970633036444        4 weeks ago         196.7 MB
schen@scvmu01:~$ 
schen@scvmu01:~$ docker rmi 4a725d3b3b1c
Error response from daemon: conflict: unable to delete 4a725d3b3b1c (must be forced) - image is referenced in one or more repositories
schen@scvmu01:~$ 
schen@scvmu01:~$ docker rmi -f 4a725d3b3b1c
Untagged: ubuntu:14.04
Untagged: ubuntu:trusty
Untagged: ubuntu@sha256:5b5d48912298181c3c80086e7d3982029b288678fccabf2265899199c24d7f89
Deleted: sha256:4a725d3b3b1cc18c8cbd05358ffbbfedfe1eb947f58061e5858f08e2899731ee
schen@scvmu01:~$ 
schen@scvmu01:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              bd3d4369aebc        6 days ago          126.6 MB
ubuntu              xenial              bd3d4369aebc        6 days ago          126.6 MB
centos              centos7             970633036444        4 weeks ago         196.7 MB
centos              latest              970633036444        4 weeks ago         196.7 MB
schen@scvmu01:~$ 
schen@scvmu01:~$ docker rmi -f $(docker images -q centos | sort -u)
Untagged: centos:centos7
Untagged: centos:latest
Untagged: centos@sha256:a66ffcb73930584413de83311ca11a4cb4938c9b2521d331026dad970c19adf4
Deleted: sha256:97063303644439d9cea259b0e5f4b468633c90d88bf526acc67e5ae0a6e9427c
schen@scvmu01:~$ 
schen@scvmu01:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              bd3d4369aebc        6 days ago          126.6 MB
ubuntu              xenial              bd3d4369aebc        6 days ago          126.6 MB
schen@scvmu01:~$ 

执行命令docker rmi ubuntu:16.04返回Untagged: ubuntu:16.04说明删除了标签,但并没有删除镜像文件,这是因为还有标签指向这个镜像。我们可以依次删除某个镜像文件所有的标签,这样当最后一个标签被删除的时候镜像文件也会随之删除,当然还有个简单的方法,那就是像docker rmi -f 4a725d3b3b1c这样直接指定镜像ID。另外,docker rmi命令并不支持直接删除某一个仓库的所有镜像,但我们可以变通一下,借助docker images -q命令来完成这个任务。

获取和推送镜像

查找镜像

大致来说查找镜像有两种方法:
1. 通过“Docker Hub”网站查找镜像;
2. 通过命令行查找镜像;

通过“Docker Hub”网站查找镜像

要通过“Docker Hub”网站查找镜像,首先要注册成为docker.com会员,注册过程十分简单,只需要验证邮箱即可。注册的账号可以用来查询、上传和分享镜像,以及享受docker.com提供的其他服务。“Docker Hub”网站的网址如下:

https://hub.docker.com

我们使用自己的账户登录网站后,直接在最上面的“Search”文本框内输入想要查找的REPOSITORY仓库名并点击回车即可。网站为我们提供了简单的过滤器,其中Offical为官方镜像,Automated为通过自动化构建的镜像,而DownloadsStars分别是以下载量和评分数进行排序。

例如,搜索并点击名为“ubuntu”的官方仓库,我们就会来到这个仓库的详情页面,在“Repo Info”选项卡中我们可以看到一些必要的介绍,在“Tags”选项卡中我们可以浏览到这个仓库的全部标签,每个标签都对应一个镜像。

通过命令行查找镜像

$ docker search [OPTIONS] TERM

接下来我们看如何通过命令行查找镜像,这里用到的就是docker search命令,--automated选项可以让命令只返回通过自动化构建的镜像,--no-trunc选项可以让命令返回未被截断的输出结果,而-s选项可以指定返回结果的最低评分数。另外,使用docker search命令最多返回25条结果。

schen@scvmu01:~$ docker search ubuntu
NAME                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                     Ubuntu is a Debian-based Linux operating s...   4606      [OK]       
ubuntu-upstart             Upstart is an event-based replacement for ...   66        [OK]       
rastasheep/ubuntu-sshd     Dockerized SSH service, built on top of of...   36                   [OK]
torusware/speedus-ubuntu   Always updated official Ubuntu docker imag...   27                   [OK]
ubuntu-debootstrap         debootstrap --variant=minbase --components...   25        [OK]       
nuagebec/ubuntu            Simple always updated Ubuntu docker images...   8                    [OK]
nickistre/ubuntu-lamp      LAMP server on Ubuntu                           8                    [OK]
nimmis/ubuntu              This is a docker images different LTS vers...   5                    [OK]
maxexcloo/ubuntu           Docker base image built on Ubuntu with Sup...   2                    [OK]
jordi/ubuntu               Ubuntu Base Image                               1                    [OK]
darksheer/ubuntu           Base Ubuntu Image -- Updated hourly             1                    [OK]
admiringworm/ubuntu        Base ubuntu images based on the official u...   1                    [OK]
datenbetrieb/ubuntu        custom flavor of the official ubuntu base ...   0                    [OK]
lynxtp/ubuntu              https://github.com/lynxtp/docker-ubuntu         0                    [OK]
smartentry/ubuntu          ubuntu with smartentry                          0                    [OK]
webhippie/ubuntu           Docker images for ubuntu                        0                    [OK]
croscon/ubuntu             Crosconized Ubuntu                              0                    [OK]
life360/ubuntu             Ubuntu is a Debian-based Linux operating s...   0                    [OK]
esycat/ubuntu              Ubuntu LTS                                      0                    [OK]
teamrock/ubuntu            TeamRock's Ubuntu image configured with AW...   0                    [OK]
konstruktoid/ubuntu        Ubuntu base image                               0                    [OK]
widerplan/ubuntu           Our basic Ubuntu images.                        0                    [OK]
ustclug/ubuntu             ubuntu image for docker with USTC mirror        0                    [OK]
dorapro/ubuntu             ubuntu image                                    0                    [OK]
uvatbc/ubuntu              Ubuntu images with unprivileged user            0                    [OK]
schen@scvmu01:~$ 
schen@scvmu01:~$ docker search -s 3 ubuntu
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                     Ubuntu is a Debian-based Linux operating s...   4606      [OK]       
ubuntu-upstart             Upstart is an event-based replacement for ...   66        [OK]       
rastasheep/ubuntu-sshd     Dockerized SSH service, built on top of of...   36                   [OK]
torusware/speedus-ubuntu   Always updated official Ubuntu docker imag...   27                   [OK]
ubuntu-debootstrap         debootstrap --variant=minbase --components...   25        [OK]       
nuagebec/ubuntu            Simple always updated Ubuntu docker images...   8                    [OK]
nickistre/ubuntu-lamp      LAMP server on Ubuntu                           8                    [OK]
nimmis/ubuntu              This is a docker images different LTS vers...   5                    [OK]
schen@scvmu01:~$ 
schen@scvmu01:~$ docker search --automated -s 3 ubuntu
Flag --automated has been deprecated, use --filter=automated=true instead
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
rastasheep/ubuntu-sshd     Dockerized SSH service, built on top of of...   36                   [OK]
torusware/speedus-ubuntu   Always updated official Ubuntu docker imag...   27                   [OK]
nuagebec/ubuntu            Simple always updated Ubuntu docker images...   8                    [OK]
nickistre/ubuntu-lamp      LAMP server on Ubuntu                           8                    [OK]
nimmis/ubuntu              This is a docker images different LTS vers...   5                    [OK]
schen@scvmu01:~$ 
schen@scvmu01:~$ docker search --automated -s 3 --no-trunc ubuntu
Flag --automated has been deprecated, use --filter=automated=true instead
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                       DESCRIPTION                                                                                       STARS     OFFICIAL   AUTOMATED
rastasheep/ubuntu-sshd     Dockerized SSH service, built on top of official Ubuntu images.                                   36                   [OK]
torusware/speedus-ubuntu   Always updated official Ubuntu docker image with Torusware Speedus acceleration software          27                   [OK]
nuagebec/ubuntu            Simple always updated Ubuntu docker images with SSH access and supervisord.                       8                    [OK]
nickistre/ubuntu-lamp      LAMP server on Ubuntu                                                                             8                    [OK]
nimmis/ubuntu              This is a docker images different LTS version of Ubuntu with a working init process and syslog    5                    [OK]
schen@scvmu01:~$ 

不难发现,所有非官方仓库的名称都是由“/”分割的两部分组成,第一部分为仓库提供者的docker.com用户名,第二部分为仓库的实际名称,这两部分共同构成了完整的仓库名,使用的时候应该把它看做一个整体。

拉取镜像

$ docker pull [OPTIONS] NAME[:TAG|@DIGEST]

当我们找到合适的镜像并把它下载到本地时,就需要用到docker pull命令,当指定-a选项时命令将会把所匹配到的镜像仓库中所有的镜像文件都下载下来。

schen@scvmu01:~$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              bd3d4369aebc        7 days ago          126.6 MB
ubuntu              xenial              bd3d4369aebc        7 days ago          126.6 MB
centos              centos7             970633036444        5 weeks ago         196.7 MB
schen@scvmu01:~$ 
schen@scvmu01:~$ docker pull ubuntu:12.04
12.04: Pulling from library/ubuntu
4bae8cb7faf8: Pull complete 
6fdbc48ff694: Pull complete 
76b89518fcb8: Pull complete 
e24f10461f80: Pull complete 
7f76cfd9cb68: Pull complete 
Digest: sha256:84c52bdd388c686ed2c72804acbb6710cc103c959cfbfee80d1c857532c502c2
Status: Downloaded newer image for ubuntu:12.04
schen@scvmu01:~$ 
schen@scvmu01:~$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              bd3d4369aebc        7 days ago          126.6 MB
ubuntu              xenial              bd3d4369aebc        7 days ago          126.6 MB
ubuntu              12.04               a11493a01736        7 days ago          103.6 MB
centos              centos7             970633036444        5 weeks ago         196.7 MB
schen@scvmu01:~$ 

这里有一个小技巧,因为docker.com的官方服务器架设在国外,所以在国内的访问有可能会很慢,我们可以通过为Docker添加镜像站点的方法解决这个问题。目前国内有很多家提供镜像加速服务的网站,大多数都需要注册账户后才能使用。注册的方法很简单,步骤也大同小异,这里就不再赘述了。

拿到镜像站点的URL后,我们需要将它添加到docker的配置文件中,编辑/etc/default/docker文件,并向DOCKER_OPTS参数添加--registry-mirror字段。最后,我们需要重启docker守护进程令更改生效。

schen@scvmu01:~$ sudo vi /etc/default/docker
schen@scvmu01:~$ 
schen@scvmu01:~$ grep DOCKER_OPTS /etc/default/docker
# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
DOCKER_OPTS="--registry-mirror=https://username.mirror.website.com"
schen@scvmu01:~$ 
schen@scvmu01:~$ sudo service docker restart
schen@scvmu01:~$ 

推送镜像

我们不仅可以从“Docker Hub”上下载镜像,还可以将我们的镜像推送到“Docker Hub”上去,这就需要用到docker push命令,它的完整格式如下:

$ docker push [OPTIONS] NAME[:TAG]

首先,我们查看一下本地已经存在的镜像:

schen@scvmu01:~$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
shichen/build_test    latest              38555e155ef6        6 weeks ago         222.4 MB
shichen/nginx         latest              38555e155ef6        6 weeks ago         222.4 MB
shichen/commit_test   latest              7f6c25158375        6 weeks ago         222.4 MB
ubuntu                16.04               bd3d4369aebc        7 weeks ago         126.6 MB
ubuntu                latest              bd3d4369aebc        7 weeks ago         126.6 MB
ubuntu                xenial              bd3d4369aebc        7 weeks ago         126.6 MB
ubuntu                12.04               a11493a01736        7 weeks ago         103.6 MB
centos                centos7             970633036444        11 weeks ago        196.7 MB
schen@scvmu01:~$ 

我决定把“shichen/nginx”镜像推送到“Docker Hub”上去,但是遇到了“unauthorized: authentication required”的问题:

schen@scvmu01:~$ docker push shichen/nginx
The push refers to a repository [docker.io/shichen/nginx]
08fe14740277: Preparing 
d17b6212b3e7: Preparing 
0cad5e07ba33: Preparing 
48373480614b: Preparing 
055757a19384: Preparing 
c6f2b330b60c: Waiting 
c8a75145fcc4: Waiting 
unauthorized: authentication required
schen@scvmu01:~$ 

这是因为我还没有登录自己的账户,因此使用docker login命令登录之后,就可以顺利推送镜像了:

schen@scvmu01:~$ docker login --username=shichen
Password: 
Login Succeeded
schen@scvmu01:~$ docker push shichen/nginx
The push refers to a repository [docker.io/shichen/nginx]
08fe14740277: Pushed 
d17b6212b3e7: Pushed 
0cad5e07ba33: Pushed 
48373480614b: Pushed 
055757a19384: Pushed 
c6f2b330b60c: Pushed 
c8a75145fcc4: Pushed 
latest: digest: sha256:f33ea62b40cd88d86e16d241e114a8a20b06bc5058c62513c4a63ee3b56bc4c2 size: 1781
schen@scvmu01:~$ 

Docker并不会把所有的镜像都传上去,而只会提交修改的部分。

作为验证,我们可以在“Docker Hub”网站上,访问到这个镜像。当然,我们也可以通过docker search命令找到它:

schen@scvmu01:~$ docker search shichen/nginx
NAME            DESCRIPTION   STARS     OFFICIAL   AUTOMATED
shichen/nginx                 0          

构建镜像

构建镜像是Docker中的重要操作,它可以:
– 保存我们对容器的修改,让我们可以在别的地方再次使用它;
– 根据构建方法的不同,它还为我们提供了自定义镜像的能力;
– 构件镜像是以软件的形式为我们打包并分发服务及其运行环境;

Docker中构建镜像有两种方法,一种是使用docker commit命令通过容器构建,另一种是通过docker build命令从Dockerfile构建。

通过commit构建镜像

$ docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

我们可以通过docker commit命令通过容器构建镜像,使用-a选项指定镜像的作者,使用-m选项指定镜像构建的信息,而-p选项可以控制构建镜像过程中是否暂停容器的运行。

首先,我们启动一个交互式容器,并将其命名为commit_test,使用apt-get命令为其安装Nginx服务。

schen@scvmu01:~$ docker run -it -p 80 --name commit_test ubuntu /bin/bash
root@1a1544b45688:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [95.7 kB]
......                             
Reading package lists... Done
root@1a1544b45688:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree       
......
Processing triggers for systemd (229-4ubuntu7) ...
root@1a1544b45688:/# exit
exit
schen@scvmu01:~$ 
schen@scvmu01:~$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
1a1544b45688        ubuntu              "/bin/bash"         12 minutes ago      Exited (0) 3 seconds ago                       commit_test
schen@scvmu01:~$ 

接下来我们使用docker commit命令,将这个容器提交为镜像。

schen@scvmu01:~$ docker commit -a "Shichen <shichen@website.com>" -m "Nginx in docker" commit_test shichen/commit_test
sha256:7f6c25158375ca6d5f8e9007f866692be2b5ff69603d4c45c76c1834a849979b
schen@scvmu01:~$ 
schen@scvmu01:~$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
shichen/commit_test   latest              7f6c25158375        6 seconds ago       222.4 MB
ubuntu                latest              bd3d4369aebc        8 days ago          126.6 MB
ubuntu                xenial              bd3d4369aebc        8 days ago          126.6 MB
ubuntu                12.04               a11493a01736        8 days ago          103.6 MB
centos                centos7             970633036444        5 weeks ago         196.7 MB
schen@scvmu01:~$ 

下面我们就可以用这个新生成的镜像创建容器了,为了对外提供服务,我们创建一个带有端口映射的守护式容器。

schen@scvmu01:~$ docker run -d --name nginx_web -p 80 shichen/commit_test nginx -g "daemon off;"
cecd6e6a40cb776836d0a3c74e83730d25854f72dcb56e54e0905a5825113e26
schen@scvmu01:~$ 
schen@scvmu01:~$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                   NAMES
cecd6e6a40cb        shichen/commit_test   "nginx -g 'daemon off"   7 seconds ago       Up 5 seconds        0.0.0.0:32769->80/tcp   nginx_web
schen@scvmu01:~$ 
schen@scvmu01:~$ curl http://127.0.0.1:32769
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
schen@scvmu01:~$ 

通过Dockerfile构建镜像

要通过Dockerfile构建镜像,我们就需要了解如何编写Dockerfile,有了Dockerfile我们就可以通过docker build命令构建出我们想要的镜像。而实际上Dockerfile就是包含了一系列命令的文本文件,为了构建出跟上节相同的镜像,我们大致需要编写如下的Dockerfile:

# First Dockerfile
FROM ubuntu:16.04
MAINTAINER Shichen "shichen@website.com"
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80

其中FROM语句指定了用于构建这个镜像的基础镜像,MAINTAINER语句指定了维护者的信息,它与docker commit命令中-a参数提供了相同的信息,RUN语句指定了构建镜像时需要运行的命令,而EXPOSE语句则指定了需要被映射的端口。

有了Dockerfile我们就可以通过docker build命令构建镜像了,其中-t选项指定了镜像的名字,该命令的完整格式如下:

$ docker build [OPTIONS] PATH | URL | -

schen@scvmu01:~$ mkdir -p ./dockerfile/build_test
schen@scvmu01:~$ 
schen@scvmu01:~$ cd ./dockerfile/build_test
schen@scvmu01:~/dockerfile/build_test$ 
schen@scvmu01:~/dockerfile/build_test$ vi Dockerfile
schen@scvmu01:~/dockerfile/build_test$ 
schen@scvmu01:~/dockerfile/build_test$ cat Dockerfile 
# First Dockerfile
FROM ubuntu:16.04
MAINTAINER Shichen "shichen@website.com"
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
schen@scvmu01:~/dockerfile/build_test$ 
schen@scvmu01:~/dockerfile/build_test$ docker build -t "shichen/build_test" ./
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:16.04
16.04: Pulling from library/ubuntu
Digest: sha256:f4691c96e6bbaa99d99ebafd9af1b68ace2aa2128ae95a60369c506dd6e6f6ab
Status: Downloaded newer image for ubuntu:16.04
 ---> bd3d4369aebc
Step 2 : MAINTAINER Shichen "shichen@website.com"
 ---> Running in 1644f43e3e80
 ---> b8051df248d3
Removing intermediate container 1644f43e3e80
Step 3 : RUN apt-get update
 ---> Running in e6eb4b2b6513
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [95.7 kB]
......
Reading package lists...
 ---> ed4c4835e691
Removing intermediate container e6eb4b2b6513
Step 4 : RUN apt-get install -y nginx
 ---> Running in 5acdefd0f83e
Reading package lists...
Building dependency tree...
......
Processing triggers for systemd (229-4ubuntu7) ...
 ---> 989af64bca25
Removing intermediate container 5acdefd0f83e
Step 5 : EXPOSE 80
 ---> Running in 6f4ecedf360b
 ---> 38555e155ef6
Removing intermediate container 6f4ecedf360b
Successfully built 38555e155ef6
schen@scvmu01:~/dockerfile/build_test$ 

我们不难发现,在每一步构建指令执行完毕后,都会生成一个ID,这就是我们所说的中间层镜像ID。如果我们在构建镜像的命令中指定了-q选项,那么Docker就会省略这些信息,而直接返回最终的镜像ID。

下面我们就通过这个镜像来运行一个容器:

schen@scvmu01:~/dockerfile/build_test$ docker run -d --name nginx_web2 -p 80 shichen/build_test nginx -g "daemon off;"
626ffb38f4cdef2474aa9548793af609dc823369109b51026abec5a21459d33c
schen@scvmu01:~/dockerfile/build_test$ 
schen@scvmu01:~/dockerfile/build_test$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                   NAMES
626ffb38f4cd        shichen/build_test    "nginx -g 'daemon off"   14 seconds ago      Up 12 seconds       0.0.0.0:32770->80/tcp   nginx_web2
cecd6e6a40cb        shichen/commit_test   "nginx -g 'daemon off"   About an hour ago   Up About an hour    0.0.0.0:32769->80/tcp   nginx_web
schen@scvmu01:~/dockerfile/build_test$ 
schen@scvmu01:~/dockerfile/build_test$ curl http://127.0.0.1:32770
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
schen@scvmu01:~/dockerfile/build_test$ 

使用Dockerfile完全可以通过写好的文件自动化地来运行,这也是Dockerfile最大的优势,因此它成为了Docker中构建镜像最主要的方式。

文章转自CSDN,点击查看原文

鲸鱼云公众号:dockercloud

鲸鱼云公众号

转载请加上原文链接和本文链接:鲸鱼云 » 从Docker零基础到懂一点实践教程(四)

分享到:更多 ()

评论 抢沙发

评论前必须登录!