- Category: DevOps

How to Run Jenkins in a Docker Container

featured article thumbnail
author avatar

Written by David Essien

Fullstack Developer/DevOps Engineer

Introduction

  In this article, I am going to show you how to install and setup Jenkins inside a docker container.

Prerequisites

  To be able to practice the things I will share with you in this article you need to have a minimum of the following:

  • Good internet connection
  • A computer with Docker installed on it either Windows, Mac, or Linux
  • A web browser
  • A command-line application - Terminal, or Windows PowerShell

 

What is Jenkins?

Jenkins is an Open Source tool built with Java and is used for Continuous Integration(CI) and Continuous Delivery(CD). Jenkins is used by many teams because:

  • It is cross-platform built with Java
  • It has a large community of users
  • It has a large plugin base that provides several plugins for integrations and performing several tasks
  • It is free since it is Open Source
  • It is easy to use

 

Why run Jenkins in a docker container?

Running Jenkins with docker makes the whole installation process seem like a piece of cake. Without Docker, you would have to install so many plugins and tools for Jenkins to be able to run properly. With docker, it is just a simple process of pulling the image from the docker registry and running it on your machine or cloud environment.

How to run Jenkins in a docker container

Step 1: Pull the Jenkins image from docker repository

visit the docker repository for Jenkins at https://hub.docker.com/r/jenkins/jenkins

Step 2: Copy the docker pull command and run it on your terminal

For this article, we will be using the alpine version. The alpine versions are usually smaller.

docker pull jenkins/jenkins:apine

docker pull jenkins/jenkins:alpine

After the image has been pulled, run docker images on the terminal to be sure it has been pulled. docker images

Step 3: Run a container using the image you just pulled

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

 

Let me explain what the above command does docker run: This is the docker command for starting a container

  • name jenkins: This command will give the container the name jenkins after it has been started.
  • -p 8080:8080: The first 8080 is the port you will use to access the Jenkins service on your own machine(localhost:8080). The second 8080 after the first colon is the Jenkins default port. What we have done with this command is bound port 8080 on Jenkins to port 8080 on our local machine.
  • -p 50000:50000: Same as the case of the Jenkins port above, we are binding the ports to each other. However, in this case, the port is meant for the Jenkins master to be able to communicate with the slaves. The Jenkins master coordinates and does the administrative tasks of your Jenkins infrastructure while the purpose of the slaves is to run the tasks. It is also important to note that the master can also run jobs like the Jenkins slaves. But you can have your Jenkins setup without the slaves.

-v /your/home:/var/jenkins_home: The -v flag is a command for mapping or creating volumes in Jenkins. Data that is generated while a container is running is ephemeral in nature. The image that is used to generate your container is read-only, so any data that is generated while the container is running will exist only inside the container. Once the container is destroyed, all that data is lost. In our case, we do not want to lose all our configurations and settings after our Jenkins container is destroyed. We have to create a volume on our local machine and map it to the Jenkins directory in the container in other to save our data. This way, any files generated while the container is running can still be accessed after the container is destroyed and when we start another container.  The first part before the colon(/your/home) is the path on your computer where you want the Jenkins files to be saved.

Note:

You may face an error that looks like the one below:

docker: Error response from daemon: Mounts denied: The paths /var/folders/zz/... and /var/folders/zz/... are not shared from OS X and are not known to Docker. You can configure shared paths from Docker -> Preferences... -> File Sharing.

 

You can read about the cause and solution in this article from StackOverflow (https://stackoverflow.com/questions/45122459/docker-mounts-denied-the-paths-are-not-shared-from-os-x-and-are-not-known/45123074) For me, I provided an absolute path to a directory so that I don't have any issues. The second part(/var/jenkins_home) is the default directory for Jenkins on the container.

  • 1282bc63ab17: This last part of our command, specifies the id of the image you want to run, which is the id Jenkins container we just pooled. You can run docker images to get the id of the image pulled on your computer. docker run --name jenkins -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home 1282bc63ab17

After the container has been started successfully, the output message Jenkins is fully up and running will be displayed on your terminal.

Jenkins login credentials

Step 4: setup Jenkins

  • Visit localhost/8080 on your browser. You should see a web page like the one below:

unlock jenkins

  • The administrator password is a long string of numbers and letters. You will find it in the terminal where you ran your docker commands. Copy it and paste it in the Administrator password field on the web page.
  • On the new page you are redirected to, click on Install suggested plugins This will take some time, so you will have to wait for the plugins to install.

Customize Jenkins

  • After the installation of the plugins, you will be redirected to a page where you will be required to enter the First Admin details. Till and form correctly, and then Save and Continue.

Create first jenkins user

  • In the next page where you are required to enter your Jenkins URL, leave the URL as it is. Jenkins Instance configuration

We are done - you can click on the button to start using Jenkins. Jenkins is ready Welcome to Jenkins

Use a docker-compose.yml file alternatively

Instead of going through the above processes, you can just create a docker-compose.yml file to run this process for you with one command. Here is the one I created:

version: '3' services: jenkins: image: jenkins/jenkins:alpine container_name: jenkins ports: - 9090:8080 - 50000:50000 volumes: - ./docker_volumes/jenkins:/var/jenkins_home

 

All you need to do is navigate to the folder containing this docker-compose.yml file and run the command: docker-compose up and you have your Jenkins setup up and running.

Conclusion

You have seen how easy it was to set up Jenkins in docker. The process is simple and fast. If you have learned anything, I would love to hear from you. Please drop your comments.