Learn how to run Jenkins in Docker container for efficient CI/CD automation. Follow our step-by-step guide to set up Jenkins with Docker, manage persistent data, and streamline your DevOps workflow. #centlinux #docker #jenkins
Table of Contents
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 Training: Jenkins: Jobs, Pipelines, CI/CD and DevOps for Beginners from Valentin Despa
Docker Host Specification
We are utilizing a preconfigured Docker Host that has been set up with all the necessary dependencies and configurations to ensure seamless container deployment. This host system is optimized for running Docker containers efficiently, providing a stable environment for application development and testing.
The Docker Host is configured with the following hardware and software specifications to support smooth operation and scalability:
- 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
To begin, establish a secure SSH connection to your Docker Host (docker-01.centlinux.com
) using an SSH client such as PuTTY, OpenSSH, or any terminal-based SSH tool. Ensure that you have the necessary root or sudo privileges to manage Docker containers and execute administrative commands.
Once connected to the server, utilize the Docker CLI to search for available Jenkins images from Docker Hub, the official public repository for container images. This will help identify the latest and most suitable Jenkins image for deployment.
# 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.
Final Thoughts
Running Jenkins in a Docker container provides a flexible and scalable solution for continuous integration and deployment. By setting up Docker volumes for data persistence, configuring proper networking and user permissions, and ensuring plugin compatibility, you can create a robust and portable Jenkins environment. This setup simplifies management, enhances security, and allows for quick updates without disrupting existing workflows.
Your Linux servers deserve expert care! I provide reliable management and optimization services tailored to your needs. Discover how I can help on Fiverr!
FAQs
Why should I run Jenkins in a Docker container?
Running Jenkins in Docker provides portability, easy setup, and isolation, allowing for quick deployment and simplified management without affecting the host system.
What are the prerequisites for running Jenkins in Docker?
Before running Jenkins in Docker, ensure that Docker is installed, your system has enough resources (CPU/RAM), and you have network access to pull Jenkins images.
How can I persist Jenkins data in a Docker container?
Using Docker volumes or bind mounts ensures that Jenkins data, including jobs, plugins, and configurations, is retained even after the container is restarted or removed.
What ports need to be configured for Jenkins in Docker?
Jenkins typically runs on port 8080 for the web interface and port 50000 for agent communication. These ports should be mapped properly when running the container.
How can I update Jenkins when running it in Docker?
To update Jenkins, pull the latest Jenkins image, stop the existing container, and restart a new container with the updated image while preserving the data volume for persistence.