Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Building Docker images works in a desktop without a problem. Installing Node.js NPM dependencies work as usual. However, when using a continuous integration server such as Jenkins that is hosted behind a corporate proxy, the build Docker Images fail.

Node.js NPM Dependencies

While building Node.js pacakges, the command npm install fails when it cannot connect to GIT while cloning GIT dependencies.

e1ce5e8407d1: Already exists Status: Image is up to date for node:0.10.33  ---> e1ce5e8407d1 Step 1 : RUN mkdir -p /usr/src/app  ---> Using cache  ---> 965cad0c68b0 Step 2 : WORKDIR /usr/src/app  ---> Using cache  ---> 4c498f0c07e9 Step 3 : COPY package.json /usr/src/app/  ---> b0662a8275fb Removing intermediate container 5aca20551452 Step 4 : RUN npm install  ---> Running in 7ccf9e5362af npm WARN package.json [email protected] No README data npm WARN package.json Dependency 'async-cache' exists in both dependencies and devDependencies, using 'async-cache@^0.1.5' from dependencies npm ERR! git clone https://github.com/npm/npm2es.git Cloning into bare repository '/root/.npm/_git-remotes/https-github-com-npm-npm2es-git-60a75edb'... npm ERR! git clone https://github.com/npm/npm2es.git fatal: unable to access 'https://github.com/npm/npm2es.git/': Failed to connect to github.com port 443: Connection timed out 

Java Maven, Ruby, Go Docker Images with Dependencies

The same occurs when building Java, Ruby or Go containers, where dependencies are located in repository servers across your corporate Proxy server.

Knowing that you can configure Docker with HTTP_PROXY environment variable, how to properly configure Docker to properly build images in CI environments?

like image 262
Marcello de Sales Avatar asked Jan 02 '15 22:01

Marcello de Sales


People also ask

Can Jenkins build Docker images?

The Jenkins project provides Docker images for controllers (and more). Beginning with Jenkins 2.344 released April 18, 2022 and Jenkins 2.332.

How do I pull a Docker image behind a proxy?

To configure Docker to work with a proxy you need to add the HTTPS_PROXY / HTTP_PROXY environment variable to the Docker sysconfig file ( /etc/sysconfig/docker ). The Docker repository (Docker Hub) only supports HTTPS.


2 Answers

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 <<MARK FROM busybox RUN <command that need http_proxy> ARG --description="foo's description" foo USER $foo MARK 
like image 69
VonC Avatar answered Sep 22 '22 02:09

VonC


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:8080 ENV http_proxy=http://proxy-us02.company.com:8080 ENV HTTP_PROXY=http://proxy-us02.company.com:8080 ENV HTTPS_PROXY=http://proxy-us02.company.com:8080 ENV 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 
like image 38
jeremysprofile Avatar answered Sep 21 '22 02:09

jeremysprofile