934 字
5 分钟

Docker Proxy Setup Guide - Mirror Acceleration and Proxy Configuration in China

Due to well-known reasons, using Docker in mainland China often results in slow or failed image pulls. This article covers two main solutions: using domestic mirror acceleration and configuring proxies.

Option 1: Domestic Mirror Acceleration#

The simplest approach is to configure a domestic mirror source. Edit the /etc/docker/daemon.json file:

{
"registry-mirrors": ["https://docker.1ms.run"]
}

However, this method is a temporary workaround and may not provide optimal speed. Some domestic mirror sources require payment:

After configuration, restart the Docker service:

Terminal window
sudo systemctl daemon-reload
sudo systemctl restart docker

Option 2: Using a Proxy#

Quick Linux Proxy Setup#

If you haven’t set up a proxy on Linux yet, the clash-for-linux-install project provides a quick deployment solution.

nelvko
/
clash-for-linux-install
Waiting for api.github.com...
00K
0K
0K
Waiting...

Installation command:

Terminal window
git clone --branch master --depth 1 https://gh-proxy.org/https://github.com/nelvko/clash-for-linux-install.git && cd clash-for-linux-install && bash install.sh

Common commands:

Terminal window
Usage:
clashctl COMMAND [OPTIONS]
Commands:
on Enable proxy
off Disable proxy
status Kernel status
proxy System proxy
ui Web panel
secret Web secret key
sub Subscription management
upgrade Upgrade kernel
tun Tun mode
mixin Mixin configuration
Global Options:
-h, --help Show help information

Understanding Proxy Address Selection#

Before configuring Docker proxy, you need to understand where your proxy is deployed:

Scenario 1: Proxy on a Remote Server (accessed via Tailscale/WireGuard)

┌─────────────────┐ VPN Network ┌─────────────────┐
│ Proxy Server │◄────────────────────►│ Local (Docker) │
│ 100.98.32.42 │ │ │
│ :7890 │ │ │
└─────────────────┘ └──────────────────┘

In this case, both the host and containers can directly use the remote proxy address 100.98.32.42:7890. Docker containers (even with bridge network) route traffic through the host, which can access the VPN network normally.

Scenario 2: Proxy Running on the Host Locally

┌─────────────────────────────────────────┐
│ Host Machine │
│ ┌─────────────┐ ┌─────────────────┐ │
│ │ Proxy │ │ Docker Container│ │
│ │ 127.0.0.1 │◄───│ (bridge network)│ │
│ │ :7890 │ │ │ │
│ └─────────────┘ └─────────────────┘ │
└─────────────────────────────────────────┘

This scenario is more complex:

  • Services on the host can directly use 127.0.0.1:7890
  • But 127.0.0.1 inside a container refers to the container itself, not the host
  • Containers need to use 172.17.0.1:7890 (docker0 bridge address) to access the proxy on the host

Docker Pull/Push Proxy Configuration#

Docker’s pull and push commands are managed by systemd, so you need to configure proxy environment variables for the Docker service.

Create the configuration directory and file:

Terminal window
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf

Add the following content (replace with your own proxy address):

[Service]
Environment="HTTP_PROXY=http://100.98.32.42:7890"
Environment="HTTPS_PROXY=http://100.98.32.42:7890"
NOTE

If the proxy is running locally on the host, you can use 127.0.0.1:7890 here since the Docker daemon runs on the host.

Reload the configuration and restart Docker:

Terminal window
sudo systemctl daemon-reload
sudo systemctl restart docker

Verify the configuration:

Terminal window
sudo systemctl show --property=Environment docker

Now docker pull will use the proxy to fetch images.

Using Proxy During Docker Build#

When building images, pass proxy settings via the --build-arg parameter:

Terminal window
docker build \
--build-arg http_proxy=http://100.98.32.42:7890 \
--build-arg https_proxy=http://100.98.32.42:7890 \
-t image_name .
NOTE

If your proxy is on a remote server (accessed via VPN), use the remote address directly. Container network traffic routes through the host, which can access proxies in the VPN network normally.

If the proxy is running locally on the host, use the docker0 bridge address:

Terminal window
docker build \
--build-arg http_proxy=http://172.17.0.1:7890 \
--build-arg https_proxy=http://172.17.0.1:7890 \
-t image_name .

Or use --network=host to let the build process use the host network directly:

