问题描述
我在使用代理,但无法构建Docker映像。
I'm behind a proxy and I'm not able to build a Docker image.
我尝试过使用 FROM ubuntu
, FROM centos
和 FROM高山
,但易于获取更新
/ yum更新
/ apk更新
失败。
I tried with FROM ubuntu
, FROM centos
and FROM alpine
, but apt-get update
/ yum update
/ apk update
failed.
我的主机操作系统是Windows 10,因此我将Docker设置配置为使用我们的代理。
My host OS is Windows 10, so I configured my Docker settings to use our proxy.
我还添加了
ENV http_proxy http://<PROXY>
ENV https_proxy http://<PROXY>
到我的Dockerfile,但没有成功。
to my Dockerfile but no success.
我还尝试将代理设置为 http://< USER>:< PASS> @< PROXY>
,但仍然没有成功。
I also tried to set my proxy to http://<USER>:<PASS>@<PROXY>
, but again no success.
我能够提取Docker映像。当我将代理设置设置为没有代理时,我无法提取图像,因此我猜我的代理URL是正确的。
I am able to pull Docker images. When I set my proxy settings to no proxy, I'm not able to pull images, so I guess my proxy URL is correct.
还有其他可以尝试的方法吗?
Any ideas what else I can try?
编辑:
我也尝试过将我们的DNS服务器(列在 ipconfig / all
下)添加到Docker设置中,但同样没有成功。
I also tried to add our DNS server (which is listed under ipconfig /all
) into the Docker settings, but again no success.
Edit2 :
我刚刚意识到我忘记了Ubuntu Dockerfile中的 http:// 。添加此选项后, docker build
现在对ubuntu可以正常工作-但对于ubuntu only 。 它对于 centos
和 alpine
仍然不起作用。
I just realized I forget the "http://" within my Ubuntu Dockerfile. After adding this, docker build
now works fine for ubuntu - but only for ubuntu. It still doesn't work for centos
and alpine
.
这是我所有的3个Dockerfile:
Here are all my 3 Dockerfiles:
Ubuntu:
FROM ubuntu
ENV http_proxy "http://<MY-PROXY>"
ENV https_proxy "http://<MY-PROXY>"
RUN apt-get update
CentOS:
FROM centos
ENV http_proxy "http://<MY-PROXY>"
ENV https_proxy "http://<MY-PROXY>"
RUN yum update
高山:
FROM alpine
ENV http_proxy "http://<MY-PROXY>"
ENV https_proxy "http://<MY-PROXY>"
RUN apk update
错误消息:
CentOS :
Step 4/4 : RUN yum update
---> Running in 3deecb71823d
Loaded plugins: fastestmirror, ovl
One of the configured repositories failed (Unknown),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
[...]
Cannot find a valid baseurl for repo: base/7/x86_64
高山:
Step 4/4 : RUN apk update
---> Running in 76c8579734cf
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.6/main: could not connect to server (check repositories file)
WARNING: Ignoring APKINDEX.84815163.tar.gz: No such file or directory
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
2 errors; 11 distinct packages available
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.6/community: could not connect to server (check repositories file)
WARNING: Ignoring APKINDEX.24d64ab1.tar.gz: No such file or directory
The command '/bin/sh -c apk update' returned a non-zero code: 2
推荐答案
将http(s)_proxy设置为系统范围的变量是一个很糟糕的主意。
U只需要使包管理器的代理工作正常。
如果您仍然想设置http(s)_proxy,请不要忘记no_proxy,否则您的所有流量都将尝试通过代理主机。
对于ubuntu,我更喜欢使用类似的东西
It's quite bad idea to set http(s)_proxy as system wide variable.U only need to make that package manager work's over proxy.If u still want to set http(s)_proxy don't forget about no_proxy or all your traffic will try to go via proxy host.For ubuntu i prefer to use something like this
FROM ubuntu
ARG PROXY=false
ARG PROXY_URL="http://proxy:8080"
RUN if [ "$PROXY" = true ] ; then echo 'Acquire::http::Proxy "'$PROXY_URL'";' >> /etc/apt/apt.conf ; fi && \
apt-get update && \
apt-get install -y vim
然后在服务器上像这样执行它互联网连接,但本地执行无需代理即可工作
And execute it like this on server without internet connection, but local execute will work without proxy
docker build -t ubuntu-with-proxy --build-arg PROXY=true .
Centos也可以在yum.conf中处理代理配置
Centos also can handle proxy configuration inside yum.conf
FROM centos
ARG PROXY=false
ARG PROXY_URL="http://proxy:8080"
RUN if [ "$PROXY" = true ] ; then echo 'proxy="$PROXY_URL";' >> /etc/yum.conf ; fi && \
yum install -y vim
然后在没有互联网连接的服务器上像这样执行,但本地执行无需代理即可工作
And execute it like this on server without internet connection, but local execute will work without proxy
docker build -t centos-with-proxy --build-arg PROXY=true .
但是我找不到适合高山的解决方案
我认为可以使用在Alpine中实现Centos / Ubuntu之类的功能,但我尚未对此进行测试。
But i can't find such solution for alpine
I think that something like for Centos/Ubuntu could be achieved in Alpine with this, but i haven't test this yet.
FROM alpine
ARG PROXY=false
ARG PROXY_URL="http://proxy:8080"
RUN if [ "$PROXY" = true ] ; then echo "http_proxy = $PROXY_URL" > /etc/wgetrc && echo "use_proxy = on" >> /etc/wgetrc ; fi && \
apk add -U vim
再次执行
docker build -t alpine-with-proxy --build-arg PROXY=true .
这篇关于百胜更新/ APK更新/ apt-get更新无法在代理后面工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!