Containerizing Jenkins with Docker on Ubuntu

Rakesh Jain
6 min readJun 18, 2020

Requirements

  • A server running Ubuntu 18.04 with minimum 2 GB of RAM.
  • A root password is set up on your server.
  • Docker should be installed on your server

Create Docker Volume for Data and Log

You will need to create a data and log volumes to backup Jenkins data and configurations including, logs, plugins, plugin configuration and job config. When you remove any container, the data and configurations are still available in the Docker volume.

Let’s start with creating volume for data and log with the following command:

root@kmaster-rj:~/jenkins-docker# docker volume create jenkins-data
root@kmaster-rj:~/jenkins-docker# docker volume create jenkins-log

Once the volumes are created, you can list them with the following command:

root@kmaster-rj:~/jenkins-docker# docker volume ls

You should see the following output:

DRIVER VOLUME NAME
local jenkins-data
local jenkins-log

Install Jenkins with Docker

root@kmaster-rj:~/# mkdir jenkins-docker ; cd jenkins-docker
root@kmaster-rj:~/jenkins-docker# nano docker/dockerfile

Add the following lines:

FROM jenkins/jenkins
LABEL maintainer=”test-jenkins@gmail.com”
USER root
RUN mkdir /var/log/jenkins
RUN mkdir /var/cache/jenkins
RUN chown -R jenkins:jenkins /var/log/jenkins
RUN chown -R jenkins:jenkins /var/cache/jenkins
USER jenkins
ENV JAVA_OPTS=”-Xmx8192m”
ENV JENKINS_OPTS=” — handlerCountMax=300 — logfile=/var/log/jenkins/jenkins.log
— webroot=/var/cache/jenkins/war”

# ”-Xmx8192m” #it sets java heap size to (8 GB)
# --handlerCountStartup = set the no of worker threads to spawn at startup. Default is 5
# --handlerCountMax = set the max no of worker threads to allow. Default is 300
# --handlerCountMaxIdle = set the max no of idle worker threads to allow. Default is 50

Note -
We have increased the connection pool here. At Riot, we get a lot of traffic to our Jenkins server, so we’ve learned to give Jenkins a bit more breathing room.
That’ll (JAVA_OPTS and JENKINS_OPTS) give Jenkins a nice base pool of handlers and a cap.

Save and close the file, when you are finished. Then, build the Jenkins image with the following command:

root@kmaster-rj:~/jenkins-docker# docker build -t myjenkins .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM jenkins/jenkins
latest: Pulling from jenkins/jenkins
3192219afd04: Pull complete
98f2b8039916: Pull complete
d50c49db56d3: Pull complete
Digest: sha256:db3f3cb7eb2ab78abd01d0734602c03a3c95baa7739805d68b5f88af8f44289a
Status: Downloaded newer image for jenkins/jenkins:latest
— -> bfb0684d7e6c
Step 2/10 : LABEL maintainer=”rakeshrhcss@gmail.com”
— -> Running in 29365eff972f
Removing intermediate container 29365eff972f
— -> 4043108e1572
Step 3/10 : USER root
— -> Running in 25f0f862a7c3
Removing intermediate container 25f0f862a7c3
— -> a7a8eb72afd9
Step 4/10 : RUN mkdir /var/log/jenkins
— -> Running in 9d396c44b8e7
Removing intermediate container 9d396c44b8e7
— -> e048bfaa87cd
Step 5/10 : RUN mkdir /var/cache/jenkins
— -> Running in 3e7100d1468c
Removing intermediate container 3e7100d1468c
— -> 2a00c14737fd
Step 6/10 : RUN chown -R jenkins:jenkins /var/log/jenkins
— -> Running in 5affbd9e1936
Removing intermediate container 5affbd9e1936
— -> 2b17117bccf8
Step 7/10 : RUN chown -R jenkins:jenkins /var/cache/jenkins
— -> Running in b67ced2b102e
Removing intermediate container b67ced2b102e
— -> 3d587c11f967
Step 8/10 : USER jenkins
— -> Running in 8e02bd850129
Removing intermediate container 8e02bd850129
— -> d44b80e1a5df
Step 9/10 : ENV JAVA_OPTS=”-Xmx8192m”
— -> Running in 3b9660eefb10
Removing intermediate container 3b9660eefb10
— -> b3f35f9172bb
Step 10/10 : ENV JENKINS_OPTS=” — handlerCountMax=300 — logfile=/var/log/jenkins/jenkins.log — webroot=/var/cache/jenkins/war”
— -> Running in f472cc3cad80
Removing intermediate container f472cc3cad80
— -> 76927bfb4f1c
Successfully built 76927bfb4f1c
Successfully tagged myjenkins:latest

