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

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

问题描述

我正在运行一个带有docker的虚拟机(Ubuntu 13.10)(版本0.8.1,构建a1598d1)。我正在尝试用dockerfile构建一个图像。首先,我想更新软件包(使用下面的代码 - 代理模糊),但是 apt-get 超出了错误:无法解决'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

我还在主机系统中运行以下操作:

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

主机能够运行 apt-get 没有问题。



如何更改docker文件以允许它从容器内到达ubuntu服务器?



更新



我在CentOS中运行代码(更改 FROM ubuntu:13.10 FROM centos ),它工作正常。这似乎是Ubuntu的一个问题。

解决方案

更新



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

  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>
运行yum更新

ENV中指定的所有变量都填入每个RUN命令。每个RUN命令都在自己的容器/环境中执行,所以它不会从以前的RUN命令继承变量!



注意:不需要使用代理来调用docker守护程序这个工作,虽然如果你想拉图像等你需要设置代码的docker deamon也。您可以在Ubuntu中的 / etc / default / docker 中设置守护程序代理(不影响容器设置)。



< hr>

此外,如果您在主机(即localhost,127.0.0.1)上运行代理,则可能会发生这种情况。主机上的本地主机与容器中的本地主机不同。在这种情况下,您需要使用其他IP(如172.17.42.1)将代理绑定到或绑定到0.0.0.0,则可以使用172.17.42.1而不是127.0.0.1从 docker build



您还可以在这里找到一个例子:如何使用缓存快速重建docker文件?


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 &

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

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

Update

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.

解决方案

UPDATE:

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

or

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

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!

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).


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.

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

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

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