Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container, how to use host proxy

Tags:

docker

proxy

Because I'm in China it is nearly impossible to use Docker Hub, Git, GitHub, npm and loads of other tools without a VPN.

I finally found how to have the Docker daemon use a proxy (share VPN in the VPN client software on the host is the proxy server).

But as soon as I run the container I'm behind the firewall again and the container is not using the host proxy. This would make it impossible to get anything from GitHub, use npm, Bower and lots of other things.

How can I force the container to use the host proxy?

like image 450
HMR Avatar asked May 25 '15 13:05

HMR


People also ask

How do I connect to Docker proxy?

If you're running Docker for Desktop this is a really simple operation. You can do this from Docker's settings Docker > Preferences > Resources > Proxies . All you need to do is provide values for the following variables: HTTP_PROXY : the proxy server endpoint to handle HTTP calls.

How do I specify proxy in Dockerfile?

We simply need to set the http_proxy and https_proxy environmental variables during build time. We can do this with the docker build command itself. $ sudo docker build -t curl --build-arg http_proxy=http://192.168.33.10:3128 . Or we can specify the http_proxy value using the ENV instruction within the Dockerfile .

What is a proxy server Docker?

docker-proxify is a docker-within-docker container that eases development when operating behind a restrictive firewall that requires a proxy server for outbound internet connectivity, by making the use of the proxy server transparent to the applications running inside the container.

Can Docker container communicate with host?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0. 0.1 ) will refer to the docker host.


2 Answers

If you don't want to use --network=host to isolate your Docker from the host's network, you need to make the host network available inside Docker.

When running docker use: (Linux, Docker version > 20.10)

--add-host=host.docker.internal:host-gateway

Say your proxy is running on port 3128, inside your docker container you need to set

http_proxy=http://host.docker.internal:3128

Also make sure that you set Gateway yes in /etc/cntlm.conf if that's the proxy you're using.

like image 158
CodeMonkey Avatar answered Sep 20 '22 12:09

CodeMonkey


I fixed it by setting the proxy in /etc/default/docker.

I also had to unset the proxy in the environment variables:

unset http_proxy
unset https_proxy
like image 23
prem Avatar answered Sep 19 '22 12:09

prem