Installing Jenkins in docker

For installing jenkins in docker, I followed the download link https://www.jenkins.io/download/.

After that I saw there is docker image available for jenkins in hub.docker.com.

Step 1: Pull docker image

I started the docker daemon and then pull the image from hub.docker.com I always use LTS version because it’s more stable.

rudra:lab/ $ docker pull jenkins/jenkins:lts-jdk11
lts-jdk11: Pulling from jenkins/jenkins
4c25b3090c26: Pull complete
750d566fdd60: Pull complete
2718cc36ca02: Pull complete
5678b027ee14: Pull complete
c839cd2df78d: Pull complete
50861a5addda: Pull complete
ff2b028e5cf5: Pull complete
ee710b58f452: Pull complete
2625c929bb0e: Pull complete
6a6bf9181c04: Pull complete
bee5e6792ac4: Pull complete
6cc5edd2133e: Pull complete
c07b16426ded: Pull complete
e9ac42647ae3: Pull complete
fa925738a490: Pull complete
4a08c3886279: Pull complete
2d43fec22b7e: Pull complete
Digest: sha256:a942c30fc3bcf269a1c32ba27eb4a470148eff9aba086911320031a3c3943e6c
Status: Downloaded newer image for jenkins/jenkins:lts-jdk11
docker.io/jenkins/jenkins:lts-jdk11

As we pulled the image from hub.docker.com, now we can create our jenkins container from the image.

rudra:lab/ $ docker image list
REPOSITORY        TAG         IMAGE ID       CREATED       SIZE
jenkins/jenkins   lts-jdk11   619aabbe0502   5 hours ago   441MB

Step 2: Create container and basic commands

Copy the image id from the above output and use it to create the container. A blog of digitalocean: 3 Tips for Naming Docker Containers, docker docs about container name –name

rudra:~/ $ docker run --name=jenkins-for-learn -d -p 8081:8080 619aabbe0502
516653a91b3bd8a00399dc5dc47faec7d0769814e12d50f448f2e8368d63892f

make sure you use --name argument after docker run command or not it can causes error. Let’s discuss about the command I write in the above command.

docker run help to create a container. -d is for detach mode. It means that the container will run in the background, -p is for port maping if you see in the command line, 8081 will talk to port 8080 inside the container. So, you can access jenkins from your local machine.

Now type docker ps to see the running container.

rudra:~/ $ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
ef7240932d77   619aabbe0502   "/sbin/tini -- /usr/…"   13 minutes ago   Up 13 minutes   50000/tcp, 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp   jenkins-for-learn

if you want to stop the container, type docker stop <container_id>

after I use stop command, I can see the container stopped. No container running after using docker ps command.

rudra:~/ $ docker stop ef7240932d77
ef7240932d77
rudra:~/ $ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

As we have container id, we can use it to access CLI from local machine to the container. Learn docker exec

rudra:~/ $ docker exec -it ef7240932d77 /bin/sh
$ ls
bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  var
$

Step 3: Install jenkins

It’s fun right? we need to access CLI, when we are going to install jenkins. As we have port 8081 which accosiate to 8080 inside the container, we can access jenkins from local machine. Let’s browse from http://localhost:8081/

It will redirect to Unlock Jenkins page. where we need to input the password and now we need the CLI to get the password.

rudra:~/ $ docker exec -it ef7240932d77 /bin/sh
$ cat /var/jenkins_home/secrets/initialAdminPassword
a5b9c40a2c7b420199827e7609406a2a

Copy the password and paste it in the Administrator password input box and click Continue. Now in Customize Jenkins page, click Install suggested plugins.

I normally go with suggested pulgin because it installed few pulgin which actually I need, probably everyon because there is Git, OWASP Markup Formatter, Pipeline, GitHub Branch Source so on.

Now, Create First Admin User input username, password, full name and email and click Save and Continue in the next page click Save instance and then Jenkins is ready! to use.

Thanks for reading this post. I will create another post about how to create Freestyle project in jenkins.

Written on August 20, 2021