Docker 简明教程

Docker - Private Registries

你可能需要拥有自己的私有存储库。你可能不想在 Docker Hub 上托管存储库。为此,Docker 本身提供了一个存储库容器。让我们看看如何下载并使用存储库容器。

You might have the need to have your own private repositories. You may not want to host the repositories on Docker Hub. For this, there is a repository container itself from Docker. Let’s see how we can download and use the container for registry.

Step 1 - 使用 Docker run 命令下载私有存储库。可以使用以下命令执行此操作。

Step 1 − Use the Docker run command to download the private registry. This can be done using the following command.

sudo docker run –d –p 5000:5000 –-name registry registry:2

需要对上述命令注意以下几点 −

The following points need to be noted about the above command −

  1. Registry is the container managed by Docker which can be used to host private repositories.

  2. The port number exposed by the container is 5000. Hence with the –p command, we are mapping the same port number to the 5000 port number on our localhost.

  3. We are just tagging the registry container as “2”, to differentiate it on the Docker host.

  4. The –d option is used to run the container in detached mode. This is so that the container can run in the background

detached mode

Step 2 - 让我们做 docker ps ,看看存储库容器是否确实在运行。

Step 2 − Let’s do a docker ps to see that the registry container is indeed running.

docker ps

我们现在已经确认存储库容器确实在运行。

We have now confirmed that the registry container is indeed running.

Step 3 - 现在,让我们标记我们现有的一个镜像,以便我们可以将其推送到我们的本地存储库。在我们的示例中,由于我们在本地有 centos 镜像可用,我们将把它标记到我们的私有存储库并添加一个 centos 的标签名。

Step 3 − Now let’s tag one of our existing images so that we can push it to our local repository. In our example, since we have the centos image available locally, we are going to tag it to our private repository and add a tag name of centos.

sudo docker tag 67591570dd29 localhost:5000/centos

需要对上述命令注意以下几点 −

The following points need to be noted about the above command −

  1. 67591570dd29 refers to the Image ID for the centos image.

  2. localhost:5000 is the location of our private repository.

  3. We are tagging the repository name as centos in our private repository.

private repository

Step 4 - 现在我们使用 Docker push 命令将存储库推送到我们的私有存储库。

Step 4 − Now let’s use the Docker push command to push the repository to our private repository.

sudo docker push localhost:5000/centos

在这里,我们正在将 centos 镜像推送到托管在 localhost:5000 的私有存储库中。

Here, we are pushing the centos image to the private repository hosted at localhost:5000.

localhost

Step 5 − 现在让我们使用 docker rmi 命令,删除我们为 centos 提供的本地图片。然后我们可以从我们的私有存储中下载所需的 centos 图片。

Step 5 − Now let’s delete the local images we have for centos using the docker rmi commands. We can then download the required centos image from our private repository.

sudo docker rmi centos:latest
sudo docker rmi 67591570dd29
docker rmi commands

Step 6 − 现在我们在本地计算机上没有任何 centos 图片,我们可以使用如下 Docker pull 命令从我们的私有存储中拉取 centos 图片。

Step 6 − Now that we don’t have any centos images on our local machine, we can now use the following Docker pull command to pull the centos image from our private repository.

sudo docker pull localhost:5000/centos

在此,我们正在将 centos 图片拉取到 localhost:5000 托管的私有存储中。

Here, we are pulling the centos image to the private repository hosted at localhost:5000.

pulling centos image

如果您现在查看系统上的图片,您还将看到 centos 图片。

If you now see the images on your system, you will see the centos image as well.