The complete guide to set up a Docker proxy

Last Update: Nov 5, 2024

There are three types of proxies that Docker can set up. Here is a summary for your quick reference.

Daemon Proxy

The daemon proxy is used when pulling from or pushing to a registry.

For Mac, you can set the proxy for Docker Desktop in settings:

For Ubuntu, there are 2 steps:

  1. Edit /etc/docker/daemon.json

  2. Restart the daemon

For example, the daemon.json:

{ "registry-mirrors": ["https://example.com”], "proxies": { "http-proxy": "http://127.0.0.1:7890", "https-proxy": "http://127.0.0.1:7890", "no-proxy": "*.test.example.com,.example.org,127.0.0.0/8" } }

How to restart the daemon:

sudo systemctl daemon-reload sudo systemctl restart docker

Building Proxy

When building an image, for instance, if there is a RUN apt-install command in your Dockerfile and you need download something through a proxy, you will need a building proxy.

Usually, the proxy host is the host machine. If so, you can use host.docker.internal.

docker build -t example_img \   --add-host=host.docker.internal:host-gateway \   --build-arg http_proxy=http://host.docker.internal:7890 \   --build-arg https_proxy=http://host.docker.internal:7890 \ .

Note: The --build-arg passed as an argument is not the same as the ENV in a Dockerfile. Using ENV will embed the configuration into the image, which may not be what you want.

Running Proxy

If your process in container requires a proxy, for example, if your Node.js code needs to download a specific resource, you may need to run through a proxy:

docker run --rm \ --add-host=host.docker.internal:host-gateway \ --env http_proxy=http://host.docker.internal:7890 \ --env https_proxy=http://host.docker.internal:7890 \ -it ubuntu /bin/bash

For you reference, see also Docker's official docs:


That's all for now, thanks for reading, Stay Awsome!