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.
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
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?
The Jenkins project provides Docker images for controllers (and more). Beginning with Jenkins 2.344 released April 18, 2022 and Jenkins 2.332.
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.
Note: Docker 1.9 might help solve this:
HTTP_PROXY
)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
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-arg
s:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With