如何在代理后面的 dockerfile 中运行 `apt-get`? [英] How do you run `apt-get` in a dockerfile behind a proxy?

查看:39
本文介绍了如何在代理后面的 dockerfile 中运行 `apt-get`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 docker(版本 0.8.1,构建 a1598d1)运行虚拟机(Ubuntu 13.10).我正在尝试使用 dockerfile 构建图像.首先,我想更新包(使用下面的代码 - 代理被混淆)但 apt-get 超时并出现错误:Could not resolve 'archive.ubuntu.com'.

I am running a virtual machine (Ubuntu 13.10) with docker (version 0.8.1, build a1598d1). I am trying to build an image with a dockerfile. First, I want to update the packages (using the code below - the proxy is obfuscated) but apt-get times out with the error: Could not resolve 'archive.ubuntu.com'.

FROM ubuntu:13.10
ENV HTTP_PROXY <HTTP_PROXY>
ENV HTTPS_PROXY <HTTPS_PROXY>
RUN export http_proxy=$HTTP_PROXY
RUN export https_proxy=$HTTPS_PROXY
RUN apt-get update && apt-get upgrade

我还在主机系统中运行了以下内容:

I have also run the following in the host system:

sudo HTTP_PROXY=http://<PROXY_DETAILS>/ docker -d &

主机能够毫无问题地运行 apt-get.

The host is able to run apt-get without issue.

如何更改 dockerfile 以允许它从容器内访问 ubuntu 服务器?

How can I change the dockerfile to allow it to reach the ubuntu servers from within the container?

更新

我在 CentOS 中运行代码(将 FROM ubuntu:13.10 更改为 FROM centos)并且运行良好.好像是 Ubuntu 的问题.

I ran the code in CentOS (changing the FROM ubuntu:13.10 to FROM centos) and it worked fine. It seems to be a problem with Ubuntu.

推荐答案

更新:

您在 ENV 中环境变量的大小写错误.正确的是http_proxy.你的例子应该是:

You have wrong capitalization of environment variables in ENV. Correct one is http_proxy. Your example should be:

FROM ubuntu:13.10
ENV http_proxy <HTTP_PROXY>
ENV https_proxy <HTTPS_PROXY>
RUN apt-get update && apt-get upgrade

FROM centos
ENV http_proxy <HTTP_PROXY>
ENV https_proxy <HTTPS_PROXY>
RUN yum update 

在 ENV 中指定的所有变量都被添加到每个 RUN 命令之前.每个 RUN 命令都在自己的容器/环境中执行,因此它不会从以前的 RUN 命令继承变量!

All variables specified in ENV are prepended to every RUN command. Every RUN command is executed in own container/environment, so it does not inherit variables from previous RUN commands!

注意:没有必要调用带有代理的 docker daemon 来让它工作,但如果你想拉图像等,你也需要为 docker deamon 设置代理.您可以在Ubuntu的/etc/default/docker中为daemon设置代理(不影响容器设置).

Note: There is no need to call docker daemon with proxy for this to work, although if you want to pull images etc. you need to set the proxy for docker deamon too. You can set proxy for daemon in /etc/default/docker in Ubuntu (it does not affect containers setting).

此外,如果您在主机上运行代理(即本地主机,127.0.0.1),也会发生这种情况.主机上的 localhost 与容器中的 localhost 不同.在这种情况下,您需要使用另一个 IP(如 172.17.42.1)来绑定您的代理,或者如果您绑定到 0.0.0.0,您可以在 docker 期间使用 172.17.42.1 而不是 127.0.0.1 来连接容器构建.

Also, this can happen in case you run your proxy on host (i.e. localhost, 127.0.0.1). Localhost on host differ from localhost in container. In such case, you need to use another IP (like 172.17.42.1) to bind your proxy to or if you bind to 0.0.0.0, you can use 172.17.42.1 instead of 127.0.0.1 for connection from container during docker build.

您也可以在此处查找示例:如何使用缓存快速重建dockerfile?

You can also look for an example here: How to rebuild dockerfile quick by using cache?

这篇关于如何在代理后面的 dockerfile 中运行 `apt-get`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