Docker learning - Mirror

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43505295/article/details/102723452

Docker mirror as the three core concepts, the most important keywords, it has a lot to operate, you have to want to learn to master the technique of container. This article will take you step by step, both graphic, started operations to learn it.

table of Contents

A download mirror Docker

If we want to run locally container, it must ensure that there is a corresponding local mirror. So, the first step, we need to download the image. When we tried to download the image, Docker will try to start with the default image repository to download (using a public warehouse Docker Hub default), of course, users can also customize the configuration repository mirroring want to download.

1.1 Download Mirror

Mirroring is a prerequisite for running a container, we can use the docker pull[IMAGE_NAME]:[TAG]command to download the image, which IMAGE_NAMErepresents the name of the mirror, but TAGa mirror image of the label, which means that we need to " mirror + label to download the image" approach.

Note: You can not explicitly specify TAG, it will default to download the latest label, that is, download the latest version of the repository mirroring. This is not recommended that you download the latest label, because the contents of the mirror keeps track of the latest version of the mirror, and change with, so it is unstable. In a production environment, there may be inexplicable bug, you are recommended to specify a specific TAG better display.

For example, if we want to download a mirror Mysql 5.7, is available through the command:

docker pull mysql:5.7

You will see the console output is as follows:

Docker Download Mirror

Note: Due to official DockerHub warehouse server in a foreign country, the download speed is slow, so I will address changes in the warehouse became the country docker.io's image warehouse, so there will be in the picture above, in front of the mirror docker.ioappears.

When there Downloaded time string output instructions to download a success! !

1.2 verification

Let's verify, whether locally mirrored Mysql5.7 exist, run the command:

docker images

Verify the existence of a local mirror

We can see the mirror does exist locally, indeed downloading a success!

Mirror 1.3 download details

To say that the mirror above the download process:

Docker image download

By downloading process, can be seen, a mirror is generally a plurality of layers ( layer), with similar f7e2b70d04aeunique ID string representation of such layers (practically complete ID including the 256 bit, 64 hexadecimal characters) .

You might think that if a number of different mirror, contains the same layer (layer), so repeat the download, would not lead to a waste of storage space it?

实际上,Docker 并不会这么傻会去下载重复的层( layer),Docker 在下载之前,会去检测本地是否会有同样 ID 的层,如果本地已经存在了,就直接使用本地的就好了。

另一个问题,不同仓库中,可能也会存在镜像重名的情况发生, 这种情况咋办?

严格意义上,我们在使用 docker pull 命令时,还需要在镜像前面指定仓库地址( Registry), 如果不指定,则 Docker 会使用您默认配置的仓库地址。例如上面,由于我配置的是国内 docker.io 的仓库地址,我在 pull 的时候,docker 会默认为我加上 docker.io/library 的前缀。

如:当我执行 docker pull mysql:5.7 命令时,实际上相当于 docker pull docker.io/mysql:5.7,如果您未自定义配置仓库,则默认在下载的时候,会在镜像前面加上 DockerHub 的地址。

Docker 通过前缀地址的不同,来保证不同仓库中,重名镜像的唯一性。

1.4 PULL 子命令

命令行中输入:

docker pull --help

会得到如下信息:

[root@iZbp1j8y1bab0djl9gdp33Z ~]# docker pull --help
Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:  -a, --all-tags                Download all tagged images in the repository      --disable-content-trust   Skip image verification (default true)      --help                    Print usage

我们可以看到主要支持的子命令有:

  1. -a,--all-tags=true|false: 是否获取仓库中所有镜像,默认为否;
  2. --disable-content-trust: 跳过镜像内容的校验,默认为 true;

二 Docker 查看镜像信息

2.1 images 命令列出镜像

通过使用如下两个命令,列出本机已有的镜像:

docker images

或:

docker image ls

如下图所示:

Docker View Mirror Information

对上述红色标注的字段做一下解释:

  • REPOSITORY: 来自于哪个仓库;
  • TAG: 镜像的标签信息,比如 5.7、latest 表示不同的版本信息;
  • IMAGE ID: 镜像的 ID, 如果您看到两个 ID 完全相同,那么实际上,它们指向的是同一个镜像,只是标签名称不同罢了;
  • CREATED: 镜像最后的更新时间;
  • SIZE: 镜像的大小,优秀的镜像一般体积都比较小,这也是我更倾向于使用轻量级的 alpine 版本的原因;

