1

我在代理后面,我需要通过apt-get.

我带来的最好的是这个

ARG PROXY
ENV http_proxy=$PROXY
ENV https_proxy=$PROXY
RUN apt-get update -y && apt-get -y install ...
ENV http_proxy=
ENV https_proxy=

问题是我需要在之后取消设置这些环境变量。

知道如何在不到 5 层中做到这一点吗?

4

2 回答 2

5

您需要使用构建时变量(–build-arg)。

此标志允许您传递像 Dockerfile 的 RUN 指令中的常规环境变量一样访问的构建时变量。此外,这些值不会像 ENV 值那样保留在中间或最终图像中。

所以,你Dockerfile只有 3 行:

ARG http_proxy
ARG https_proxy
RUN apt-get update -y && apt-get -y install ...

您只需要定义构建时变量http_proxy和/或https_proxy在图像构建期间:

$ docker build --build-arg http_proxy=http://<proxy_ip>:<proxy_port> --build-arg https_proxy=https://<proxy_ip>:<proxy_port> . 
于 2018-02-12T20:48:52.620 回答
2

还使用构建时间变量(-build-arg),您可以在开头(之前apt-get update)添加apt 代理配置:

...
ARG APT_PROXY
RUN echo "Acquire::http::Proxy \"$APT_PROXY\";" | tee /etc/apt/apt.conf.d/01proxy
RUN apt-get update -y && apt-get -y install ...
...

这在您只想缓存来自 APT 存储库的包的情况下很有用

于 2018-02-14T16:00:16.313 回答