Docker 简明教程

Docker - Public Repositories

公开存储库可用于托管 Docker 映像,其他人可以利用这些映像。一个示例是 Docker Hub 中可用的映像。大多数映像,如 Centos、Ubuntu 和 Jenkins,都对所有人公开。我们还可以通过将其发布到 Docker Hub 上的公共存储库来使我们的映像可用。

Public repositories can be used to host Docker images which can be used by everyone else. An example is the images which are available in Docker Hub. Most of the images such as Centos, Ubuntu, and Jenkins are all publicly available for all. We can also make our images available by publishing it to the public repository on Docker Hub.

对于我们的示例,我们将使用在“构建 Docker 文件”一章中构建的 myimage 库,并将该映像上传到 Docker Hub。让我们首先查看 Docker 主机上的映像,以了解我们可以将哪些映像推送到 Docker 注册表。

For our example, we will use the myimage repository built in the "Building Docker Files" chapter and upload that image to Docker Hub. Let’s first review the images on our Docker host to see what we can push to the Docker registry.

my image

这里,我们有 myimage:0.1 映像,它是作为“构建 Docker 文件”一章的一部分创建的。让我们使用它上传到 Docker 公共存储库。

Here, we have our myimage:0.1 image which was created as a part of the “Building Docker Files” chapter. Let’s use this to upload to the Docker public repository.

以下步骤说明了如何将映像上传到公共存储库。

The following steps explain how you can upload an image to public repository.

Step 1 − 登录 Docker Hub 并创建你的存储库。这是将存储你的映像的存储库。转到 https://hub.docker.com/ 并使用你的凭据登录。

Step 1 − Log into Docker Hub and create your repository. This is the repository where your image will be stored. Go to https://hub.docker.com/ and log in with your credentials.

docker hub

Step 2 − 在以上屏幕上单击“创建存储库”按钮,并创建名为 demorep 的存储库。请确保存储库的可见性为公开。

Step 2 − Click the button "Create Repository" on the above screen and create a repository with the name demorep. Make sure that the visibility of the repository is public.

demorep

一旦创建了存储库,请记下附加到存储库的 pull 命令。

Once the repository is created, make a note of the pull command which is attached to the repository.

repository

用于我们的存储库中的 pull 命令如下 −

The pull command which will be used in our repository is as follows −

docker pull demousr/demorep

Step 3 − 现在返回 Docker 主机。这里我们需要将我们的 myimage 标记到在 Docker Hub 中创建的新存储库。我们可以通过 Docker tag command 来完成此操作。

Step 3 − Now go back to the Docker Host. Here we need to tag our myimage to the new repository created in Docker Hub. We can do this via the Docker tag command.

我们将在本章后面详细了解这个 tag command

We will learn more about this tag command later in this chapter.

Step 4 − 从命令提示符发出 Docker 登录命令以登录到 Docker Hub 存储库。Docker 登录命令将提示你输入 Docker Hub 存储库的用户名和密码。

Step 4 − Issue the Docker login command to login into the Docker Hub repository from the command prompt. The Docker login command will prompt you for the username and password to the Docker Hub repository.

docker login command

Step 5 − 一旦映像被标记,现在是时候将映像推送到 Docker Hub 存储库了。我们可以通过 Docker push 命令来完成此操作。我们将在本章后面详细了解此命令。

Step 5 − Once the image has been tagged, it’s now time to push the image to the Docker Hub repository. We can do this via the Docker push command. We will learn more about this command later in this chapter.

docker tag

此方法允许将一个映像标记到相关存储库。

This method allows one to tag an image to the relevant repository.

Syntax

docker tag imageID Repositoryname

Options

  1. imageID − This is the ImageID which needs to be tagged to the repository.

  2. Repositoryname − This is the repository name to which the ImageID needs to be tagged to.

Return Value

None

Example

sudo docker tag ab0c1d3744dd demousr/demorep:1.0

Output

上面示例的示例输出如下。

A sample output of the above example is given below.

docker tag

docker push

此方法允许将映像推送到 Docker Hub。

This method allows one to push images to the Docker Hub.

Syntax

docker push Repositoryname

Options

  1. Repositoryname − This is the repository name which needs to be pushed to the Docker Hub.

Return Value

推送到 Docker Hub 的存储库的长 ID。

The long ID of the repository pushed to Docker Hub.

Example

sudo docker push demousr/demorep:1.0

Output

docker push

如果你返回 Docker Hub 页面并转到你的存储库,你将在存储库中看到标签名称。

If you go back to the Docker Hub page and go to your repository, you will see the tag name in the repository.

tag name in repository

现在让我们尝试拉取我们将上传到 Docker 主机的存储库。我们先从本地 Docker 主机中删除图像 myimage:0.1demousr/demorep:1.0 。我们使用 Docker pull command 从 Docker Hub 拉取存储库。

Now let’s try to pull the repository we uploaded onto our Docker host. Let’s first delete the images, myimage:0.1 and demousr/demorep:1.0, from the local Docker host. Let’s use the Docker pull command to pull the repository from the Docker Hub.

docker pull command

从上面的截图中,可以看到 Docker pull 命令已把我们的新存储库从 Docker Hub 提出并放在了我们的机器上。

From the above screenshot, you can see that the Docker pull command has taken our new repository from the Docker Hub and placed it on our machine.