注意:图中的镜像大小信息只是逻辑上的大小信息,因为一个镜像是由多个镜像层( layer)组成的,而相同的镜像层本地只会存储一份,所以,真实情况下,占用的物理存储空间大小,可能会小于逻辑大小。

2.2 使用 tag 命令为镜像添加标签

通常情况下,为了方便在后续工作中,快速地找到某个镜像,我们可以使用 docker tag 命令,为本地镜像添加一个新的标签。如下图所示:

Docker tag Add tags

docker.io/mysql 镜像,添加新的镜像标签 allen_mysql:5.7。然后使用 docker images 命令,查看本地镜像:

Docker tag Add tags

可以看到,本地多了一个 allen_mysql:5.7 的镜像。细心的你一定还会发现, allen_mysql:5.7docker.io/mysql:5.7 的镜像 ID 是一模一样的,说明它们是同一个镜像,只是别名不同而已。

docker tag 命令功能更像是, 为指定镜像添加快捷方式一样。

2.3 使用 inspect 命令查看镜像详细信息

通过 docker inspect 命令,我们可以获取镜像的详细信息,其中,包括创建者,各层的数字摘要等。

docker inspect docker.io/mysql:5.7

Docker inspect view mirror details

docker inspect 返回的是 JSON 格式的信息,如果您想获取其中指定的一项内容,可以通过 -f 来指定,如获取镜像大小:

docker inspect -f {{".Size"}} docker.io/mysql:5.7

Docker inspect view mirror details

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

前面的小节中,我们知道了,一个镜像是由多个层(layer)组成的,那么,我们要如何知道各个层的具体内容呢?

通过 docker history 命令,可以列出各个层(layer)的创建信息,如我们查看 docker.io/mysql:5.7 的各层信息:

docker history docker.io/mysql:5.7

Docker history information layers

可以看到,上面过长的信息,为了方便展示,后面都省略了,如果您想要看具体信息,可以通过添加 --no-trunc 选项,如下面命令:

docker history --no-trunc docker.io/mysql:5.7

三 Docker 搜索镜像

3.1 search 命令

您可以通过下面命令进行搜索:

docker search [option] keyword

比如,您想搜索仓库中 mysql 相关的镜像,可以输入如下命令:

docker search mysql

Docker Search Mirror

3.2 search 子命令

命令行输入 docker search--help, 输出如下:

Usage:  docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:  -f, --filter filter   Filter output based on conditions provided      --help            Print usage      --limit int       Max number of search results (default 25)      --no-index        Don't truncate output      --no-trunc        Don't truncate output

可以看到 search 支持的子命令有:

  • -f,--filter filter: 过滤输出的内容;
  • --limitint:指定搜索内容展示个数;
  • --no-index: 不截断输出内容;
  • --no-trunc:不截断输出内容;

举个列子,比如我们想搜索官方提供的 mysql 镜像,命令如下:

docker search --filter=is-offical=true mysql

Docker official image search

再比如,我们想搜索 Stars 数超过 100 的 mysql 镜像:

docker search --filter=stars=100 mysql

Docker Search Mirror

四 Docker 删除镜像

4.1 通过标签删除镜像

通过如下两个都可以删除镜像:

docker rmi [image]

或者:

docker image rm [image]

支持的子命令如下:

  • -f,-force: 强制删除镜像,即便有容器引用该镜像;
  • -no-prune: 不要删除未带标签的父镜像;

Docker View Mirror InformationDocker 查看镜像信息

例如,我们想删除上章节创建的 allen_mysql:5.7 镜像,命令如下:

docker rmi allen_mysql:5.7

Docker remove a mirror

从上面章节中,我们知道 allen_mysql:5.7docker.io/mysql:5.7 实际上指向的是同一个镜像,那么,您可以能会有疑问,我删除了 allen_mysql:5.7, 会不会将 docker.io/mysql:5.7 镜像也给删除了?

实际上,当同一个镜像拥有多个标签时,执行 docker rmi 命令,只是会删除了该镜像众多标签中,您指定的标签而已,并不会影响原始的那个镜像文件。

