2

For the past few days I’ve been scratching my head trying to get the Jenkins docker image to work

The issue appears to be a proxy issue.

OS: Ubuntu 16.04 LTS

Running behind work proxy

Docker version 19.03.5, build 633a0ea838

Jenkins:https://hub.docker.com/_/jenkins/

When I run the following command, I can’t access jenkins on localhost:7000

docker run --name myjenkins -p 7000:8080 -p 50000:50000 -v /var/jenkins_home jenkins

However I can with the following command

docker run --name myjenkins -p 7000:8080 -p 50000:50000 --env HTTP_PROXY="http:// localhost:8080" --env HTTPS_PROXY="https:// localhost:8080" -v /var/jenkins_home jenkins

The issue is, with the second command, I can access the Jenkins but I can’t install plugins and it asks me to configure the proxy Setting the proxy in the UI didn’t work

My understanding is that Jenkins look for port 8080 by default and because in the command above I’m using port 7000, this is preventing Jenkins from connecting to the internet (correct me if I’m wrong)

So what I did was download the Dockerfile for Jenkins and changed the port from 8080 to 7000

Dockerfile

I am now attempting to build the image with the following command

docker build --tag jenkins:1.0 .

I get the following error

curl: (7) Failed to connect to github. com port 443: Connection timed out
The command '/bin/sh -c curl -fsSL https:// github. com/krallin/tini/releases/download/v${TINI_VERSION}/tini-static-amd64 -o /bin/tini && chmod +x /bin/tini && echo "$TINI_SHA /bin/tini" | sha256sum -c -' returned a non-zero code: 7

I suspect, this is a proxy issue and I did put proxy arguments in the Dockerfile but not sure if this is what I need to do or whether I’m doing it correctly

Any idea what could be the solution?

Thank you

4
  • 1
    You have -p 7000:8000 in your first command - is it a typo or a cause of your problem?
    – taleodor
    Commented May 16, 2020 at 0:02
  • It's not a typo, I want 7000 as 8080 is already in use, hence me changing the Dockerfile default port from 8080 to 7000
    – Sgr
    Commented May 16, 2020 at 11:13
  • I meant 8000 instead of 8080.
    – taleodor
    Commented May 16, 2020 at 11:53
  • yes, sorry, it's a typo
    – Sgr
    Commented May 16, 2020 at 12:25

1 Answer 1

3

I believe that the problem lies on your proxy environment variables.

$ docker run \
    --name myjenkins \
    -p 7000:8080 \
    -p 50000:50000 \
    --env HTTP_PROXY="http:// localhost:8080" \
    --env HTTPS_PROXY="https:// localhost:8080" \
    -v /var/jenkins_home \
    jenkins

The values of $HTTP_PROXY and $HTTP_PROXY are not supposed to have space characters on them and the protocol should be http. Additionally, although setting $HTTP_PROXY is useful for compatibility with other applications running inside the container, curl expects the HTTP proxy environment variable to be written in lowercase, that is, $http_proxy. From curl (1) manual page:

Environment

The environment variables can be specified in lower case or upper case. The lower case version has precedence. http_proxy is an exception as it is only available in lower case.

