Search code examples
dockerproxyenvironment-variablesaptitude

run apt-get with proxy in Dockerfile


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?


Solution

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