How to configure jenkins in a docker container


In this blog, I will explain step-by-step process how can we configure jenkins in a docker container and then running jobs inside conatiners. First of all, let us see why we need to do so ?
Configuring jenkins in a docker container has a number of benefits:
1️⃣ Docker container image can be easily pushed to docker hub which anyone can access and reuse.
2️⃣ Docker image can be pulled on any server which has docker installed on it.
3️⃣ Docker volumes helps in retaining jenkin server’s data like users,configurations,jobs etc.
4️⃣ We can use docker containers inside jenkins to launch containers for different job stages, thus helping in running different execution enviroment for each job.
Now, lets talk about the steps how we can easily launch jenkins server using a docker container.
✅ Step 1 : First of all, install docker desktop on host machine. After installation, run the following command to verify that docker has been installed.
﹥ docker --version
✅ Step 2: Now, run the following docker commands to get your jenkins container up and running. I will use some extra commands which will help in using docker containers inside jenkins for different jobs.
﹥ docker run -td --name jenkins-container -p 8080:8080 --user root --privileged -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:lts
✅ Step 3: After the image has been pulled, you can list the running container and can see your jenkins server running on port 8080 of your host machine.
✅ Step 4: To get the initialAdminPassword, get access to your container by running
﹥ docker exec -it jenkins-container /bin/bash
✅ Step 5: Open the file located at /var/jenkins_home/secrets/initialAdminPassword. This file contains your initialAdminPassword which will be reuqired only one time to make sure you are managing the server and you have the keyfile accessible to you.
That is it. Now you can easily setup your further configuration regarding your pipelines and source code.
Happy Learning : )
Subscribe to my newsletter
Read articles from Huzaifa Jamal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
