Search code examples
dockerproxydocker-composemitmproxy

No http/https connectivity inside docker container after proxy configuration on Docker client


I followed the Docker guide of how configurate a proxy for containers. So I put the following in ~/.docker/config.json

{
   (...),
   "proxies":{
      "default":{
         "httpProxy":"http://127.0.0.1:8080",
         "httpsProxy":"http://127.0.0.1:8080",
      }
   }
}

And runned

sudo systemctl daemon-reload

sudo systemctl restart docker

In the host, I started the proxy (mitmproxy) in the 127.0.0.1:8080 address. The proxy looks like is working correctly in the host. However, when I connect in the container it seems it could not use the proxy correctly.

docker exec -it my_container_1 /bin/bash

In the bash:

bash-5.0#  wget http://google.com
wget: can't connect to remote host (127.0.0.1): Connection refused

If I use ping or some ftp address, it works properly, so it's something restricted to the proxy configuration.

Any thoughts?


Solution

  • The http_proxy and https_proxy defined in config.json are passed by the docker client to the containers. This practically sets the http_proxy and https_proxy environment variables in the container. Running echo $http_proxy in the container will output the value.

    Assuming the proxy is configured correctly on the host, the IP address of the proxy should be the IP of the host as seen by the container. Replace in config.json 127.0.0.1 with the IP of the docker0 or main network interface (eth0) of the host.