首页 文章

apt-get在公司代理后面的docker中

提问于
浏览
8

我正在尝试使用Docker在公司代理服务器后面设置开发环境 . 尽我所能,我无法让docker容器与代理服务器通信 .

代理服务器和apt-get在主机上工作正常,即Ubuntu 12.04

在Dockerfile中完成的第一件事是尝试设置代理变量:

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

它将图像拉得很好,设置了变量,但是当它进入apt-get update时,它会尝试一段时间然后失败并:

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.

我设置的这些变量与主机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机器上运行的虚拟机vm,我发现了大多数似乎不起作用的各种半生不熟的解决方案 . 无论我尝试什么,我都无法解决代理服务器的主机名

4 回答

  • 3

    问题最终出现在DNS上 . Docker在Ubuntu上运行,Ubuntu本身就是VirtualBox上的Guest OS . 由于它自己的虚拟版mumbo jumbo,它在resolv.conf中分配了一个127.0.0.1的名称服务器 .

    发生这种情况时,Docker将为自己分配一个8.8.8.8(谷歌名称服务器)的DNS名称服务器,因为localhost引用的是docker容器而不是主机 .

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

    ipconfig /all
    

    并获得我的笔记本电脑DNS服务器的IP地址 . 我使用--dns = my.dns.ip.address将这些添加到配置文件中的DOCKER_OPTS并重新启动了docker,我通过代理所采取的其他措施运行正常 .

  • 7

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

    在没有构建args的情况下构建Dockerfile失败并阻塞如下:

    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块,因为它与 archive.ubuntu.com 没有连接...你可以通过运行图像来验证...

    # 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
    
  • 4

    my own experience之后的几条评论:

    • 确保使用HTTP网址为HTTPS_PROXY .

    • Dockerfile itself中使用小写代理变量

    • docker profile中使用两种情况代理变量(在我的例子中,它在/var/lib/boot2docker/profile中)

    • 在所有实例中,设置 no_proxy / NO_PROXY 变量(到 .company,.sock,localhost,127.0.0.1,::1

    • 如果您的代理请求身份验证,请不要忘记在凭据中包含凭据 .

  • 1

    添加到上面的解决方案,我们也可以在容器内执行以下操作以使用apt-get进行安装

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

    docker run -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

相关问题