在 Docker for Windows 上,如何推送到正在端口转发到主机上本地主机的注册表?

On Docker for Windows, how do you push to a registry being port forwarded to localhost on the host machine?

我只是把它放在这里,因为很难找到关于这个主题的信息,最后我自己解决了它。

设置

curl localhost:5000/v2/_catalog 提供已安装注册表的列表。

到目前为止一切顺利。

docker tag {my image} localhost:5000/{my image}
docker push localhost:5000/{my image}

结果

The push refers to repository [localhost:5000/{my image}]                                                 
Get http://localhost:5000/v2/: dial tcp 127.0.0.1:5000: connect: connection refused

我们如何连接到转发到本地主机的注册表端口?

我发现一些晦涩的帖子建议您制作一个自定义特权容器并在容器内进行 ssh 堡垒端口转发。这实质上是解决了 docker 守护进程实际上 运行 在虚拟机中的事实引入的问题!

https://docs.docker.com/docker-for-windows/networking/

您可以在这里找到提示:

I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.

因此鉴于上述情况,我推断即使这个建议是针对容器的,docker 守护程序本身也可能在类似上下文中对 docker cli 命令起作用。

因此,首先您需要在 docker 守护程序设置中添加 host.docker.internal:5000 作为不安全注册表。在 Windows 的 Docker 上,可以在“设置”>“守护程序”>“不安全的注册表”中找到。不幸的是,这不算作本地主机,所以必须这样做(Docker 默认允许本地主机不安全的注册表)。然后简单地:

docker tag {my image} host.docker.internal:5000/{my image}
docker push host.docker.internal:5000/{my image}

成功!

希望这对其他一些非常困惑的开发人员有所帮助。