run apt-get with proxy in Dockerfile

女生的网名这么多〃 提交于 2019-12-02 13:43:20

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/48749200/run-apt-get-with-proxy-in-dockerfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!