Docker: Install Jenkins for CI server

This article will describe running Jenkins on Docker.

1 Run Jenkins via HTTP

This article will describe running Jenkins via HTTP.

  • Jenkins container uses 8080/tcp for HTTP service. Map 8080/tcp of Jenkins container to 80/tcp of host machine.
  • For persistent storage, map jenkins_home volume to JENKINS_HOME directory which uses for Jenkins data (Default value is /var/jenkins_home).

Run Jenkins container.

$ docker run -d --name jenkins \
         -p 80:8080 \
         -v jenkins_home:/var/jenkins_home \
         jenkins/jenkins:lts

Jenkins first password is stored to ${JENKINS_HOME}/secrets/initialAdminPassword. You need to wait Jenkins container initialization for accessing this file.

$ while : ; do
    docker cp jenkins:/var/jenkins_home/secrets/initialAdminPassword . \
           > /dev/null 2>&1 && break
    sleep 10;
  done
$ cat initialAdminPassword
$ rm -f initialAdminPassword

Access the following URL with IP address or hostname of host machine.

http://<ipaddr>

2 Run Jenkins via HTTPS

This article will describe running Jenkins via HTTPS.

  • Use 8443/tcp for HTTPS service of Jenins. The lower port will be failed to bind. Map 8443/tcp of Jenkins container to 443/tcp of host machine.
  • Create SSL certification files for HTTPS with debian container and stored these to jenkins_home volume.

Define variables.

$ JENKINS_HOME="/var/jenkins_home"
$ JENKINS_OPTS="--httpPort=-1"
$ JENKINS_OPTS="${JENKINS_OPTS} --httpsPort=8443"
$ JENKINS_OPTS="${JENKINS_OPTS} --httpsPrivateKey=${JENKINS_HOME}/jenkins.key"
$ JENKINS_OPTS="${JENKINS_OPTS} --httpsCertificate=${JENKINS_HOME}/jenkins.pem"

Create SSL certification files with debian container's openssl and store these to jenkins_home volume.

$ docker run --rm \
         -v jenkins_home:${JENKINS_HOME} \
         debian:jessie \
         /bin/sh -c "
set -e
apt update -y
apt install -y openssl
cd ${JENKINS_HOME}
openssl genrsa -out jenkins.key
cat <<EOF | openssl req -new -key jenkins.key -out jenkins.crt
AU
Some-State
city
company
section




EOF
openssl x509 -req -days 9999 -in jenkins.crt -signkey jenkins.key \
        -out jenkins.pem
"

Run Jenkins container.

$ docker run -d --name jenkins \
         -p 443:8443 \
         -e JENKINS_HOME=${JENKINS_HOME} \
         -e JENKINS_OPTS="${JENKINS_OPTS}" \
         -v jenkins_home:${JENKINS_HOME} \
         jenkins/jenkins:lts

Jenkins first password is stored to ${JENKINS_HOME}/secrets/initialAdminPassword. You need to wait Jenkins container initialization for accessing this file.

$ while : ; do
    docker cp jenkins:/var/jenkins_home/secrets/initialAdminPassword . \
           > /dev/null 2>&1 && break
    sleep 10;
  done
$ cat initialAdminPassword
$ rm -f initialAdminPassword

Access the following URL with IP address or hostname of host machine.

https://<ipaddr>

0001_Jenkins.png