【玩转Docker系列4】镜像和仓库

举报
fengfeng 发表于 2017/03/08 00:52:19 2017/03/08
【摘要】 镜像和仓库、下载镜像、查看镜像、 通过命令直接下载镜像……

镜像和仓库

1.1    下载、查看镜像

1.1.1     通过命令直接下载镜像

UbuntuTest:~ # docker pull ubuntu

Using default tag: latest

latest: Pulling from library/ubuntu

952132ac251a: Pull complete

82659f8f1b76: Pull complete

c19118ca682d: Pull complete

8296858250fe: Pull complete

24e0251a0e2c: Pull complete

Digest:   sha256:f4691c96e6bbaa99d99ebafd9af1b68ace2aa2128ae95a60369c506dd6e6f6ab

Status: Downloaded newer image for ubuntu:latest

UbuntuTest:~ # docker images

REPOSITORY         TAG                IMAGE ID              CREATED               SIZE

ubuntu                latest                bd3d4369aebc        12 days   ago         126.6 MB

python                latest                0b5a17c108b6        7 weeks   ago         694.2 MB

golang                latest                c47d40563451        7 weeks   ago         753.5 MB

opensuse             latest                76c8c565bc28        8 weeks   ago         97.74 MB

hello-world           latest                c54a2cc56cbb        9 weeks   ago         1.848 kB

testframe5.0          latest                1bd575496a8e        12 weeks   ago        773.1 MB

thezxm/redhat7        latest                8feabc99ecd9        18 months   ago       436.9 MB

1.1.2     第一次创建容器时下载镜像(本地无此镜像)

UbuntuTest:~ # docker run hello-world

Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world

c04b14da8d14: Pull complete

Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9

Status: Downloaded newer image for   hello-world:latest

 Hello from Docker!

This message shows that your installation appears   to be working correctly.

 To generate this message, Docker took the   following steps:

 1. The Docker client contacted the Docker   daemon.

 2. The Docker daemon pulled the   "hello-world" image from the Docker Hub.

 3. The Docker daemon created a new   container from that image which runs the

    executable that produces the   output you are currently reading.

 4. The Docker daemon streamed that output   to the Docker client, which sent it

    to your terminal.

 To try something more ambitious, you can   run an Ubuntu container with:

 $ docker run -it ubuntu bash

 Share images, automate workflows, and more   with a free Docker Hub account:

 https://hub.docker.com

 For more examples and ideas, visit:

 https://docs.docker.com/engine/userguide/

1.1.3     查看当前系统可用的镜像列表

UbuntuTest:~ # docker images

REPOSITORY      TAG                   IMAGE ID              CREATED               SIZE

ubuntu                latest                bd3d4369aebc        12 days   ago         126.6 MB

python                latest                0b5a17c108b6          7 weeks ago         694.2 MB

golang                latest                c47d40563451        7 weeks   ago         753.5 MB

opensuse              latest                76c8c565bc28        8 weeks   ago         97.74 MB

hello-world           latest                c54a2cc56cbb        9 weeks   ago         1.848 kB

testframe5.0          latest                1bd575496a8e        12 weeks   ago        773.1 MB

thezxm/redhat7        latest                8feabc99ecd9        18 months   ago       436.9 MB

1.1.4     镜像文件后台存储位置

Docker相关文件存放在:/var/lib/docker目录下,镜像的存储结构主要分两部分,一是镜像ID之间的关联,一是镜像ID与镜像名称之间的关联,前者的结构体叫Graph,后者叫TagStore

UbuntuTest:/var/lib/docker # l

total 60

drwx------  5 root root  4096 Jul 22   04:35 aufs

drwx------  8 root root  4096 Sep    8 19:57 containers

drwx------ 47 root root  4096 Jul 24 01:36 graph

drwx------  3 root root  4096 Jul 22   04:35 image

drwx------  2 root root  4096 Jul 23   19:26 init

-rw-r--r--  1 root root 11264 Sep  5   16:47 linkgraph.db

drwxr-x---  3 root root  4096 Jul 22   04:35 network

-rw-------  1 root root   686 Jul   24 01:54 repositories-aufs

drwx------  2 root root  4096 Sep    5 16:47 swarm

drwx------  2 root root  4096 Sep 14   11:35 tmp

drwx------  2 root root  4096 Jul 22   04:35 trust

drwx------  3 root root  4096 Jul 24   01:28 vfs

drwx------ 10 root root  4096 Sep  5   17:17 volumes

1 Docker镜像后台存储文件列表:

文件或目录名

说明

/var/lib/docker/repositories-aufs

TagStore的存储地方,里面有image idreponametag之间的映射关系. aufsdriver名。

/var/lib/docker/graph

每个镜像的json描述文件和layersize大小。

/var/lib/docker/aufs/diff

每层layer与其父layer之间的文件差异,有的为空,有的有文件(镜像实际存储的地方)。

/var/lib/docker/aufs/layers

每层layer一个文件,记录其父layer一直到根layer之间的ID,每个ID一行。大部分文件的最后一行都一样,表示继承自同一个layer

/var/lib/docker/aufs/mnt

有容器运行时里面有数据(容器数据实际存储的地方,包含整个文件系统数据),退出时里面为空。

