在Dockerfile中使用代理运行apt-get [英] run apt-get with proxy in Dockerfile
问题描述
我在代理后面,我需要通过apt-get
安装某些东西.
I am behind a proxy an i need to install something via 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=
问题是我之后需要取消设置这些环境变量.
The thing is that I need to unset those environment variables afterwards.
有没有想法在不到5层的时间内做到这一点?
Any idea how to do it in less then 5 layers?
推荐答案
You need to use build-time variables (–build-arg).
此标志允许您传递在Dockerfile的RUN指令中像常规环境变量一样访问的构建时变量.而且,这些值不会像ENV值那样保留在中间或最终图像中.
This flag allows you to pass the build-time variables that are accessed like regular environment variables in the RUN instruction of the Dockerfile. Also, these values don’t persist in the intermediate or final images like ENV values do.
因此,您的Dockerfile
只有3行:
ARG http_proxy
ARG https_proxy
RUN apt-get update -y && apt-get -y install ...
您只需要在图像构建过程中定义构建时变量http_proxy
和/或https_proxy
:
And you just need to define build-time variables http_proxy
and/or https_proxy
during image building:
$ docker build --build-arg http_proxy=http://<proxy_ip>:<proxy_port> --build-arg https_proxy=https://<proxy_ip>:<proxy_port> .
这篇关于在Dockerfile中使用代理运行apt-get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!