Cloud Native 5 min read

Master Docker Proxy Setup: Daemon, Container, and Build Configurations

This guide explains how to configure HTTP/HTTPS proxies for Docker's daemon via systemd, set per‑container proxy settings through the Docker config file, and pass proxy arguments during image builds, covering common pitfalls and network isolation considerations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Docker Proxy Setup: Daemon, Container, and Build Configurations

Sometimes network constraints such as corporate NAT require using a proxy. Docker proxy configuration can be complex because it involves three scenarios, but all rely on Linux environment variables like http_proxy.

dockerd proxy

When running docker pull, the daemon dockerd performs the operation, so the proxy must be set in the daemon's environment, which is managed by systemd. Create a drop‑in directory and a proxy configuration file:

sudo mkdir -p /etc/systemd/system/docker.service.d
touch /etc/systemd/system/docker.service.d/proxy.conf

In proxy.conf add:

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/"
Environment="HTTPS_PROXY=http://proxy.example.com:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"

Replace proxy.example.com:8080 with a reachable, authentication‑free proxy (often set up locally with cntlm).

Container proxy

For containers that need internet access, configure ~/.docker/config.json. The following JSON works for Docker 17.07+:

{
  "proxies": {
    "default": {
      "httpProxy": "http://proxy.example.com:8080",
      "httpsProxy": "http://proxy.example.com:8080",
      "noProxy": "localhost,127.0.0.1,.example.com"
    }
  }
}

This user‑level configuration is convenient for personal development environments. In CI/CD pipelines or production, injecting proxy variables with -e is often preferable to avoid dependency on the host configuration.

docker build proxy

Although docker build also runs a container, it does not inherit the user‑level proxy settings. Pass proxy arguments explicitly:

docker build . \
  --build-arg "HTTP_PROXY=http://proxy.example.com:8080/" \
  --build-arg "HTTPS_PROXY=http://proxy.example.com:8080/" \
  --build-arg "NO_PROXY=localhost,127.0.0.1,.example.com" \
  -t your/image:tag

Both docker run and docker build operate in isolated networks; a proxy bound to localhost:3128 will not work unless --network host is used. Typically you need an external proxy IP and the proxy must run in gateway mode.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ProxyConfigurationnetworkContainerbuildsystemd
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.