Docker HTTP Proxy and DNS Configuration Cheat Sheet (now includes automatic HTTP Proxy detection)


This blog post provides a little cheat sheet on running a Linux host with or without a Docker client behind HTTP proxies. For Ubuntu and similar Linux distributions, we will also show how to detect the proxy available and adapt the proxy settings accordingly. This comes handy, if you are a road warrior that often switches from a network with proxy and a network with no or a different proxy without restarting the Linux host (e.g. a notebook you only place in standby or hibernate mode). This is not tested for other Linux distributions, but might work there as well.

Introduction

For setting the HTTP proxy settings of a Docker host, you can try to follow this official docker instructions on how to handle HTTP proxy configuration. As fas as I remember, those instructions have not worked on my Ubuntu Docker host (I cannot test it easily in the moment). This is, why I have dug deeper into that matter and why I have come up with this little cheat sheet.

The steps needed to connect a Docker client to Docker hub via a HTTP proxy seem to depend on the Linux distribution used. I have tested the HTTP configuration for Ubuntu and CoreOS. For most Debian-based Linux distributions, I guess, you can follow the same instructions as you find here for Ubuntu. In any case, check out the various answers and comments in this StackOverflow question, if the instructions below do not work. Feedback is highly welcome.

Ubuntu

Manual Settings

First let us detect, whether sudo is needed:

sudo echo hello > /dev/null 2>&1 && SUDO=sudo

On Ubuntu (tested with 14.04 LTS), we need to configure the file /etc/default/docker.

$SUDO vi /etc/default/docker

add proxy, if needed

export http_proxy='http://proxy.example.com:8080'
export https_proxy='http://proxy.example.com:8080'

In case your HTTP proxy requires authentication, try with:

export http_proxy='http://yourname:yourpassword@proxy.example.com:8080'
export https_proxy='http://yourname:yourpassword@proxy.example.com:8080'

In any case, you need to exchange the values for yourname, yourpassword, the proxy FQDN or IP address (proxy.example.com) and the TCP port (8080) by the values that apply to your environment.

Then:

$SUDO restart docker

then you can test it with:

$SUDO docker search ubuntu

should work now. If the proxy configuration is not correct, you will encounter an error message that looks like similar to following line:

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

Automated Settings

If you are running a Linux host on your notebook (with or without Docker) and if you often change the network environment without rebooting the Linux machine, you might find it handy to set the proxy automagically just by typing p<Enter> . This can be done using my linux_set_proxy git repository like follows:

Installation
git clone https://github.com/oveits/linux_set_proxy
cd linux_set_proxy; sudo ./install.sh
Usage

Then each time the proxy has changed, you just type:

source proxy <proxy-name_or_IP> <proxy_port>

If the proxy is not available, the proxy settings are reset automatically.

Simplified Usage

Consider you are switching between network environments with no proxy, proxy1.example.com, 10.1.1.1, proxy3.example.com and the ports 81, 8080 and 81, respectively, you just need to define an alias like follows:

alias p=source proxy proxy1.example.com 81 || source proxy 10.1.1.1 8080 || source proxy proxy3.example.com 81

If you want this definition to persist, you can add this line to your ~/.bashrc file (if bash is your favorite Linux shell).

Next time, the proxy has changed, you just need to type

p<Enter>

and the program will test, which proxy is available and

  1. adjust the settings of the HTTP_PROXY, HTTPS_PROXY environment variables, and
  2. change the Docker configuration file and restart Docker, making sure that any subsequent docker command are using the right HTTP proxy settings

CoreOS

Manual Settings

First let us detect, whether sudo is needed:

sudo echo hello > /dev/null 2>&1 && SUDO=sudo

For CoreOS, we can follow the official docker instructions:

$SUDO mkdir /etc/systemd/system/docker.service.d
$SUDO vi /etc/systemd/system/docker.service.d/http-proxy.conf

and add something like:

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/" 
Environment="NO_PROXY=localhost,127.0.0.1"

(replace the URL, so it fits to your environment)

Then:

$SUDO reboot

or if you do not want to reboot, try:

$SUDO sysctl docker restart

Automated Settings

Not supported yet.

Docker Host DNS Configuration for Vagrant

On a Vagrant based Ubuntu system, I had a problem resolving FQDNs to IP addresses. The solution was to add the following three lines into the Vagrantfile. The fix is described here:

#Vagrantfile
...
# The fix is described on http://askubuntu.com/questions/238040/how-do-i-fix-name-service-for-vagrant-client:
config.vm.provider "virtualbox" do |v|
  v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
...

One thought on “Docker HTTP Proxy and DNS Configuration Cheat Sheet (now includes automatic HTTP Proxy detection)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s