root@kmaster-rj:~/jenkins-docker# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myjenkins latest 76927bfb4f1c 3 seconds ago 659MB

Run Jenkins Container with Data and Log Volume

root@kmaster-rj:~/jenkins-docker# docker run -p 8080:8080 -p 50000:50000 — name=jenkins-master — mount source=jenkins-log,target=/var/log/jenkins — mount source=jenkins-data,target=/var/jenkins_home -d myjenkins

Verify the running container -
root@kmaster-rj:~/jenkins-docker# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e208792831c myjenkins “/sbin/tini — /usr/…” 3 seconds ago Up 1 second 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins-master

Next, you will need to check the jenkins log file whether everything is working fine or not:

root@kmaster-rj:~/jenkins-docker#docker exec jenkins-master tail -f /var/log/jenkins/jenkins.log

You should see the following output:
Please use the following password to proceed to installation:
36c06f339a704589b3f3301867770101

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************
Please note the above password, you will need it during the Jenkins web set up wizard.

You can access the password like this as well -
root@kmaster-rj:~/jenkins-docker# docker exec jenkins-master cat /var/jenkins_home/secrets/initialAdminPassword

36c06f339a704589b3f3301867770101

Access Jenkins Web Interface

Now, open your web browser and type the URL http://your-server-ip:8080. You will be redirected to the Jenkins setup screen as shown below:

Provide your administrator password and click on the Continue button. You should see the following page:

Now, click on the “Install suggested plugins” to install the required plugins. Once the installation has been finished. You should see the following page:

Now, provide your admin username, password and full name then click on the Save and Finish button. You should see the Instance configuration page Now, just click on the Save and Finish button. Once the setup completed successfully, you should see the following page:

Now, click on the “Start using Jenkins“. You will be redirected to the Jenkins dashboard as shown in the following page:

Now, click on the “create a job” button. You should see the following page:

Now, click on the Save button to save all the settings. You should see your newly created jobs in the following page:

Test Jenkins Persistent Data

Jenkins is now installed and configured. Next, you will need to test whether Jenkins data and log are still persisting after removing the Jenkins container.

To do so, first stop and delete the Jenkins container with the following command:

root@kmaster-rj:~/jenkins-docker#docker container stop jenkins-master
root@kmaster-rj:~/jenkins-docker#docker container rm jenkins-master

Now, start the Jenkins container again with the following command:

root@kmaster-rj:~/jenkins-docker#docker run -p 8080:8080 -p 50000:50000 — name=jenkins-master — mount source=jenkins-log,
target=/var/log/jenkins — mount ​source=jenkins-data,target=/var/jenkins_home -d myjenkins

Once the Jenkins container has been started, open your web browser and type the URL http://your-server-ip:8080. You will be redirected to the following page:

Now, provide your admin user name and password then click on the Sign in button. You should see the Jenkins dashboard in the following page:

That means you have preserved all the data, logs, setup configuration and plugin installs. You should also see that your mytestjob is still there.

Now you can containerize your devops process with Jenkins and Docker, enjoy.

Hope you like the tutorial. Please let me know your feedback in the response section.

Happy Learning!

--

--