【问题标题】:apt-get in docker behind corporate proxyapt-get 在公司代理后面的 docker 中
【发布时间】:2015-06-20 05:57:36
【问题描述】:

我正在尝试使用 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 机器上运行的 virtualbox vm,我发现了各种半生不熟的解决方案,这些解决方案大多似乎都不起作用。无论我尝试什么,我都无法解析代理服务器的主机名

【问题讨论】:

    标签: proxy dns docker virtualbox apt-get


    【解决方案1】:

    问题最终出在 DNS 上。 Docker 在 Ubuntu 上运行,Ubuntu 本身就是 VirtualBox 上的访客操作系统。由于它自己的虚拟化 mumbo jumbo,它在 resolv.conf 中分配了一个 127.0.0.1 的名称服务器。

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

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

    ipconfig /all
    

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

    【讨论】:

    • 先生,您是救命稻草。
    【解决方案2】:

    几个cmets,在my own experience之后:

    • 确保为 HTTPS_PROXY 使用 http url。
    • Dockerfile itself 中使用小写代理变量
    • docker profile 中使用这两种情况的代理变量(在我的情况下,它在/var/lib/boot2docker/profile 中)
    • 在所有情况下,设置no_proxy/NO_PROXY 变量(到.company,.sock,localhost,127.0.0.1,::1
    • 如果您的代理请求身份验证,请不要忘记在代理 URL 中包含凭据。

    【讨论】:

    • 在使用 http url 更改 https 代理后为我工作
    【解决方案3】:

    如果您在防火墙后面构建,则必须使用 Docker 1.9.x build-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】:

      除了上述解决方案外,我们还可以在容器内执行以下操作以使用 apt-get 进行安装

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

      docker run -it ubuntu:14.04

      apt-get 安装 wget

      此命令将无法从 apt-get 中提取软件包,请使用以下命令进行此操作

      docker run -it --net=host ubuntu:14.04

      导出 http_proxy="proxy:port"

      apt-get 安装 wget

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        • 2014-10-28
        • 1970-01-01
        • 2012-08-05
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多