In this article, you will learn how to run Jenkins in Docker. #centlinux #docker #jenkins
Table of Contents
What is Jenkins? :
Jenkins is a free and open-source automation server. Jenkins helps in automation of software development processes related to building, testing and deploying, thus it facilitates continuous integration and continuous delivery.
Jenkins is written in Java and it is distributed under MIT License.
Jenkins is server based system that runs in a Java servlet container such as Apache Tomcat.
We are using a preconfigured Docker Host with following specifications.
- CPU – 3.4 Ghz (2 cores)
- Memory – 4 GB
- Storage – 60 GB
- Operating System – CentOS 8.1
- Hostname – docker-01.centlinux.com
- IP Address – 192.168.116.206 /24
Pull Jenkins image from Docker Hub:
Connect to Docker Host (docker-01.centlinux.com) as root user by using a SSH tool.
Use the docker command to search for the available jenkins images.
# docker search jenkins --filter is-official=true Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg. INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/library/jenkins Official Jenkins Docker image 4800 [OK]
There is only one official Jenkins docker image available, that has been deprecated in favor of jenkins/jenkins:lts image.
jenkins/jenkins:lts image is maintained by the Jenkins community and it is the most suitable and up-to-date docker image.
Pull the Jenkins docker image by using the following command.
# docker pull jenkins/jenkins:lts Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg. Trying to pull registry.access.redhat.com/jenkins/jenkins... name unknown: Repo not found Trying to pull registry.fedoraproject.org/jenkins/jenkins... invalid status code from registry 503 (Service Unavailable) Trying to pull registry.centos.org/jenkins/jenkins... manifest unknown: manifest unknown Trying to pull docker.io/jenkins/jenkins... Getting image source signatures Copying blob d108b8c498aa done Copying blob 1bfe918b8aa5 done Copying blob 9d647f502a07 done Copying blob cc4fe40d0e61 done Copying blob 3192219afd04 done Copying blob 17c160265e75 done Copying blob dafa1a7c0751 done Copying blob b2d02276dac1 done Copying blob 2c0d0c8c3efd done Copying blob 96361a673333 done Copying blob 81c6f1bc405d done Copying blob 0a46f33b1b25 done Copying blob 30eaf72640cc done Copying blob f4b226e89c35 done Copying blob bb775209c68a done Copying blob 27df1ec63d52 done Copying blob 229f7473962e done Copying blob afd6ff4cc063 done Copying blob c69f789a4a12 done Copying config 6328c71fe3 done Writing manifest to image destination Storing signatures 6328c71fe374c19ecc3df6b7ab5528ffaaca8e8fcdb000c8aa270b9368ccbb7f
Run Jenkins in Docker Container:
Now, we have the required Jenkins docker image. So we can create and run a Jenkins docker container by using the following command.
# docker run -p 8080:8080 > -p 50000:50000 > --name=jenkins-master > -v jenkins_home:/var/jenkins_home > -d --env JAVA_OPTS="-Xmx8192m" > --env JENKINS_OPTS="--handlerCountMax=300" > jenkins/jenkins:lts Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg. e823fc1178a38973bfc61e22a848c61779868fe72d0428c8dec56df75d059a4d
Complete Documentation of the Jenkins Docker image is available at Git Hub. Here, we are only using few necessary parameters.
- -p 8080:8080 -> Maps the Jenkins Web UI port of Docker container to Docker host.
- -p 50000:50000 -> Maps the Build Slaves port of Docker container to Docker host.
- –name=jenkins-master -> Name of the Jenkins docker container
- -v jenkins_home:/var/jenkins_home -> Bind mount for storing Jenkins container data and configurations.
- -d –env JAVA_OPTS=”-Xmx8192m” -> Set a JVM memory parameter
- –env JENKINS_OPTS=”–handlerCountMax=300″ -> Set the JENKINS_OPTS environment variable
- jenkins/jenkins:lts -> the image used to create Jenkins docker container
Allow the required service ports in Docker host firewall.
# firewall-cmd --permanent --add-port=8080/tcp success # firewall-cmd --permanent --add-port=50000/tcp success # firewall-cmd --reload success
Access Jenkins Web UI:
Browse URL http://docker-01.centlinux.com:8080 by using a Google Chrome browser.
Since, we are accessing the Jenkins web interface for the first time, therefore, we need the admin password to sign-in into the Jenkins web interface.
The path to password file has been provided by the Jenkins web interface. Therefore, we can use Docker cp command to copy this file to Docker host machine.
# docker cp jenkins-master:/var/jenkins_home/secrets/initialAdminPassword . Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
Display the contents of this file to obtain the admin password.
# cat initialAdminPassword 6b7e5847bf7e41a5a90d8eceeade3e1b
Login to Jenkins web interface by using this password.
We have successfully run Jenkins in docker container. If you want to learn more about Jenkins usage then you should read Continuous Delivery with Docker and Jenkins: Create secure applications by building complete CI/CD pipelines, 2nd Edition (PAID LINK) by Packt Publishing or you can enroll in Jenkins for Beginners course at Udemy.