Terminal window
docker build --network=host \
--build-arg http_proxy=http://127.0.0.1:7890 \
--build-arg https_proxy=http://127.0.0.1:7890 \
-t image_name .

Docker Global Proxy Configuration#

Configure ~/.docker/config.json to set proxy for all newly created containers:

Terminal window
vim ~/.docker/config.json
{
"proxies": {
"default": {
"httpProxy": "http://100.98.32.42:7890",
"httpsProxy": "http://100.98.32.42:7890",
"noProxy": "localhost,127.0.0.1,.local"
}
}
}
IMPORTANT

This configuration only affects containers created by build and run. It does not affect docker pull. For pull, you still need to configure the systemd proxy as described above.

CAUTION

Once a container is created, proxy environment variables are embedded in it. Even if you modify or delete config.json later, existing containers will still use the proxy settings from creation time. To disable proxy, manually clear environment variables inside the container:

Terminal window
export http_proxy=
export https_proxy=

Using Proxy Inside Containers#

Sometimes you need to access external networks from within a running container. Here are several common methods.

Inside the container, run:

Terminal window
export ALL_PROXY='socks5://100.98.32.42:7890'
# or
export http_proxy='http://100.98.32.42:7890'
export https_proxy='http://100.98.32.42:7890'

If the proxy is running locally on the host, use the docker0 address:

Terminal window
export ALL_PROXY='socks5://172.17.0.1:7890'

Method 2: Use Host Network Mode#

When creating the container, use the --network=host parameter:

Terminal window
docker run --network=host your_image

The container now shares the host’s network stack. If the proxy is running locally on the host, you can use 127.0.0.1:

Terminal window
export ALL_PROXY='socks5://127.0.0.1:7890'
NOTE

With --network=host, the -p port mapping parameter becomes ineffective. All container ports will be directly exposed on the host.

Method 3: Use Docker Global Proxy Configuration#

As described above, after configuring ~/.docker/config.json, newly created containers will automatically inherit proxy settings.

Network Troubleshooting#

Verify Container Can Access Proxy#

Test proxy connectivity from inside a container:

Terminal window
# Test HTTP proxy
curl -x http://100.98.32.42:7890 https://www.google.com
# Test SOCKS5 proxy
curl -x socks5://100.98.32.42:7890 https://www.google.com

Check docker0 Bridge Address#

Terminal window
ip addr show docker0

Usually it’s 172.17.0.1, but it may vary depending on configuration.

Confirm VPN Network is Reachable#

If the proxy is accessed via VPN, first confirm connectivity from the host:

Terminal window
curl -x http://100.98.32.42:7890 https://www.google.com

Proxy Address Quick Reference#

ScenarioProxy on Remote ServerProxy on Local Host
systemd configRemote address (e.g., 100.98.32.42:7890)127.0.0.1:7890
docker build (bridge)Remote address172.17.0.1:7890
docker build (host)Remote address127.0.0.1:7890
Inside container (bridge)Remote address172.17.0.1:7890
Inside container (host)Remote address127.0.0.1:7890
config.jsonRemote address172.17.0.1:7890

FAQ#

SOCKS5 Proxy Support#

Docker’s config.json currently only supports HTTP/HTTPS/FTP protocols, not SOCKS5 directly. If you’re using Clash or similar proxies with mixed port support, the same port typically supports both HTTP and SOCKS5. Just use HTTP configuration.

Temporarily Disabling Proxy#

If you have global proxy configured but need to temporarily disable it:

  1. Rename the ~/.docker/config.json file
  2. Clear environment variables inside container: export http_proxy= https_proxy=
  3. Override with --build-arg http_proxy= --build-arg https_proxy=

Summary#

RequirementRecommended Solution
Accelerate image pullssystemd proxy configuration
Network access during build--build-arg or global config
Temporary network access in containerSet ALL_PROXY environment variable
Default proxy for all containers~/.docker/config.json global config

The key is understanding where your proxy is deployed: use the remote address directly for remote proxies, and use 172.17.0.1 for local proxies when accessing from inside containers.

Docker Proxy Setup Guide - Mirror Acceleration and Proxy Configuration in China
https://catcat.blog/en/2026/01/docker-proxy-setup
作者
猫猫博客
发布于
2026-01-07
许可协议
CC BY-NC-SA 4.0