Skip to main content
 首页 » 编程设计

docker之无法在 dockerfile 中设置代理

2024年12月31日30hnrainll

我无法运行 Docker "Getting started" example在企业防火墙后面。它在没有防火墙的情况下在家中运行良好。

当我使用 docker build -t my_tag . 构建图像时,尝试安装某些 python 包时失败。 docker 文件有这个命令:

RUN pip install -r requirements.txt 

错误信息是:

Collecting Redis (from -r requirements.txt (line 1)) Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))': /simple/redis/



我尝试在 dockerfile 中设置代理(以各种格式,但它们都失败了):
ENV http_proxy http://my_username:my_password@my_host:/80 
ENV https_proxy https://my_username:my_password@my_host:/80 
ENV HTTP_PROXY http://my_username:my_password@my_host:/80 
ENV HTTPS_PROXY https://my_username:my_password@my_host:/80 
ENV http_proxy http://my_username:my_password@my_host:80 
ENV https_proxy https://my_username:my_password@my_host:80 
ENV HTTP_PROXY http://my_username:my_password@my_host:80 
ENV HTTPS_PROXY https://my_username:my_password@my_host:80 
ENV http_proxy=http://my_username:my_password@my_host:80 
ENV https_proxy=https://my_username:my_password@my_host:80 
ENV HTTP_PROXY=http://my_username:my_password@my_host:80 
ENV HTTPS_PROXY=https://my_username:my_password@my_host:80 

我尝试在文件/etc/systemd/system/docker.service.d/http-proxy.conf 中设置环境:
[Service] 
Environment="HTTP_PROXY=http://my_username:my_password@my_host:80/" "HTTPS_PROXY=https://my_username:my_password@my_host:80/" 

我曾尝试更改 RUN pip install命令在 dockerfile 中,因此它也包括代理:
RUN pip install --proxy="my_username:my_password@my_host:80" -r requirements.txt 

我尝试在/etc/default/docker 中设置选项,添加:
DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" 

我尝试将代理添加到 docker build 命令:
docker build -t python_example_1 --build-arg HTTPS_PROXY=https://my_username:my_password@my_host:80 --build-arg HTTP_PROXY=http://my_username:my_password@my_host:80 . 

我正在使用 docker 1.12.6 版在 Linux mint 上运行它。

谁有想法?

更新 1:
如果我简化 dockerfile 所以它使用 curl (没有 python 或 pip)
FROM ubuntu 
WORKDIR /app 
ADD . /app 
EXPOSE 80 
 
# Define environment variable 
ENV http_proxy=http://my_username:my_password@my_host:80 
ENV https_proxy=http://my_username:my_password@my_host:80 
ENV HTTP_PROXY=http://my_username:my_password@my_host:80 
ENV HTTPS_PROXY=http://my_username:my_password@my_host:80 
 
RUN apt-get -qq update 
RUN apt-get -qq -y install curl 
 
CMD ["curl www.google.com.au"] 

错误消息表明它正在获取代理信息,但存在某种名称解析问题:
Removing intermediate container 945f8123da61 
Step 10 : RUN apt-get -qq update 
 ---> Running in 99a5bbbb943d 
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial/InRelease  Temporary failure resolving 'my_host' 

其中“my_host”是正确的代理(顺便说一句,我已经对此进行了测试,并且用户名/密码/代理主机名确实有效)

更新 2
如果我从 docker 文件中删除 ENV,那么现在是:
FROM ubuntu 
WORKDIR /app 
ADD . /app 
EXPOSE 80 
 
ARG http_proxy=http://my_username:my_password@my_host:80 
ARG https_proxy=http://my_username:my_password@my_host:80 
ARG HTTP_PROXY=http://my_username:my_password@my_host:80 
ARG HTTPS_PROXY=http://my_username:my_password@my_host:80 
 
RUN apt-get -qq update 

并使用 --build-args 构建:
docker build --build-arg HTTPS_PROXY=http://my_username:my_password@my_host:80 --build-arg HTTP_PROXY=http://my_username:my_password@my_host:80 . 

错误仍然是 Temporary failure resolving 'archive.ubuntu.com'
如果我尝试通过将其放在 docker 文件中来添加名称服务器:
RUN echo "nameserver 8.8.8.8" >> /etc/resolv.conf 
RUN cat /etc/resolve.conf 

错误信息是:
cat: /etc/resolve.conf: No such file or directory 
The command '/bin/sh -c cat /etc/resolve.conf' returned a non-zero code: 1 

也许这不起作用,因为它正在“中间容器”中创建/etc/resolv.conf 文件,而该文件在下一步中不可用?。更长的输出是:
Removing intermediate container 1f77c03ee8be 
Step 5 : RUN echo "nameserver 8.8.8.8" >> /etc/resolv.conf 
 ---> Running in e2a9717f6bed 
 ---> 13752a04094b 
Removing intermediate container e2a9717f6bed 
Step 6 : RUN cat /etc/resolve.conf 
 ---> Running in 94ce4a72867b 
cat: /etc/resolve.conf: No such file or directory 

请您参考如下方法:

如果您在正常环境中设置了代理(意味着在 docker 构建中没有,但对于您的主机操作系统),请仔细检查其 https_proxy值(value)。

https_proxy URL 应该与 http_proxy 相同一:应该以http开头.
不是 https .

首先在简化的 Dockerfile 中做一个简单的测试(例如 curl www.google.com)
然后,另见 pip issue 1805其中显示了如何 pip可以使用代理。