/var/lib/docker/volumes

volumes卷的存储地。

1.2    删除、重命名镜像

1.2.1     删除镜像

UbuntuTest:~ # docker rmi hello-world

Untagged: hello-world:latest

Untagged: hello-world@sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9

Deleted:   sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc

Deleted:   sha256:a02596fdd012f22b03af6ad7d11fa590c57507558357b079c3e8cebceb4262d7

UbuntuTest:~ # docker images

REPOSITORY         TAG                   IMAGE ID              CREATED               SIZE

ubuntu                latest                bd3d4369aebc        12 days   ago         126.6 MB

python                latest                0b5a17c108b6        7 weeks   ago         694.2 MB

golang                latest                c47d40563451        7 weeks   ago         753.5 MB

opensuse              latest                76c8c565bc28        8 weeks   ago         97.74 MB

testframe5.0          latest                1bd575496a8e        12 weeks   ago        773.1 MB

redhat7               latest                8feabc99ecd9        18 months   ago       436.9 MB

thezxm/redhat7        latest                8feabc99ecd9        18 months   ago       436.9 MB

1.2.2     重命名镜像

UbuntuTest:~ # docker tag thezxm/redhat7   redhat7

UbuntuTest:~ # docker images

REPOSITORY        TAG                   IMAGE ID              CREATED               SIZE

ubuntu                latest                bd3d4369aebc        12 days   ago         126.6 MB

python                latest                0b5a17c108b6        7 weeks   ago         694.2 MB

golang                latest                c47d40563451          7 weeks ago         753.5 MB

opensuse              latest                76c8c565bc28        8 weeks   ago         97.74 MB

testframe5.0          latest                1bd575496a8e        12 weeks   ago        773.1 MB

thezxm/redhat7        latest                8feabc99ecd9        18 months   ago       436.9 MB

redhat7               latest                8feabc99ecd9        18 months   ago       436.9 MB

1.3    查找镜像

如下图所示,其中OFFICIALOK表示该镜像是由上游开发者管理的镜像(如ubuntu镜像由Ubuntu团队管理和提供)。

搭建私有仓库

步骤1:获取registry镜像。

UbuntuTest:~/dockerfile_test # docker pull   registry:2.1.1

2.1.1: Pulling from library/registry

Digest:   sha256:4a9bc5424fa7cc4fafd88b5d8dfb102ceb339e8f418fdeca7f094c86db284f1c

Status: Downloaded newer image for registry:2.1.1

 

UbuntuTest:~/dockerfile_test # docker images  

REPOSITORY                TAG                   IMAGE ID              CREATED               SIZE

ubuntu                       latest                bd3d4369aebc        3 weeks   ago         126.6 MB

registry                      2.1.1                 52bb991b482e        11 months   ago       220.1 MB

redhat7                      latest                8feabc99ecd9        18 months   ago       436.9 MB

步骤2:运行已下载的registry镜像。

UbuntuTest:~/dockerfile_test # docker run -d   -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name   registry registry:2.1.1

9d2df623dbce5468189ddea5945b133f90407ef49c15264308131dcb1f5b382a

UbuntuTest:~/dockerfile_test # docker ps

CONTAINER   ID          IMAGE                 COMMAND                    CREATED               STATUS                PORTS                      NAMES

9d2df623dbce          registry:2.1.1      "/bin/registry   /etc/d"   9 seconds ago       Up   8 seconds        0.0.0.0:5000->5000/tcp     registry

 

打开浏览器输入http://10.71.96.200:5000/v2,出现下面情况说明registry运行正常步骤3:从私有仓库下载镜像

删除本地的127.0.0.1:5000/ubuntu_file_testhuguosheng888/ubuntu_file_test

UbuntuTest:~/dockerfile_test # docker rmi   huguosheng888/ubuntu_file_test

UbuntuTest:~/dockerfile_test # docker rmi   127.0.0.1:5000/ubuntu_file_test -f

UbuntuTest:~/dockerfile_test # docker images

REPOSITORY                      TAG                   IMAGE ID              CREATED               SIZE

ubuntu                            latest                bd3d4369aebc        3 weeks   ago         126.6 MB

registry                            2.1.1                 52bb991b482e        11 months   ago       220.1 MB

 

UbuntuTest:~/dockerfile_test # docker pull   127.0.0.1:5000/ubuntu_file_test

Using default tag: latest

latest: Pulling from ubuntu_file_test

Digest:   sha256:75ba99ae30de5fde5a8f0423f4251f904c8bf98fb4c556401613e57f9824d32f

Status: Downloaded newer image for   127.0.0.1:5000/ubuntu_file_test:latest

 

UbuntuTest:~/dockerfile_test # docker images

REPOSITORY                    TAG                   IMAGE ID              CREATED               SIZE

127.0.0.1:5000/ubuntu_file_test     latest                b80d42590d81        3 hours   ago         126.8 MB

ubuntu                           latest                bd3d4369aebc        3 weeks   ago         126.6 MB

registry                           2.1.1                 52bb991b482e        11 months   ago       220.1 MB

作者 | 胡国胜

转载请注明出处:华为云博客 https://portal.hwclouds.com/blogs

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200