apt-get在Docker后面的公司代理

我试图在Docker的企业代理服务器背后设置一个开发环境。 尽可能地尝试,我不能让docker集装箱与代理服务器交谈。

代理服务器和apt-get在主机上运行良好,即Ubuntu 12.04

Dockerfile中的第一件事是尝试设置代理variables:

FROM ubuntu RUN echo 'Acquire::http { Proxy "http://my.proxy.net:8000"; };' >> /etc/apt/apt.conf.d/01proxy ENV HTTP_PROXY http://my.proxy.net:8000 ENV http_proxy http://my.proxy.net:8000 ENV HTTPS_PROXY https://my.proxy.net:8000 ENV https_proxy https://my.proxy.net:8000 RUN apt-get update && apt-get install -y build-essential 

它拉好图像,设置variables,但是当它进入apt-get更新,它会尝试一会儿,然后失败:

 W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/InRelease W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/InRelease W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/InRelease W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/Release.gpg Could not resolve 'my.proxy.net' W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/Release.gpg Could not resolve 'my.proxy.net' W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/Release.gpg Could not resolve 'my.proxy.net' W: Some index files failed to download. They have been ignored, or old ones used instead. 

我设置的这些variables与主机的linux安装(VirtualBox上的Ubuntu 12.04,如果有的话)是一致的,

我也有/ etc / default / docker设置了:

 export http_proxy="http://my.proxy.net:8000" export http_proxy="https://my.proxy.net:8000" 

有什么想法吗?

更新:

看起来像这是DNS的问题,不一定是代理服务器。 主机/etc/resolve.conf包含:

 # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 127.0.0.1 search dhcp.mycompany.com 

主机是在Windows 7机器上运行的virtualbox虚拟机,我发现了大多数似乎不起作用的各种不完全解决scheme。 无论我尝试什么,我都无法解决代理服务器的主机名

这个问题最终与DNS。 Docker在Ubuntu上运行,它本身就是VirtualBox上的一个Guest OS。 由于它是自己的虚拟化巨无霸,它在resolv.conf中分配了一个名字服务器127.0.0.1。

发生这种情况时,Docker将自己分配一个名为8.8.8.8(google的名称服务器)的DNS域名服务器,因为localhost引用的不是主机。

为了解决这个问题,我一路走到Windows并跑

 ipconfig /all 

并获得我的笔记本电脑的DNS服务器的IP地址。 我使用–dns = my.dns.ip.address将它们添加到configuration文件中的DOCKER_OPTS中,并重新启动了docker,而我通过代理执行的其他措施也正常运行。

如果你在防火墙后面build立,你必须使用Docker 1.9.x build-args。

在没有构build参数的情况下构buildDockerfile将失败并阻塞,如下所示:

 3b0d8aa7c417: Pull complete Digest: sha256:dc31e6056d314218689f028b51a58b2ca69b1dfdc5f33ead52b9351e9a19ee85 Status: Downloaded newer image for nodesource/trusty:4.2.3 ---> e17bee681d8f Step 2 : RUN apt-get update ---> Running in bdaf0006ccbd 

Apt-get block,因为它没有与archive.ubuntu.com连接…您可以validation通过运行图像…

 # docker run -ti --net=host --rm nodesource/trusty:4.2.3 bash root@pppdc9prd9rj:/usr/src/app# apt-get update 0% [Connecting to archive.ubuntu.com (91.189.92.201)]^C root@pppdc9prd9rj:/usr/src/app# ping archive.ubuntu.com PING archive.ubuntu.com (91.189.91.24) 56(84) bytes of data. ^C --- archive.ubuntu.com ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 1999ms 

使用build-arg解决了这个问题….

 # docker build -t migrator --build-arg http_proxy=$HTTP_PROXY . arg http_proxy=$HTTP_PROXY . Sending build context to Docker daemon 3.333 MB Step 1 : FROM nodesource/trusty:4.2.3 ---> e17bee681d8f Step 2 : RUN apt-get update ---> Running in 019b32d09a77 Ign http://archive.ubuntu.com trusty InRelease Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB] Get:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB] Get:3 http://archive.ubuntu.com trusty Release.gpg [933 B] Get:4 http://archive.ubuntu.com trusty Release [58.5 kB] Get:5 http://archive.ubuntu.com trusty-updates/main Sources [326 kB] Get:6 http://archive.ubuntu.com trusty-updates/restricted Sources [5217 B] Get:7 http://archive.ubuntu.com trusty-updates/universe Sources [1 

一些评论,根据我自己的经验 :

  • 确保使用HTTPS_PROXY的http url。
  • 在Dockerfile本身中使用小写代理variables
  • /var/lib/boot2docker/profile使用两种情况下的代理variables(在我的情况下,它在/var/lib/boot2docker/profile
  • 在所有情况下,设置一个no_proxy / NO_PROXYvariables(对于.company,.sock,localhost,127.0.0.1,::1
  • 如果您的代理请求身份validation,请不要忘记在代理url中包含凭据。

加上上面的解决scheme,我们也可以在容器内做下面的事情来用apt-get来安装

在VM中,通过使用后台代理设置在容器中运行映像时,安装docker之后

docker运行-it ubuntu:14.04

apt-get install wget

这个命令将无法从apt-get中取出包,使用下面的命令来执行此操作

docker run -it –net = host ubuntu:14.04

export http_proxy =“proxy:port”

apt-get install wget