run apt-get with proxy in Dockerfile

2019-03-03 04:35发布

I am behind a proxy an i need to install something via apt-get.

The best I came with is this

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.

Any idea how to do it in less then 5 layers?

2条回答
Evening l夕情丶
2楼-- · 2019-03-03 05:01

Using also build time variables (–build-arg), you can add at the beginning (before apt-get update) apt proxy configuration:

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

This is useful in the scenario you want to only cache packages from APT repositories

查看更多
【Aperson】
3楼-- · 2019-03-03 05:03

You need to use build-time variables (–build-arg).

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.

So, your Dockerfile is only 3 lines:

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

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> . 
查看更多
登录 后发表回答