How to build Docker Images with Dockerfile behind HTTP_PROXY by Jenkins? How to build Docker Images with Dockerfile behind HTTP_PROXY by Jenkins? docker docker

How to build Docker Images with Dockerfile behind HTTP_PROXY by Jenkins?


Note: Docker 1.9 might help solve this:

  • "Issue 14634": Builder - Build-time argument passing (e.g., HTTP_PROXY)
  • "PR 15182": Support for passing build-time variables in build context

Usage (proposed):

docker build --build-arg http_proxy=http://my.proxy.url  --build-arg foo=bar <<MARKFROM busyboxRUN <command that need http_proxy>ARG --description="foo's description" fooUSER $fooMARK


Docker has multiple ways to set proxies that take effect at different times.


If your docker build has to retrieve a base image through a proxy, you'll want to specify build-args:

docker build --build-arg HTTP_PROXY=$http_proxy \--build-arg HTTPS_PROXY=$http_proxy --build-arg NO_PROXY="$no_proxy" \--build-arg http_proxy=$http_proxy --build-arg https_proxy=$http_proxy \--build-arg no_proxy="$no_proxy" -t myContainer /path/to/Dockerfile/directory

where $http_proxy and $no_proxy were set in my bashrc. I used both HTTP_PROXY and http_proxy because different utilities will check different variables (curl checks both, wget only checks the lowercase ones, etc).


If your docker build has a RUN curl/wget/etc command that has to go through the proxy, you'll need to specify an environment variable inside your docker image:

ENV https_proxy=http://proxy-us02.company.com:8080ENV http_proxy=http://proxy-us02.company.com:8080ENV HTTP_PROXY=http://proxy-us02.company.com:8080ENV HTTPS_PROXY=http://proxy-us02.company.com:8080ENV no_proxy="localhost,localdomain,127.0.0.1,etc"ENV NO_PROXY="localhost,localdomain,127.0.0.1,etc"

If you don't want this environment variable inside your image at runtime, you can remove all these at the end:

RUN unset http_proxy https_proxy no_proxy HTTP_PROXY HTTPS_PROXY NO_PROXY


Docker Daemon HTTP Proxy

A lot of documentation is available about setting up the HTTP_PROXY environment variable for Docker's daemon. The environment variable is only available when running containers, so it won't help us here.

Solution in Dockerfile

Although setting up the environment variable HTTP_ENV or http_env in the Dockerfile might help, it does not help our cause either.

ENV http_proxy http://proxy.mycompany.com:80

The reason why is that each specific service only honors HTTP Proxy setting in a different way. The way I could solve is below.

  • NPM: NPM requires setting up the HTTP_PROXY variable using a CLI command.
  • GIT: GIT requires setting up the HTTP_PROXY variable using a CLI command as well.
  • MAVEN: MVN command requires setting up the HTTP_PROXY as an XML file under the user's directory at ~/.m2/settings.xml. For Docker, you can add it to the root's "/root/.m2/settings.xml" directory (unsafe, development-only), or to the Dockerfile's user's home directory.

For instance, running an application using Dockerfile, I can build an image using the following Dockerfile:

FROM node:0.10.33# PrepareRUN mkdir -p /usr/src/appWORKDIR /usr/src/app# Use the cache for dependenciesCOPY package.json /usr/src/app/# If building behind an http_proxy, set them for git and npmRUN git config --global http.proxy http://qypprdproxy02.ie.company.net:80 && \    npm config set proxy http://qypprdproxy02.ie.company.net:80 && \    npm config set https-proxy http://qypprdproxy02.ie.company.net:80# Install dependenciesRUN npm install# Copy all the sourceCOPY . /usr/src/app# Execute the dev stepsCOPY ./numbat-config.example.js /usr/src/app/numbat-config.jsCOPY ./.env.example /usr/src/app/.evnRUN touch /usr/src/app/config.admin.js

Note that I have configured both GIT and NPM using their CLI command to explicitly take the proxy settings before running the NPM install command. That way, both NPM and GIT dependencies will be automatically retrieved and cloned, respectively.

The result of building an image with this Dockerfile works as expected:

[root@pppdc9prd6dq newww]# fig build......Building npmregistryserver... ---> Using cache ---> 965cad0c68b0Step 2 : WORKDIR /usr/src/app ---> Using cache ---> 4c498f0c07e9Step 3 : COPY package.json /usr/src/app/ ---> ae8ff7861246Removing intermediate container ba1d7b8c9963Step 4 : RUN npm config set proxy http://qypprdproxy02.ie.company.net:80 &&     npm config set https-proxy http://qypprdproxy02.ie.company.net:80 &&     npm install ---> Running in aa6e05d9c7a4npm WARN package.json newww@2.0.0 No README datanpm WARN package.json Dependency 'async-cache' exists in both dependencies and devDependencies, using 'async-cache@^0.1.5' from dependenciesnpm WARN deprecated extend@1.1.3: Please update to the latest version.> v8flags@1.0.8 install /usr/src/app/node_modules/gulp/node_modules/v8flags> node fetch.js> hiredis@0.1.17 install /usr/src/app/node_modules/hiredis> node-gyp rebuildmake: Entering directory '/usr/src/app/node_modules/hiredis/build'  CC(target) Release/obj.target/hiredis/deps/hiredis/hiredis.o  CC(target) Release/obj.target/hiredis/deps/hiredis/net.o  CC(target) Release/obj.target/hiredis/deps/hiredis/sds.o  CC(target) Release/obj.target/hiredis/deps/hiredis/async.o  AR(target) Release/obj.target/deps/hiredis.a  COPY Release/hiredis.a  CXX(target) Release/obj.target/hiredis/src/hiredis.o  CXX(target) Release/obj.target/hiredis/src/reader.o  SOLINK_MODULE(target) Release/obj.target/hiredis.node  SOLINK_MODULE(target) Release/obj.target/hiredis.node: Finished  COPY Release/hiredis.nodemake: Leaving directory '/usr/src/app/node_modules/hiredis/build'npm WARN engine hawk@0.10.2: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"})> pngcrush-bin@1.0.0 postinstall /usr/src/app/node_modules/imagemin-pngcrush/node_modules/pngcrush-bin> node lib/install.js     fetch : https://raw.githubusercontent.com/imagemin/pngcrush-bin/v1.0.0/vendor/linux/pngcrush✔ pre-build test passed successfully!> dtrace-provider@0.3.1 install /usr/src/app/node_modules/npm-typeahead/node_modules/restify/node_modules/dtrace-provider> scripts/install.jsnpm WARN engine cryptiles@0.1.3: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"})npm WARN engine sntp@0.1.4: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"})npm WARN engine boom@0.3.8: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"})npm WARN engine hoek@0.7.6: wanted: {"node":"0.8.x"} (current: {"node":"0.10.33","npm":"2.1.11"})npm WARN cannot run in wd newww@2.0.0 gulp build (wd=/usr/src/app)newww-metrics@1.0.0 node_modules/newww-metricsmurmurhash@0.0.2 node_modules/murmurhashnpm-humans@2.0.1 node_modules/npm-humansleven@1.0.1 node_modules/levenchunk@0.0.2 node_modules/chunknpm-expansions@1.14.0 node_modules/npm-expansionssimilarity@1.0.1 node_modules/similaritytruncate@1.0.4 node_modules/truncate

This properly worked as expected and you can have a CI/CD environment behind an http proxy to rebuild images based on this Dockerfile.