不信的话,我们可以执行 docker images 命令,来看下 docker.io/mysql:5.7 镜像还在不在:

Docker View Mirror Information

可以看到, docker.io/mysql:5.7 镜像依然存在!

那么,如果某个镜像不存在多个标签,当且仅当只有一个标签时,执行删除命令时,您就要小心了,这会彻底删除镜像。

例如,这个时候,我们再执行 docker rmi docker.io/mysql:5.7 命令:

Docker remove a mirror

从上图可以看到,我们已经删除了 docker.io/mysql:5.7 镜像的所有文件层。该镜像在本地已不复存在了!

4.2 通过 ID 删除镜像

除了通过标签名称来删除镜像,我们还可以通过制定镜像 ID, 来删除镜像,如:

docker rmi ee7cbd482336

一旦制定了通过 ID 来删除镜像,它会先尝试删除所有指向该镜像的标签,然后在删除镜像本身。

4.3 删除镜像的限制

删除镜像很简单,但也不是我们何时何地都能删除的,它存在一些限制条件。

当通过该镜像创建的容器未被销毁时,镜像是无法被删除的。为了验证这一点,我们来做个试验。首先,我们通过 docker pull alpine 命令,拉取一个最新的 alpine 镜像, 然后启动镜像,让其输出 hello,docker!:

Docker run alpine

接下来,我们来删除这个镜像试试:

Docker remove a mirror

可以看到提示信息,无法删除该镜像,因为有容器正在引用他!同时,这段信息还告诉我们,除非通过添加 -f 子命令,也就是强制删除,才能移除掉该镜像!

docker rmi -f docker.io/alpine

但是,我们一般不推荐这样暴力的做法,正确的做法应该是:

  1. 先删除引用这个镜像的容器;
  2. 再删除这个镜像;

也就是,根据上图中提示的,引用该镜像的容器 ID ( 9d59e2278553), 执行删除命令:

docker rm 9d59e2278553

然后,再执行删除镜像的命令:

docker rmi 5cb3aa00f899

Docker remove a mirrorDocker 删除镜像

这个时候,就能正常删除了!

4.4 清理镜像

我们在使用 Docker 一段时间后,系统一般都会残存一些临时的、没有被使用的镜像文件,可以通过以下命令进行清理:

docker image prune

它支持的子命令有:

  • -a,--all: 删除所有没有用的镜像,而不仅仅是临时文件;
  • -f,--force:强制删除镜像文件,无需弹出提示确认;

五 Docker 创建镜像

此小节中,您将学习 Docker 如何创建镜像?Docker 创建镜像主要有三种:

  1. 基于已有的镜像创建;
  2. 基于 Dockerfile 来创建;
  3. 基于本地模板来导入;

我们将主要介绍常用的 1,2 两种。

5.1 基于已有的镜像创建

通过如下命令来创建:

docker container commit

支持的子命令如下:

  • -a,--author="": 作者信息;
  • -c,--change=[]: 可以在提交的时候执行 Dockerfile 指令,如 CMD、ENTRYPOINT、ENV、EXPOSE、LABEL、ONBUILD、USER、VOLUME、WORIR 等;
  • -m,--message="": 提交信息;
  • -p,--pause=true: 提交时,暂停容器运行。

接下来,基于本地已有的 Ubuntu 镜像,创建一个新的镜像:

Docker create a mirror

首先,让我将它运行起来,并在其中创建一个 test.txt 文件:

Docker create a mirror

命令如下:

docker run -it docker.io/ubuntu:latest /bin/bashroot@a0a0c8cfec3a:/# touch test.txtroot@a0a0c8cfec3a:/# exit

创建完 test.txt 文件后,需要记住标注的容器 ID: a0a0c8cfec3a, 用它来提交一个新的镜像(PS: 你也可以通过名称来提交镜像,这里只演示通过 ID 的方式)。

执行命令:

docker container commit -m "Added test.txt file" -a "Allen" a0a0c8cfec3a test:0.1

提交成功后,会返回新创建的镜像 ID 信息,如下图所示:

Docker submitted to the newly created mirror

再次查看本地镜像信息,可以看到新创建的 test:0.1 镜像了:

Docker View Mirror Information

