Keywords: Docker proxy configuration | HTTP_PROXY environment variable | systemd service configuration | image download issues | corporate network environment
Abstract: This technical paper provides a comprehensive analysis of configuring Docker for successful image downloads in corporate proxy environments. Through systematic examination of common network connectivity errors, it details methods for setting HTTP/HTTPS proxies using systemd service configuration and daemon.json files. The paper explores version compatibility differences in Docker proxy configuration and offers practical steps for environment variable setup, service restart procedures, and configuration validation. Special attention is given to NO_PROXY environment variable configuration for internal registry access, ensuring readers gain complete mastery of Docker deployment best practices in complex network environments.
Problem Background and Error Analysis
In corporate network environments, users often need to access external resources through proxy servers due to security policy restrictions. Docker, as a core containerization tool, frequently encounters connection issues when pulling images through proxies. Typical errors manifest as:
Pulling repository busybox
2014/04/16 09:37:07 Get https://index.docker.io/v1/repositories/busybox/images: dial tcp: lookup index.docker.io on 127.0.1.1:53: no answer from server
This error indicates that the Docker client cannot resolve Docker Hub's domain name or establish TCP connections, fundamentally because proxy configuration hasn't been properly applied to the Docker daemon.
System-Level Proxy Configuration Solution
For Linux distributions using systemd as the initialization system (such as Ubuntu, CentOS, etc.), the most reliable solution involves configuring Docker service environment variables through systemd drop-in directories.
First, create a dedicated configuration directory:
sudo mkdir -p /etc/systemd/system/docker.service.d
Then create the proxy configuration file /etc/systemd/system/docker.service.d/http-proxy.conf:
[Service]
Environment="HTTP_PROXY=http://192.168.1.1:3128/"
Environment="HTTPS_PROXY=http://192.168.1.1:3128/"
After configuration, reload systemd configuration and restart the Docker service:
sudo systemctl daemon-reload
sudo systemctl restart docker
Verify that the configuration has taken effect:
sudo systemctl show --property Environment docker
Internal Network Exemption Configuration
In enterprise environments, access to internal Docker registries typically needs to bypass the proxy. This can be achieved using the NO_PROXY environment variable to specify direct connection addresses:
[Service]
Environment="HTTP_PROXY=http://192.168.1.1:3128/"
Environment="HTTPS_PROXY=http://192.168.1.1:3128/"
Environment="NO_PROXY=localhost,127.0.0.0/8,docker-registry.internal.com"
NO_PROXY supports multiple formats: individual hostnames, IP addresses, CIDR network segments, and wildcard domains, ensuring internal service network access remains unaffected by the proxy.
Version Compatibility Considerations
Different Docker versions exhibit varying levels of support for proxy configuration. In Docker 20.10.8 and later versions, due to the underlying migration to Go 1.16, proxy semantics have changed:
- For
https://URLs, theHTTPS_PROXYenvironment variable is now exclusively used - There is no longer fallback to
HTTP_PROXYfor HTTPS proxy configuration
This means that in newer Docker versions, both HTTP_PROXY and HTTPS_PROXY environment variables must be configured, rather than relying solely on the traditional HTTP_PROXY.
Alternative Configuration Methods
Beyond systemd configuration, Docker also supports proxy configuration through the daemon.json file, particularly in Docker 17.07 and later versions:
{
"proxies": {
"default": {
"httpProxy": "http://192.168.1.1:3128",
"httpsProxy": "http://192.168.1.1:3128",
"noProxy": "*.test.example.com,.example2.com,127.0.0.0/8"
}
}
}
This method offers the advantage of centralized configuration but requires attention to file permissions and correct file paths. Configuration files are typically located at /etc/docker/daemon.json or ~/.docker/config.json.
Troubleshooting and Validation
When proxy configuration fails to take effect, follow these troubleshooting steps:
- Verify proxy server accessibility:
curl -I http://192.168.1.1:3128 - Check if environment variables are correctly set:
sudo systemctl show --property Environment docker - Confirm configuration takes effect after Docker service restart
- For authenticated proxies, ensure correct credentials are provided
Common configuration errors include: environment variables not properly passed when using sudo commands, incorrect configuration file paths, wrong proxy server addresses or ports, etc.
Best Practices Summary
Successful Docker deployment in corporate proxy environments requires: using systemd drop-in configuration as the primary solution, setting both HTTP and HTTPS proxy environment variables, properly configuring NO_PROXY to avoid internal network proxy bypass, regularly validating configuration effectiveness, and maintaining Docker version updates to access the latest proxy support features. Through systematic configuration approaches, Docker can operate stably in complex network environments.