How to run Jenkins in Docker Container

Share on Social Media

Learn how to run Jenkins in Docker with this step-by-step guide. Discover how to set up, configure, and manage Jenkins within a Docker container for efficient and scalable continuous integration and delivery. #centlinux #docker #jenkins

What is Jenkins?

Jenkins is an open-source automation server widely used for continuous integration (CI) and continuous delivery (CD) in software development. It helps automate the parts of software development related to building, testing, and deploying, facilitating DevOps practices. Here are some key features and aspects of Jenkins:

Key Features of Jenkins

  • Automation:
  • Jenkins automates the process of integrating code changes from multiple contributors, ensuring that new code is automatically built and tested.
  • It supports various stages of the software development lifecycle, from code commit to deployment.
  • Extensibility:
  • Jenkins is highly extensible through a vast ecosystem of plugins. Over 1,800 plugins are available, allowing Jenkins to integrate with many different tools and platforms.
  • Plugins enable Jenkins to perform tasks such as static code analysis, deployment, and integration with version control systems like Git.
  • Ease of Use:
  • Jenkins provides a web-based interface that is easy to use and configure. This interface includes a dashboard where users can monitor the status of their builds and pipelines.
  • It supports a wide range of configurations and customizations to suit different project needs.
  • Scalability:
  • Jenkins can be scaled to manage large projects with complex build processes. It supports distributed builds, where multiple machines (nodes) can be used to perform builds and tests in parallel.
  • This scalability ensures that Jenkins can handle increased workloads as projects grow.
  • Pipeline as Code:
  • Jenkins supports “Pipeline as Code,” allowing developers to define their build and deployment processes in code using a domain-specific language (DSL). This approach enables versioning and code reviews for build configurations.
  • Jenkins pipelines can be written in a simple text file called a Jenkinsfile, which can be stored in a project’s version control repository.
  • Integration:
  • Jenkins integrates seamlessly with many development, testing, and deployment tools, such as Maven, Gradle, Docker, Kubernetes, and many more.
  • It can trigger builds automatically based on changes in version control systems, schedules, or other events.
  • Community and Support:
  • Jenkins has a large and active community that contributes to its development and maintenance. This community support ensures that Jenkins stays up-to-date with the latest trends and technologies.
  • Extensive documentation and numerous online resources are available to help users get the most out of Jenkins.

Use Cases for Jenkins

  • Continuous Integration: Automatically build and test code changes as they are committed, ensuring that integration issues are identified early.
  • Continuous Delivery/Deployment: Automate the deployment of applications to various environments, from development to production, ensuring that deployments are consistent and reliable.
  • Automated Testing: Run automated tests as part of the build process, providing immediate feedback on the quality of the code.
  • Infrastructure as Code (IaC): Manage and deploy infrastructure using code, integrating with tools like Terraform and Ansible.

Conclusion

Jenkins is a robust and versatile automation server that plays a crucial role in modern software development practices. By automating the build, test, and deployment processes, Jenkins helps teams deliver high-quality software faster and more efficiently. Its extensive plugin ecosystem, ease of use, and strong community support make it a popular choice for implementing CI/CD pipelines.

Recommended Online Training: DevOps con Docker, Jenkins, Kubernetes, git, GitFlow CI y CD

3800620 36a8 5show?id=oLRJ54lcVEg&offerid=1606991.3800620&bids=1606991

Docker Host Specification

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

Read Also: How to install JFrog Artifactory on CentOS 7

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.

Loading Jenkins Dashboard
Loading Jenkins Dashboard
Run Jenkins in Docker - Unlock
Run Jenkins in Docker – Unlock

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.

Jenkins Dashboard
Jenkins Dashboard

Final Thoughts

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.

Leave a Comment