Docker

Pull Proxy in Docker

An HTTP proxy is an intermediate server between a client (e.g., a web browser or a Docker container) and a web server. It acts as a gateway which forwards the client requests to the web server and receives the responses from the server before sending them back to the client. You might need an HTTP proxy for various reasons, and it can serve several functions such as caching, security, load balancing, and more.

This tutorial quickly shows you how to configure the Docker if you are behind an HTTP proxy such as Nginx or HAProxy.

Configure the Docker Client to Use Proxy

The first and most common method that you can use to configure Docker to use a proxy is adding the configuration of the Docker client in the ~/.docker/config.json.

Edit the file and add the configuration as follows:

{
 "proxies"
: {
   "default"
: {
     "httpProxy"
: "http://proxy.example.com:3128",
     "httpsProxy"
: "https://proxy.example.com:3129",
     "noProxy"
: "*.test.example.com,.example.org,127.0.0.0/8"
   }
 }
}

Replace the given configuration with the details of your Proxy servers.

Save and close the file. This forces the Docker client to reload the changes that are defined in the file and apply the configuration. Keep in mind that the changes apply to new containers. This does not affect the existing containers.

Configure the Proxy Settings Per Daemon

You can also configure the proxy usage in Docker in the daemon configuration file. Edit the “/etc/docker/daemon.json” file and add the properties of the proxies as follows:

{
 "proxies"
: {
   "default"
: {
     "httpProxy"
: "http://proxy.example.com:3128",
     "httpsProxy"
: "https://proxy.example.com:3129",
     "noProxy"
: "*.test.example.com,.example.org,127.0.0.0/8"
   },
   "tcp://docker-daemon1.example.com"
: {
     "noProxy"
: "*.internal.example.net"
   }
 }
}

Similarly, ensure to configure the details for your Proxy server in the provided parameters.

Configure Proxy Using the CLI

You can also set the proxy configuration in the command-line instead of using the Docker daemon. To set the proxy within the command-line, use the –env flags as shown in the following command:

$ docker run --env HTTP_PROXY="http://proxy.example.com:3000" mysql

You can also use the –build-env parameter instead as follows:

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

Conclusion

In this tutorial, we learned how to quickly configure the proxy settings for the Docker Engine using the daemon configuration file or the CLI.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list