无法在dockerfile中设置代理
我无法运行企业防火墙背后的Docker “入门”示例 。 它在没有防火墙的情况下运行良好。
当我使用docker build -t my_tag .
构build图像时docker build -t my_tag .
,试图安装一些python软件包时会失败。 docker文件有这个命令:
RUN pip install -r requirements.txt
错误消息是:
收集Redis(来自-r requirements.txt(第1行))在连接被'ProxyError('无法连接到代理服务器')“的情况下重试(Retry(total = 4,connect = None,read = None,redirect = None) NewConnectionError(':无法build立新连接:[Errno -2]名称或服务未知',))':/ 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/"
我曾尝试更改dockerfile中的RUN pip install
命令,以使其包含代理:
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"]
该错误消息表明它正在获取代理信息,但存在某种名称parsing问题:
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”是正确的代理(BTW我已经testing了这个和用户名/密码/代理主机名工作)
更新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构build:
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
也许这不起作用,因为它是在下一步中不可用的“中间容器”中创build/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
值。
https_proxy
url应该与http_proxy
url相同:它应该以http
开头。
不是 https
。
首先在一个简化的Dockerfile中进行一个简单的testing(例如curl www.google.com
)
然后,请参阅pip issue 1805 ,其中显示了pip
如何使用代理。
解决scheme是将DNS条目添加到/etc/docker/daemon.json。
看到这里的描述