DEV Community


Posted on

Docker Build Proxy Setup the proxy for Dockerfile building

If we just need to pull or push the docker image, it is fine to set up the proxy by system service conf file. But this proxy setting seems don't work when building the Dockerfile. We have tried to modify the /etc/default/docker configuration, but this seems also do not work. By looking a lot of answers from StackOverflow, we finally found the right setting to set the proxy.

docker build --build-arg http_proxy= --build-arg https_proxy= . is the proxy address of my company, you may need to change according to your company proxy address and port.

You also can set the proxy in the Dockerfile.

ENV http_proxy

But this method is not recommended in my opinion, since the end-users may directly connect to the internet rather than by proxy.

Top comments (9)

dcruz1990 profile image
Dennis Quesada Cruz

Didn't work in my laptop :(

```$ sudo docker build --build-arg http_proxy= --build-arg https_proxy= -t thibaut/devdocs .
Sending build context to Docker daemon 18.3MB
Step 1/10 : FROM ruby:2.6.0
Get net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

activeliang profile image

it work for me, thx~

pyoy profile image

The proxy configuration did not take effect, so I hope we can discuss this issue.
I found that the build was connected to the nearest acceleration address in the /etc/docker/daemon.json configuration, and even after removing the daemon.json file, the proxy configuration of the build still did not take effect, and it was directly connected to the official address without accessing it through a proxy, which itself is reliable, and even switched to a LAN proxy to ensure its reliability;

The proxy configuration, whether configured in docker-compose.yml or in Dockerfile, did not take effect.

My environment I:
AlmaLinux release 8.7 (Stone Smilodon) 4.18.0-425.3.1.el8.x86_64
Docker 24.0.2
Docker Compose version v2.18.1

My environment II:
CentOS Linux release 7.9.2009 (Core) 3.10.0-1160.el7.x86_64
Docker Version: 23.0.1
docker-compose version 1.29.2

The configuration is the same for both environments:
docker-compose.yml agent configuration:

version: "2.7"
      context: . /nginx
      dockerfile: . /nginx_Dockerfile
        - http_proxy=""
        - https_proxy=""
    container_name: "nginx"
Enter fullscreen mode Exit fullscreen mode

Dockerfile proxy configuration:

cat nginx/nginx_Dockerfile 
FROM nginx:1.23.1
ENV http_proxy
ENV https_proxy
Enter fullscreen mode Exit fullscreen mode
lixiton profile image

works for me.
As a reminder: point the proxy server to your linux machine's ip. Don't use localhost because localhost refer to your docker and docker run in an isolated network. Obviously, you don't have a proxy server in it.

alexandrett profile image

I do agreed!!!! This is clearly a bad practice to use ENV to declare a proxy:
Your image becomes dependent from environment, your dockerfile could contain sensitive data...

santizo profile image


indykpol profile image

Thanks Peter, this actually worked for me.

rajitid profile image

great.. worked fine.

davidperezingeniero profile image
David Pérez Carmona

It works! Thanks.