Jenkins on Docker

 

What is not included

This article doesn't discuss about jenkins, its uses, configurations etc. It just aims to run jenkins as a docker image, as an alternative to an installation.

This article doesn't discuss about running jenkins slaves as docker container.

Which Image?

Running Jenkins on docker is just about using the right image.

When searching for "jenkins" on docker hub, the first result we get is an official image. But open it and it reads "This image has been deprecated in favor of the jenkins/jenkins:lts". This means, we need to use jenkins/jenkins:lts image.

Command

Pull correct image

docker pull jenkins/jenkins:lts

And run it

docker run --detach \
--publish 8082:8080 --publish 50000:50000 \
--volume ~/docker/data/jenkins/jenkins_home:/var/jenkins_home \
--name jenkins \
jenkins/jenkins:lts

Flags explaination:

--detach: We shall run this image detached - i.e. the logs will not be printed on current terminal.

--publish: Jenkins exposes two set of ports - 8080 (tomcat) and 50000. We are trying to map it to local ports 8082 and 50000. We can use 8080. But in my case, I already have my java services running on 8080.

--volume: Jenkins keeps its config files in /var/jenkins_home. We are mapping it to a local folder.

--name: I shall call my container as "jenkins"

Open & Setup

Open http://localhost:8082



It opens first page that asks for Administrator password.
It also mentions the location where this password is.
And we mapped "/var/jenkins_home" already to our local folder. So lets go and check for password in file "~/docker/data/jenkins/jenkins_home/secrets/initialAdminPassword". 
Copy password into text box.

Note: default username is "admin".

Now we could install suggested plugins, or ignore this step for now. I choose to ignore.

First Job

Now we should be on home page. 

  1. Click on "New Item" on left menu
  2. Enter item name. I chose "FirstJob"
  3. Select "Freestyle project"
  4. OK
  5. It opens configuration page for our new job.
  6. Scroll down to Build
  7. In "Add build step" dropdown, select "Execute Shell"
  8. In command just enter "uname -a". You could enter any command you want.
  9. Save
First job is now ready to be triggered.
Go to your first job, and trigger "Build Now" on left menu.

Job is run, and it prints outputs uname command. I got
Linux bdc39ab786e1 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 GNU/Linux

Conclusion

I do not find any difference between jenkins installed, and jenkins as docker.
I prefer docker over an installation on Mac because
  1. I have no worry of installation and uninstallation leaving cruft behind.
  2. I can re-create containers as many times as I want.
  3. I can have multiple instances of same software on my system without interfering with each other.

Comments