OSETC TECH

How to Configure HTTP/HTTPS Proxy for Docker on CentOS or Ubuntu Linux

This post will guide you how to configure HTTP/HTTPS Proxy for your Docker Container on CentOS or Ubuntu Linux server. How do I use docker command to pull docker images from Docker Hub when your server is behind an HTTP or HTTPS proxy server.

We have talked that how to install and use Docker container in both CentOS or Ubuntu Linux server in the previous post. If your docker server is behind an HTTP/HTTPS proxy server, you can not search or download a docker image from Docker Hub, and you will get the following error message:

Error response from daemon: Get https://index.docker.io/v1/search?q=ubuntu&n=25: dial tcp 3.91.211.1:443: connect: no route to host

How to fix this problem? You need to configure HTTPS_PROXY environment variable. And the Docker daemon uses the HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environmental variables in its startup environment to configure HTTP or HTTPS proxy behavior. Just do the following steps to configure HTTP/HTTPS Proxy for your Docker on Linux system:

#1 create a systemd drop-in directory for the docker service under /etc/systemd/system/ directory, type:

$ sudo mkdir -p /etc/systemd/system/docker.service.d

#2 create a file called http-proxy.conf under /etc/systemd/system/docker.service.d directory, then adding the following lines into this file.

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80/"

If you are behind an HTTPS proxy server, just create a file called https-proxy.conf under /etc/systemd/system/docker.service.d directory, then adding the following lines into this file:

[Service]
Environment="HTTPS_PROXY=https://proxy.example.com:443/"

save and close the file.

#3 Flush all above changes in your system, type:

$ sudo systemctl daemon-reload

#4 you need to restart the Docker service, type:

$ sudo systemctl restart docker

#5 You can verify if the HTTP/HTTPS proxy configuration has been loaded with the below command:

$ systemctl show --property=Environment docker

Outputs:

Environment=HTTP_PROXY=http://proxy.example.com:80/

#6 search or download a Ubuntu docker image using docker command with the search or pull sub-command, type:

$ docker search ubuntu

or

$ docker pull ubuntu

Outputs:

devops@devops-osetc:~$ docker search ubuntu
NAME                                              DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu                                            Ubuntu is a Debian-based Linux operating sys… 9341 [OK]
dorowu/ubuntu-desktop-lxde-vnc                    Docker image to provide HTML5 VNC interface … 283 [OK]
rastasheep/ubuntu-sshd                            Dockerized SSH service, built on top of offi… 209 [OK]
consol/ubuntu-xfce-vnc                            Ubuntu container with "headless" VNC session… 166 [OK]
ubuntu-upstart                                    Upstart is an event-based replacement for th… 96 [OK]
ansible/ubuntu14.04-ansible                       Ubuntu 14.04 LTS with ansible 96 [OK]
neurodebian                                       NeuroDebian provides neuroscience research s… 56 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
ubuntu-debootstrap                                debootstrap --variant=minbase --components=m… 40 [OK]
nuagebec/ubuntu                                   Simple always updated Ubuntu docker images w… 23 [OK]
i386/ubuntu                                       Ubuntu is a Debian-based Linux operating sys… 17
1and1internet/ubuntu-16-apache-php-7.0            ubuntu-16-apache-php-7.0 13 [OK]
ppc64le/ubuntu                                    Ubuntu is a Debian-based Linux operating sys… 12
eclipse/ubuntu_jdk8                               Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 8 [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 7 [OK]
darksheer/ubuntu                                  Base Ubuntu Image -- Updated hourly 5 [OK]
codenvy/ubuntu_jdk8                               Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 5 [OK]
pivotaldata/ubuntu                                A quick freshening-up of the base Ubuntu doc… 2
1and1internet/ubuntu-16-sshd                      ubuntu-16-sshd 1 [OK]
smartentry/ubuntu                                 ubuntu with smartentry 1 [OK]
paasmule/bosh-tools-ubuntu                        Ubuntu based bosh-cli 1 [OK]
1and1internet/ubuntu-16-php-7.1                   ubuntu-16-php-7.1 1 [OK]
ossobv/ubuntu                                     Custom ubuntu image from scratch (based on o… 0
pivotaldata/ubuntu-gpdb-dev                       Ubuntu images for GPDB development 0
1and1internet/ubuntu-16-healthcheck               ubuntu-16-healthcheck 0 [OK]

devops@devops-osetc:~$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
Digest: sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210
Status: Image is up to date for ubuntu:latest