{"id":456,"date":"2016-02-05T19:28:55","date_gmt":"2016-02-05T11:28:55","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=456"},"modified":"2018-09-04T22:50:17","modified_gmt":"2018-09-04T14:50:17","slug":"ensuring-docker-can-run-properly-behind-your-http-proxy-on-ubuntu-14-0-4","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/ensuring-docker-can-run-properly-behind-your-http-proxy-on-ubuntu-14-0-4\/","title":{"rendered":"Ensuring docker can run properly behind your HTTP proxy on Ubuntu 14.0.4"},"content":{"rendered":"<p>I was trying to setup docker on my Ubuntu server as part of a recent project. In order to do so, I followed <a target=\"_blank\" title=\"Docker reference to install docker engine on Ubuntu\" href=\"https:\/\/docs.docker.com\/engine\/installation\/ubuntulinux\/\">the guide provided by docker<\/a>.<\/p>\n<p>After successfully installing docker on my Ubuntu Server, I ran the following command to test my docker installation:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo docker run hello-world\r\n<\/pre>\n<p>However, I got the following response:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nUnable to find image 'hello-world:latest' locally\r\nPulling repository docker.io\/library\/hello-world\r\nNetwork 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.\r\n<\/pre>\n<p>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 <a href=\"http:\/\/hub.docker.com\" target=\"_blank\" title=\"Link to Docker Hub\">Docker Hub<\/a>. <\/p>\n<h3>Why does my docker installation face internet connection issues?<\/h3>\n<p>My Ubuntu server is sitting behind a corporate proxy server and I had not provided the relevant proxy settings to docker.  When I ran <code>sudo docker run hello-world<\/code>, the shell was using the docker client to get the docker daemon to run the hello-world image. <\/p>\n<p>The docker daemon then realized that there was no hello-world image in its local image repository and connected to <a href=\"http:\/\/hub.docker.com\" target=\"_blank\" title=\"Link to Docker Hub\">Docker Hub<\/a> in order to download the hello-world image. Since it is the docker daemon that connects to <a href=\"http:\/\/hub.docker.com\" target=\"_blank\" title=\"Link to Docker Hub\">Docker Hub<\/a>, I would need to ensure that the docker daemon was able to use my corporate proxy settings.<\/p>\n<p>As the docker daemon was created via <code>sudo service docker start<\/code>, <strong>the HTTP proxy settings should be applied to the environment which the docker daemon runs in.<\/strong> Hence, I should be setting the proxy settings via the <code>\/etc\/default\/docker<\/code> script as this is what the docker daemon will execute when it starts running on my Ubuntu server.<\/p>\n<h3>Setting http_proxy environment variable at \/etc\/default\/docker<\/h3>\n<p>To set the http_proxy environment variable, I first run <code>sudo vi \/etc\/default\/docker<\/code> to get my editor to open up the <code>\/etc\/default\/docker<\/code> script for editing. I then add the following command into the script: <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nexport http_proxy='http:\/\/username:password@proxy-host:proxy-port'\r\n<\/pre>\n<p>After saving the changes made to the <code>\/etc\/default\/docker<\/code>, I ran <code>sudo service docker restart<\/code> to get the docker daemon to pick up the proxy settings.<\/p>\n<h3>Verifying that my docker daemon can connect to Docker Hub<\/h3>\n<p>Once the docker daemon restarted successfully, I ran <code>sudo docker run hello-world<\/code> again. This time, I got the following response:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nUnable to find image 'hello-world:latest' locally\r\nlatest: Pulling from library\/hello-world\r\nb901d36b6f2f: Pull complete\r\n0a6ba66e537a: Pull complete\r\nDigest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7\r\nStatus: Downloaded newer image for hello-world:latest\r\n\r\nHello from Docker.\r\nThis message shows that your installation appears to be working correctly.\r\n\r\nTo generate this message, Docker took the following steps:\r\n 1. The Docker client contacted the Docker daemon.\r\n 2. The Docker daemon pulled the &quot;hello-world&quot; image from the Docker Hub.\r\n 3. The Docker daemon created a new container from that image which runs the\r\n    executable that produces the output you are currently reading.\r\n 4. The Docker daemon streamed that output to the Docker client, which sent it\r\n    to your terminal.\r\n\r\nTo try something more ambitious, you can run an Ubuntu container with:\r\n $ docker run -it ubuntu bash\r\n\r\nShare images, automate workflows, and more with a free Docker Hub account:\r\n https:\/\/hub.docker.com\r\n\r\nFor more examples and ideas, visit:\r\n https:\/\/docs.docker.com\/userguide\/\r\n<\/pre>\n<p>With this response, I concluded that my proxy settings were successfully applied to my docker daemon.<\/p>\n<h3>Setting http proxy settings in Dockerfile<\/h3>\n<p>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 <code>Dockerfiles<\/code>. This is because when docker build images, the environment is based upon the base image that is specified in the first line of my <code>Dockerfile<\/code>.<\/p>\n<p>For instance, the follow <code>Dockerfile<\/code> does not take my corporate http proxy settings into account:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFROM ubuntu:14.04\r\nRUN apt-get update &amp;&amp; apt-get install -y \\\r\n\tgit \\\r\n        nodejs \\\r\n        npm\r\n<\/pre>\n<p>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 <code>ENV<\/code> command to define the http_proxy environment variable after the <code>FROM<\/code> command:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFROM ubuntu:14.04\r\nENV http_proxy http:\/\/username:password@proxy-host:proxy-port\r\nRUN apt-get update &amp;&amp; apt-get install -y \\\r\n\tgit \\\r\n        nodejs \\\r\n        npm\r\n<\/pre>\n<p>With that, I was able to build my docker image from my <code>Dockerfile<\/code>, behind the corporate proxy server.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-7m\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-7m&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-7m&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F456&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>I was trying to setup docker as part of a recent project. I had followed <a target=\"_blank\" title=\"Docker reference to install docker engine on Ubuntu\" href=\"https:\/\/docs.docker.com\/engine\/installation\/ubuntulinux\/\">the guide provided by docker<\/a> to set up docker on my Ubuntu server.<\/p>\n<p>After successfully installing docker on my Ubuntu Server, I was not able to run sudo docker run hello-world successfully.<\/p>\n","protected":false},"author":1,"featured_media":1239,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[221,223,195],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Docker-logo-01.png","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-7m","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/456"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=456"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1239"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}