http_proxy [protocol://]<host>[:port] Sets the proxy server to use for HTTP.

HTTPS_PROXY [protocol://]<host>[:port] Sets the proxy server to use for HTTPS.

FTP_PROXY [protocol://]<host>[:port] Sets the proxy server to use for FTP.

ALL_PROXY [protocol://]<host>[:port] Sets the proxy server to use if no protocol-specific proxy is set.

NO_PROXY <comma-separated list of hosts> list of host names that shouldn't go through any proxy. If set to a asterisk '*' only, it matches all hosts.

Therefore the docker run command might be written as below:

$ docker run \
    --name myjenkins \
    -p 7000:8080 \
    -p 50000:50000 \
    --env HTTP_PROXY="http://localhost:8080" \
    --env http_proxy="http://localhost:8080" \
    --env HTTPS_PROXY="http://localhost:8080" \
    --env https_proxy="http://localhost:8080" \
    -v /var/jenkins_home \
    jenkins

However, my solution will certainly not work because the port 8080 inside the container is being used by Jenkins and not by a HTTP proxy. Please, could you provide more information on how your work proxy is set up so I can edit my answer with more accurate details? Are you running a HTTP proxy such as squid or cntlm in your computer that receives requests from applications running in your workstation and forwards them to the corporate proxy after authentication? If so, you will need to adjust container's network configuration according to Stack Overflow question From inside of a Docker container, how do I connect to the localhost of the machine?.


EDIT: According to comments, a cntlm instance running locally on port 8080/tcp provides a HTTP proxy service that local applications must use in order to access internet. I believe that the least complex way to expose it to Jenkins container is by specifying --network=host parameter and sharing the root network namespace with the Jenkins container. However, Jenkins is set to listen the port 8080/tcp by default, therefore it must be instructed to listen to an alternative port, such as 7000/tcp. That can be achieved by passing --httpPort=# command line argument to Jenkins via JENKINS_OPTS environment variable.

EDIT 2: While analyzing the repository code, I've realized that Jenkins is a Java application and I am not sure that JVM recognizes $http_proxy and $https_proxy environment variables properly. So, to ensure that Jenkins will use cntlm as HTTP proxy, I also suggest passing proxy configuration properties to the JVM via the JAVA_OPTS environment variable.

EDIT 3: The Docker image jenkins is deprecated in favor of the jenkins/jenkins:lts one. The former image is currently unmaintained and seems to contain a version that is outdated and incompatible with plugins being downloaded.

To sum up, the docker run command have to be written as below for Jenkins to work in your environment:

$ docker run \
    --name myjenkins \
    --network=host \
    --env HTTP_PROXY="http://localhost:8080" \
    --env http_proxy="http://localhost:8080" \
    --env HTTPS_PROXY="http://localhost:8080" \
    --env https_proxy="http://localhost:8080" \
    --env JAVA_OPTS="-Dhttp.proxyHost=localhost -Dhttp.proxyPort=8080 -Dhttps.proxyHost=localhost -Dhttps.proxyPort=8080" \
    --env JENKINS_OPTS="--httpPort=7000" \
    -v /var/jenkins_home \
    jenkins/jenkins:lts

Passing --env JAVA_OPTS="-Djava.net.useSystemProxies=true" instead might work. I am not able to perform a try, though.

6
  • Thank you for your help. The spacing it's just some formatting error during the copy and paste. I've tried to run the container with lowercase environment variables but still not any luck. I get the same plugin error as above as well as not being able to configure the proxy within the Jenkins UI. I connect through Netscaler, it's a cntlm VPN I believe. I connect using a password and authentication code. I've tried the solution on the link you've provided, using --network="host", but it didn't work
    – Sgr
    Commented May 20, 2020 at 11:45
  • @Sgr Could you recheck my answer? I believe that the edited one will work for you. Commented May 20, 2020 at 13:58
  • Thank you, that worked like a charm. It's a bit of a kill having so many proxy settings but if I understand you correctly, this is what you've done: set network=="host" so that the container can use docker host cntlm to get to internet. 2)set http/https proxy in the docker environment so that curl can connect to hos. 3) set proxy settings for java/jenkins so that jenkins can connect the internet as well as use port=7000. I'll now try to put all these settings in a Dockerfile
    – Sgr
    Commented May 21, 2020 at 19:14
  • Thanks for you feedback. You've understood correctly. Another change that I made and is actually not related to proxy environment is the reference to the image jenkins/jenkins:lts instead of jenkins. Because the latter has been deprecated, it uses a version that is outdated and incompatible with plugins being downloaded. Commented May 21, 2020 at 20:46
  • Did --env JAVA_OPTS="-Djava.net.useSystemProxies=true" work for you? Commented May 21, 2020 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.