build造docker集装箱时将stream量路由到主机

我正在build造的docker图像通过pip拉了很多Python包。

RUN pip install -r lots-of-packages.txt 

我的主机正在运行PyPi软件包镜像和caching,以便这些软件包可以快速下载。

pip install命令允许用户通过参数--index-url指定包索引的位置:

 RUN pip install --index-url http://localhost:3141/root/pypi/ -r lots-of-packages.txt 

然而,在docker build期间,主机localhost引用泊坞窗容器本身,而不是我的主机服务caching。

如何通过docker build Docker容器来访问主机上的networking资源?

docker build --add-host <host>:<ip>似乎可以让我为IP设置一个主机名,但是在构build过程中是否有一个有效的IP来处理主机?

您可以通过在构build时指定networking模式来执行此操作:

 docker build --network host ... 

最简单的方法是检查docker0网桥的ip a (例如使用ip a命令),并在该网桥上使用主机ip而不是localhost。 所以假设你运行Linux和docker0 ip是172.17.0.1你的命令将如下所示:

RUN pip install --index-url http://172.17.0.1:3141/root/pypi/ lots-of-packages.txt

如果您在Windows或Mac上运行,那么可能会涉及到一些端口转发。