Search code examples
dockerproxyvagranthttp-proxy

Using docker within vagrant behind a proxy


I want to run apt from within a docker container, within a vagrant machine (running on virtualbox), but this fails because I'm behind a proxy.

I use vagrant-proxyconf to allow the vagrant machine itself to connect to the internet, which works fine:

if Vagrant.has_plugin?("vagrant-proxyconf")
  config.proxy.http     = ...
  config.proxy.https    = ...
  config.proxy.no_proxy = "localhost,127.0.0.1,.example.com"
end

However, these settings aren't carried through to docker containers started within the vagrant machine. When I start a debian-based docker container with

docker run -it debian /bin/bash

and within the bash I run

apt-get update

then apt can't establish a connection. I can fix this problem by adding the following to my Dockerfile

ENV http_proxy <myproxy>

but adjusting all Dockerfile's would be cumbersome, and I'd prefer not to hardcode my proxy into the Dockerfile's themselves, as those are also used in a different setup.

I've also tried telling docker what proxy to use using: https://docs.docker.com/engine/admin/systemd/

However, this appears not to have any effect on the proxy that apt uses within the docker container.

Is there a way to pass the http_proxy environment variable to all docker containers started within my machine by default? Alternatively, would it be possible to configure vagrant / virtualbox to "emulate" a "proxyless" internet connection so that I don't have to reach the proxy settings down through all the virtualization layers?


Solution

  • You can add the vars passing them as arguments on docker build command. In that way it will work and the proxy ip won't be on Dockerfile. In this way:

    docker build -t --build-arg http_proxy="http://yourIp" yourImage 
    

    Then on Dockerfile you must set the var as argument:

    ARG http_proxy
    

    Automatically the var is able to be used in this way:

    RUN echo ${http_proxy}
    

    But in your case you don't need to use it, only setting the proxy var is enough to be used during building.

    This technique could be very useful too in order to avoid write passwords on Dockerfiles.

    Hope it helps