5.2 基于 Dockerfile 创建

通过 Dockerfile 的方式来创建镜像,是最常见的一种方式了,也是比较推荐的方式。Dockerfile 是一个文本指令文件,它描述了是如何基于一个父镜像,来创建一个新镜像的过程。

下面让我们来编写一个简单的 Dockerfile 文件,它描述了基于 Ubuntu 父镜像,安装 Python3 环境的镜像:

FROM docker.io/ubuntu:latest
LABEL version="1.0" maintainer="Allen <weiwosuo@github>"
RUN apt-get update && \    apt-get install -y python3 && \    apt-get clean && \    rm -rf /var/lib/apt/lists/*

创建完成后,通过这个 Dockerfile 文件,来构建新的镜像,执行命令:

docker image build -t python:3 .

注意: 命令的最后有个点,如果不加的话,会构建不成功 !

Docker mirror constructed by Dockerfile

编译成功后,再次查看本地镜像信息,就可以看到新构建的 python:3 镜像了。

Docker View Mirror Information

六 Docker 导出&加载镜像

此小节中,您将学习 Docker 如何导出&加载镜像。

通常我们会有下面这种需求,需要将镜像分享给别人,这个时候,我们可以将镜像导出成 tar 包,别人直接通过加载这个 tar 包,快速地将镜像引入到本地镜像库。

要想使用这两个功能,主要是通过如下两个命令:

  1. docker save
  2. docker load

6.1 导出镜像

查看本地镜像如下:

Docker View Mirror Information

例如,我们想要将 python:3 镜像导出来,执行命令:

docker save -o python_3.tar python:3

执行成功后,查看当前目录:

Docker export fileDocker 导出文件

可以看到 python_3.tar 镜像文件已经生成。接下来,你可以将它通过复制的方式,分享给别人了!

6.2 加载镜像

别人拿到了这个 tar 包后,要如何导入到本地的镜像库呢?

通过执行如下命令:

docker load -i python_3.tar

或者:

docker load < python_3.tar

导入成功后,查看本地镜像信息,你就可以获得别人分享的镜像了!怎么样,是不是很方便呢!

七 Docker 上传镜像

我们将以上传到 Docker Hub 上为示例,演示 Docker 如何上传镜像。

7.1 获取 Docker ID

想要上传镜像到 Docker Hub 上,首先,我们需要注册 Docker Hub 账号。打开 Docker Hub 网址 https://hub.docker.com,开始注册:

Docker Hub registered account

填写您的 Docker ID (也就是账号),以及密码,Email, 点击继续。

接下来,Docker Hub 会发送验证邮件,到您填写的邮箱当中:

Docker Hub verification email

点击验证即可,接下来,再次返回 Docker Hub 官网,用您刚刚注册的 Docker ID 和密码来登录账号!

Docker Hub login page

7.2 创建镜像仓库

登录成功后,会出现如下页面:

Welcome to the Docker Hub

选择创建一个镜像仓库:

Creating Python warehouse

填写仓库名称描述信息是否公开后,点击创建。

Warehouse Mirror Display page仓库镜像展示页

我们看到,仓库已经创建成功了,但是里面还没有任何镜像,接下来开始上传镜像,到此新创建的仓库中。

7.3 上传镜像

Enter the command line, with Docker ID and password we have just obtained , execute the command:

docker login

Command line login Docker IDCommand line login Docker ID

After a successful login, we began to prepare upload a local python:3image:

python: 3 Mirror

First, we hit them a new label prefix consistent with Docker ID, the name of our new warehouse created :

docker tag python:3 weiwosuoai1991/python:3

python: 3 image to play tag

View local information, see the label hit success. Continued open, start uploading! Excuting an order:

docker push weiwosuoai1991/python:3

Upload python: 3 Mirror

Upload successful! Docker Hub to the official website, information pages of the newly created repository to test, whether it is a success:

Warehouse Mirror Display pageWarehouse Mirror Display page

Carpenter TsugeNaru! ! !

Eight summary

In this paper, we focus on learning the Docker download mirror ,, mirror view, search mirrors, remove a mirror ,, create a mirror, Export & load the image and upload the image related operations to Docker Hub.

Guess you like

Origin blog.csdn.net/weixin_43505295/article/details/102723452