简体   繁体   English

带有镜像的 Docker 私有注册表

[英]Docker private registry with mirror

I created two Docker containers.我创建了两个 Docker 容器。 The first one provides a private Docker registry and the second one is a mirror of the official Docker registry:第一个提供私有 Docker 注册表,第二个是官方 Docker 注册表的镜像:

docker run -d --name registry -v /local/path/to/registry:/registry -e SETTINGS_FLAVOR=local -e STORAGE_PATH=/registry -p 5000:5000 registry

docker run -d --name mirror -v /local/path/to/mirror:/registry -e STORAGE_PATH=/registry -e STANDALONE=false -e MIRROR_SOURCE=https:/registry-1.docker.io -e MIRROR_SOURCE_INDEX=https://index.docker.io -p 5555:5000 registry

Now I would like to combine both.现在我想将两者结合起来。 Whenever a user pulls images it should first query the private registry and then the mirror.每当用户拉取镜像时,它应该首先查询私有注册表,然后查询镜像。 And when images are pushed they should only be pushed to the private registry.当图像被推送时,它们应该只被推送到私有注册表。

I do not have an idea about how this can be done.我不知道如何做到这一点。 Any help is appreciated.任何帮助表示赞赏。

You cannot just force all docker push commands to push to your private registry. 您不能只强制所有docker push命令推送到您的私有注册表。 One reason is that you can have any number of those registers. 一个原因是你可以拥有任意数量的寄存器。 You have to first tell docker where to push by tagging the image (see lower). 你必须首先通过标记图像告诉docker在哪里推送(见下图)。

Here is how you can setup docker hosts to work with a running private registry and local mirror. 以下是如何设置docker主机以使用正在运行的私有注册表和本地镜像。

Client set-up 客户端设置

Lets assume that you are running both mirror and private registry on (resolvable) host called dockerstore . 让我们假设您在名为dockerstore的 (可解析的)主机上运行镜像和私有注册表。 Mirror on port 5555, registry on 5000. 镜像端口5555,注册表5000。

Then on client machine(s) you should pass extra options to docker daemon startup. 然后在客户端计算机上,您应该将额外的选项传递给docker守护程序启动。 In your case: 在你的情况下:

  1. Add --registry-mirror=http://dockerstore:5555 to tell daemon to prefer using local mirror rather then dockerhub. 添加--registry-mirror=http://dockerstore:5555告诉守护进程更喜欢使用本地镜像而不是dockerhub。 source 资源
  2. Add --insecure-registry dockerstore:5000 to access the private registry without further configuration. 添加--insecure-registry dockerstore:5000访问私有注册表而无需进一步配置。 See this answer 看到这个答案
  3. Restart docker daemon 重启docker守护进程

Using the mirror 使用镜子

When you pull any image the first source will be the local mirror. 拉动任何图像时,第一个源将是本地镜像。 You can confirm by running a docker pull, eg 您可以通过运行docker pull来确认,例如

docker pull debian

In the output there will be message that image is being pulled from your mirror - dockerstore:5000 在输出中会有消息显示图像正从镜像中拉出 - dockerstore:5000

Using local registry 使用本地注册表

In order to push to private registry first you have to tag the image to be pushed with full name of the registry. 为了首先推送到私有注册表,您必须使用注册表的全名来标记要推送的映像 Make sure that you have a dot or colon in the first part of the tag , to tell docker that image should be pushed to private registry. 确保标记第一部分中有一个点或冒号,告诉docker该图像应该被推送到私有注册表。

Docker looks for either a “.” (domain separator) or “:” (port separator) to learn that the first part of the repository name is a location and not a user name. Docker查找“。”(域分隔符)或“:”(端口分隔符)以了解存储库名称的第一部分是位置而不是用户名。

Example: 例:

Tag 30d39e59ffe2 image as dockerstore:5000/myapp:stable 标记30d39e59ffe2图像为dockerstore:5000 / myapp:稳定

docker tag 30d39e59ffe2 dockerstore:5000/myapp:stable

Push it to private registry 将其推送到私人注册表

docker push dockerstore:5000/myapp:stable

Then you can pull as well 然后你也可以拉

docker pull dockerstore:5000/myapp:stable

Repository names are intended to be global , that is the repository redis always refers to the official Redis image from the Docker Hub. 存储库名称是全局的 ,即存储库redis始终引用Docker Hub中的官方Redis映像。 If you want to use a private registry, you prefix the repository name with the name of the registry eg localhost.localdomain:5000/myimage:mytag . 如果要使用私有注册表,则在存储库名称前加上注册表的名称,例如localhost.localdomain:5000/myimage:mytag

So when you pull or push, it will automatically go to the relevant registry. 所以当你拉或推时,它会自动转到相关的注册表。 The mirror should be easy to set up, you just pass the URL to the daemon with the --registry-mirror= argument. 镜像应该易于设置,您只需使用--registry-mirror=参数将URL传递给守护程序。

This isn't perfect for enterprise users, hence this (closed) Docker issue . 这对于企业用户来说并不完美,因此这个(封闭的)Docker问题

If not present, create the file:如果不存在,请创建文件:

sudo nano /etc/docker/daemon.json

Then paste the following:然后粘贴以下内容:

{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}

Then retart Docker daemon然后重启 Docker 守护进程

$ sudo systemctl restart docker

[ Source ] [来源]

Just to be clear, docker documentation confirms that: 为了清楚起见docker文档确认:

It's currently not possible to mirror another private registry. 目前无法镜像另一个私有注册表。 Only the central Hub can be mirrored. 只能镜像中央集线器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM