In this session, we will understand Jenkins docker. I will explain how to set up Jenkins on your machine using docker and docker-compose. so let’s get started.
In dockerhub, there any many ready-to-use images available for Jenkins. There are official images, images from the verified publisher as well as images from sponsored OOS.
You can choose the image as per your need. The safe option is to choose the image which has high downloads and stars.
Jenkins docker image
The most popular Jenkins image is Jenkins/Jenkins which is available in dockerhub. I’ll recommend using this image as this image is continuously updated by the open-source community and has very less vulnerability as compared to other images.
You can visit the Jenkins/Jenkins image github page to read more about how these images have been created.
You will also get information about volume mounting and other configurations.
Jenkins docker install
Jenkins is a popular CICD tool that many companies use to automate the CICD process. To install the Jenkins via docker make sure that the docker instance is running and working.
You can also follow the below tutorials to get familiar with docker.
We will use the official docker image Jenkins/Jenkins which is available in dockerhub to deploy the Jenkins instance. You can choose the image tag based on your requirement. For this demo, I will be using the latest tag of the Jenkins image.
Let’s create a local directory to assign the volume to the Jenkins container.
Now type the below command to install the Jenkins image
docker run -p 8080:8080 -p 50000:50000 --restart=on-failure -v jenkins_data:/var/jenkins_home jenkins/jenkins:latest
Jenkins image explanation
- -p flag is used to map port 8080 of the container to local port 8080. The Jenkins instance will be available on this port in your local environment.
- We have also mapped the agent port 50000 to local port 50000.
- -v flag is used to mound the local volume jenkins_data to container volume /var/jenkins_home. It means all the data from the /var/jenkins_home folder will be backed up to the local folder jenkins_data.
- a –a restart=on-failure flag will restart the Jenkins instance in case of any failure.
Now go to https://localhost:8080 to access the Jenkins instance.
Pass the admin password which is displayed in the terminal and click on the continue button.
Now click on install suggested plugin to install the plugin suggested by docker.
Once all the necessary plugins are installed, your Jenkins setup is completed.
Now you can either create an admin user or skip and continue as an admin based on your need. For this demo, I am creating a user as per the below screenshot.
Now click on start using Jenkins
Jenkins dockerfile example
So far we have seen how to install Jenkins via a simple readily available image. But user can write their own dockerfile as per their need. Below is the sample dockerfile which will start a Jenkins and install pipeline and GitHub plugin plugins
FROM jenkins/jenkins:lts-jdk11 RUN jenkins-plugin-cli --plugins pipeline-model-definition github-branch-source:1.9
Users can also create a plugins.txt file and mention all the required pluggins>Below is the sample dockerfile for reference
FROM jenkins/jenkins:lts-jdk11 COPY --chown=jenkins:jenkins plugins.txt /usr/share/jenkins/ref/plugins.txt RUN jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt
More information can be found here
So far we have seen how to install Jenkins using the docker command or using the dockerfile. But a user can also install Jenkins using a docker-compose file.
To deploy a container with docker-compose make sure the docker-compose library is installed in your system.
Now create a folder called jenkins_data
and create a docker-compose.yml file and paste the below content.
version: '1.1' services: jenkins: image: jenkins/jenkins:latest restart: on-failure privileged: true user: root ports: - 8080:8080 - 50000:50000 container_name: jenkins volumes: - ~/jenkins_data:/var/jenkins_home - /var/run/docker.sock:/var/run/docker.sock
Now run the below command to start the Jenkins container.
docker-compose up -d
Goto localhost:8080 to access the Jenkins URL.
I hope you have liked this tutorial about how to install Jenkins via docker and docker-compose. Please do let me know if you are facing any issues while following along.