Ensuring docker can run properly behind your HTTP proxy on Ubuntu 14.0.4
I was trying to setup docker on my Ubuntu server as part of a recent project. In order to do so, I followed the guide provided by docker.
After successfully installing docker on my Ubuntu Server, I ran the following command to test my docker installation:
sudo docker run hello-world
However, I got the following response:
Unable to find image 'hello-world:latest' locally Pulling repository docker.io/library/hello-world Network timed out while trying to connect to https://index.docker.io/v1/repositories/library/hello-world/images. You may want to check your internet connection or if you are behind a proxy.
This response indicated that my Ubuntu server was not able to reach docker's image repository. Without the connection to docker's image repository, I cannot continue with subsequent development operations as I will need to base my docker image off one of those that are available on Docker Hub.
Why does my docker installation face internet connection issues?
My Ubuntu server is sitting behind a corporate proxy server and I had not provided the relevant proxy settings to docker. When I ran sudo docker run hello-world
, the shell was using the docker client to get the docker daemon to run the hello-world image.
The docker daemon then realized that there was no hello-world image in its local image repository and connected to Docker Hub in order to download the hello-world image. Since it is the docker daemon that connects to Docker Hub, I would need to ensure that the docker daemon was able to use my corporate proxy settings.
As the docker daemon was created via sudo service docker start
, the HTTP proxy settings should be applied to the environment which the docker daemon runs in. Hence, I should be setting the proxy settings via the /etc/default/docker
script as this is what the docker daemon will execute when it starts running on my Ubuntu server.
Setting http_proxy environment variable at /etc/default/docker
To set the http_proxy environment variable, I first run sudo vi /etc/default/docker
to get my editor to open up the /etc/default/docker
script for editing. I then add the following command into the script:
export http_proxy='http://username:password@proxy-host:proxy-port'
After saving the changes made to the /etc/default/docker
, I ran sudo service docker restart
to get the docker daemon to pick up the proxy settings.
Verifying that my docker daemon can connect to Docker Hub
Once the docker daemon restarted successfully, I ran sudo docker run hello-world
again. This time, I got the following response:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world b901d36b6f2f: Pull complete 0a6ba66e537a: Pull complete Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7 Status: Downloaded newer image for hello-world:latest Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker Hub account: https://hub.docker.com For more examples and ideas, visit: https://docs.docker.com/userguide/
With this response, I concluded that my proxy settings were successfully applied to my docker daemon.
Setting http proxy settings in Dockerfile
As I continued on to build a image via Dockerfile, I came to realized that the proxy settings that I had applied via the /etc/default/docker did not take effect in the process of building docker images via Dockerfiles
. This is because when docker build images, the environment is based upon the base image that is specified in the first line of my Dockerfile
.
For instance, the follow Dockerfile
does not take my corporate http proxy settings into account:
FROM ubuntu:14.04 RUN apt-get update && apt-get install -y \ git \ nodejs \ npm
In the above example, the apt-get update will stall for quite sometime before reporting that there was a network problem when contacting the update sites provided by Ubuntu. In order for the proxy settings to take effect during image building, I will have to use the ENV
command to define the http_proxy environment variable after the FROM
command:
FROM ubuntu:14.04 ENV http_proxy http://username:password@proxy-host:proxy-port RUN apt-get update && apt-get install -y \ git \ nodejs \ npm
With that, I was able to build my docker image from my Dockerfile
, behind the corporate proxy server.