216 views

Setting up a proxy in Docker

Docker is a widely-used platform that packages applications into containers, streamlining development and deployment processes. Each Docker container includes not only the application but also its execution environment - a virtualized layer with just the essential operating system processes, a server, and other necessary files. This containerization ensures that an application can easily run across different environments, from a local Windows computer to a MacBook and then onto a Linux server, without the need for extensive infrastructure adjustments or project rebuilding.

For developers using Docker, there may be instances where changing network settings is crucial, particularly for testing an application across different geographical locations or to circumvent ISP restrictions. Setting up a proxy within Docker is an effective solution to these challenges. Below, we provide step-by-step instructions on how to configure a proxy in Docker, enabling flexible and controlled network access for your containerized applications.

Video tutorial for proxy configuration in Docker

How to set up a proxy in Docker

Docker has a built-in proxy client. There are two ways to set network settings: by creating a configuration file for the Docker client and through the command line.

Step-by-step proxy setup in Docker via configuration file:

  1. Navigate to the ~/.docker/config.json file on your system. If this file doesn’t exist, you’ll need to create it.
  2. Open the file with a text editor or a code editor like Notepad. Add the following lines to the file:
  3. {

    "proxies": {

    "default": {

    "httpProxy": "http://proxy.example.com:Port",

    "httpsProxy": "https://proxy.example.com:Port",

    "noProxy": "*.test.example.com,.example.org,127.0.0.0/8"

    }

    }

    }

  4. Replace “http://proxy.example.com:Port” with the actual IP address and port of your proxy server. Note that Docker’s built-in client doesn’t support proxy authentication via username and password. For a seamless experience, it's advisable to use private proxies with IP authorization.
  5. Once you save the changes in the config.json file, these proxy settings will apply to all new Docker containers and any assemblies downloaded from the Docker Hub repository.

In the configuration, you can set various parameters:

  • httpProxy - configures a non-encrypted proxy. Set the HTTP_PROXY environment variable for this.
  • httpsProxy - configures a proxy with SSL encryption. Use the HTTPS_PROXY environment variable.
  • ftpProxy - manages FTP sessions. Assign this with the FTP_PROXY environment variable.
  • noProxy - creates a list of exceptions that bypass the proxy. Use the NO_PROXY environment variable for this.

Please note that as of the latest platform version, Docker does not support SOCKS proxy.

Setting up a proxy via the command line

Setting up a proxy in Docker can also be accomplished directly from the command line using the docker build and docker run commands along with the -env flag. This method provides a quick and efficient way to configure proxy settings for your Docker containers.

The general syntax for registering a proxy is PROTOCOL_PROXY="IP-address:Port". To set up an HTTP proxy, for example, the commands would be structured as follows:

docker build --build-arg HTTP_PROXY="http://proxy.example.com:3128"

docker run --env HTTP_PROXY="http://proxy.example.com:3128" redis

With just these two commands, you can effectively configure a proxy in Docker. The platform also supports SSL-encrypted proxies, which enhances network security, especially when testing applications. This feature is particularly valuable for ensuring secure and reliable internet connectivity within Docker environments.