Run Jenkins on Docker Container

Reading Time: 3 minutes

Hello readers, in this blog I’ll tell you how you can setup Jenkins in a docker container. I’ll also cover how to start Jenkins container and what are the required setting and configurations you are required to have. So before getting started, let’s know some basics about docker and jenkins.

Docker

Docker is basically a tool used to create, deploy and run applications using containers. A container includes the application and its dependent libraries in one package which is deployed wherever it is required to be run. It does not interact with the host OS or other containers, i.e. every container is isolated from each other an the host OS. Docker allows an application to be packed inside a container, which is fully deployable.

Jenkins

Jenkins is an open-source CI/CD tool which is used to automate the build, then performing the tests, and then finally deploying the application by performing a sequence of steps which are mentioned in a script.

Now, moving further, let’s see how we can run the Jenkins in a container.

Jenkins containerization

Following are the series of steps you need to follow:

1. Get the jenkins image

To run the Jenkins in docker, you need the Docker image of Jenkins which can be found at DockerHub which is a global repository of Docker images.

Pull the Jenkins image to your own system using the command docker pull jenkins. To verify that, you can run docker images command which gives the list of images you have on your local system.

2. Run the jenkins image

To run the docker image, you need to execute the following command:

docker run -p 8080:8080 -p 50000:50000 --name jenkins -v /your/home:/var/jenkins_home jenkins

docker run is used to run the jenkins image with default tag latest.

The -p flag is used to publish a container’s port to the host. Jenkins default port 8080 and the Jenkins API port 50000 are mapped to the host’s 8080 and 50000 port respectively. You can map this to any of your host’s unused port. The sequence of mapping the port in the tag -p is: -p <host_port>:<container_port>.

The -v flag which is used to include a volume to Jenkins container because the containers are stateless and incapable of storing data. The Jenkins creates its home directory Jenkins_home which is stored in the bind mount directory. If we don’t assign any volume to the Jenkins, the Jenkins_home directory will be deleted every time the container is restarted.

For -v flag, instead of bind mount, you can mount it to docker volume. To create a volume the command is docker volume create <volume_name>, and then assigning it to the Jenkins through the following command:

docker run -p 8080:8080 -p 50000:50000 -v <volume_name>:/var/jenkins_home jenkins

3. Verify jenkins

After running the docker run command, the Jenkins will start on the container’s port 8080 and its home directory will be created on the path we provided in the -v flag.

This can be verified by checking if the container is up and running by using the command: docker ps -a | grep jenkins. Also, you will see the Jenkins Dashboard running on localhost:8080 rendered by the Jenkins container. Now you can configure the Jenkins as per your requirements.

After going through the contents, now you’ll be able to containerize Jenkins. Still, if you have any queries, feel free to contact me at yatharth.sharma@knoldus.in.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies

Written by 

Yatharth Sharma is a Software Consultant at Knoldus Software LLP. He has done MCA from Bharati Vidyapeeth Institute of Computer Application and Management, Paschim Vihar. He has a decent knowledge of Java Language and currently working on DevOps technologies/tools like Ansible, Jenkins, Docker, Kubernetes. Apart from programming, he loves listening to rap music.