Setting up a Proxy Server on the Docker Platform
Step-by-step tutorial on how to configure a proxy server in Docker using the command line interface (CLI) and the config.json configuration file.
Олександр Л.
Proxy server configuration may be required on the Docker platform in a number of cases. The most common ones are bypassing web provider limits and testing software you developed across multiple geolocations. Let's start by looking at the concept of Docker.
What Docker is
Docker is a set of platform-as-a-service (PaaS) products that use OS-level virtualization to deliver software in packages called containers, that is, to perform containerization of packages. The service has different tiers based on payment: free and premium with advanced features. This software was released in 2013. In other words, Docker is a platform for developing, delivering, and running software, where you can separate your software from your infrastructure. Thus, the assembled container can be run on different operating systems. In addition, you will be pleased to know that the Docker platform supports SSL, which provides an excellent level of information and network security when working with it, in particular, when testing application performance. Containerized information contains:
- The software itself, the launch of which is required by the developer;
- The runtime environment — a virtual machine with a set of minimally necessary processes;
- Files required to run the software;
- Server.
Configuring a Proxy in Docker
There are 2 ways to configure it: via the command line and using a config file. Working in the command line looks like this:
- Since you will be using the
proxy.example.com:Portparameter, you need to find out the IP address of your proxy and the port used. - In the command line, write the
docker buildcommand, which creates the config. It is necessary to use the--build-argargument in it. The full command will look like this: Bashdocker build --build-arg HTTP_PROXY="http://proxy.example.com:Port" . - Then, in the same command line, enter the
docker runcommand to launch the created config. It is necessary to use the--envargument. The full command will look as follows: Bashdocker run --env HTTP_PROXY="http://proxy.example.com:Port" redis
Configuring a Docker proxy using a config file looks like this:
- In the working directory, find the
.dockerfolder. It should contain a file namedconfig.json. Create it if it is missing. - In the file, using your favorite code editor, enter the lines:
JSON
{ "proxies": { "default": { "httpProxy": "http://proxy.example.com:Port", "httpsProxy": "https://proxy.example.com:Port", "ftpProxy": "https://proxy.example.com:Port", "noProxy": "*.test.example.com,.example.org,127.0.0.0/8" } } } - In place of
proxy.example.com:Port, your IP and port data should be specified, just like in the command line example. - In the config file, as seen from the code above, you can use 4 proxy options:
- HttpProxy — for the http option with no encryption;
- HttpsProxy — for the https option with existing encryption;
- ftpProxy — for the ftp option to transfer files using ftp;
- noProxy — for routing direct traffic.
You must use similar proxy options when working with the command line, depending on what your proxy is. After saving the docker proxy config file, the information you specified will apply to all new containers and to ready-made containers downloaded from the Docker repository.
Frequently Asked Questions
How do I set up authentication (username and password) for a proxy in Docker?
If your proxies require a username and password, you must include them directly in the proxy URL inside the configuration file or command. The format is: http://username:[email protected]:Port. Note that if the password contains special characters (like @, :, /), they must be URL-encoded.
Why are current running containers not using the proxy after modifying config.json?
The configuration specified in ~/.docker/config.json is applied as environment variables only at the moment new containers are created or started. Already running containers cannot dynamically read changes from this file. For them to use the proxy, they must be stopped, removed, and started again.
How do I configure a proxy for the Docker daemon itself (to pull images)?
To allow the Docker daemon itself to pull images (e.g., executing docker pull) through a proxy, user settings in config.json are not enough. You need to configure the proxy for the system service. On Linux systems, this is done by creating a configuration file at /etc/systemd/system/docker.service.d/http-proxy.conf specifying the HTTP_PROXY and HTTPS_PROXY environment variables, and then restarting the service with systemctl restart docker.
What should I do if a container needs to access local services bypassing the proxy?
Use the noProxy parameter in the configuration file or the --env NO_PROXY="..." argument. In it, specify a comma-separated list of domain names, hosts, or subnets that should be accessed directly. Make sure to include localhost, 127.0.0.1, and your Docker internal network range to avoid breaking container-to-container communication.
Can I configure different proxies for different containers simultaneously?
Yes. The config.json file sets the default (global) settings for all containers. If you need a specific container to use a different proxy, simply override the environment variables directly during its launch using the -e HTTP_PROXY="..." and -e HTTPS_PROXY="..." flags within the docker run command. Command-line parameters take precedence.
Do SOCKS5 proxies work in Docker configuration?
Officially, Docker expects httpProxy and httpsProxy inside config.json. However, many modern tools and base images support the SOCKS5 protocol if specified in the URL scheme: socks5://proxy.example.com:Port or socks5h://... (to resolve DNS through the proxy). If a specific software inside the container does not support SOCKS, you will need to use third-party tools to convert traffic (e.g., proxychains or privoxy).
