gpt4 book ai didi

docker - 构建docker镜像时如何访问父主机服务?

转载 作者:行者123 更新时间:2023-12-02 20:05:02 25 4
gpt4 key购买 nike

在构建 docker 镜像时,我想访问托管在父主机上的服务。例如,假设我需要访问 npm在主机上运行的私有(private)存储库xpto:8080 .在 xpto 上,我还在 Dockerfile 调用上构建一个新图像

RUN npm set registry http://xpto:8080
RUN npm install

当我尝试 docker build -t=my_image .我总是得到

failed, reason: connect EHOSTUNREACH 192.168.2.103:4873



也试过 RUN wget xpto:8080并得到

failed: No route to host.



尝试使用 --add-host 参数,但它没有锻炼。

奇怪的是,当我尝试从另一个容器访问父主机服务时,它运行正常,但必须添加 --net="host"参数,如下所示:
docker run --it --rm --net="host" my-test-image sh
wget xpto:8080

问题是 docker build 不支持这个 --net 参数!

谢谢,

最佳答案

由于某些未知原因,centosOS 防火墙仅在构建时阻止连接,而不是在运行时阻止。

解决方案是在防火墙中添加一个异常(exception),问题就解决了。

关于docker - 构建docker镜像时如何访问父主机服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50377704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com