How to Set up Jenkins in Docker Container ?
Setting up of Jenkins in a docker container provides the facility of streamlining the CI/CD with scalability and consistency. It also helps for attaching the worker nodes as slaves and run the build jobs over their. In this article, we will guide you through the process of deploying jenkins in a Docker container over AWS Cloud EC2 instance.
What is Jenkins ?
Jenkins is an open-source automation server widely used for continuous integration (CI) and continuous delivery (CD) processes in software development. It allows developers to automate the building, testing, and deployment of code changes, ensuring rapid and reliable software delivery. With its vast plugin ecosystem and flexibility, Jenkins has become a cornerstone tool for modern software development teams, enabling efficient collaboration and faster time-to-market for applications.
Primary Terminologies
-
Jenkins is an open-source automation server that is used to automate different parts of your software development related to building, testing, and deploying.
-
Docker is a set of platforms as a service product that uses OS-level virtualization to deliver software in packages called containers.
-
EC2 or Elastic Compute Cloud is a scalable computing service launched on the AWS cloud platform.
If you’re looking to expand your knowledge of Jenkins, Docker, and CI/CD pipelines, the DevOps Live Course covers everything from Jenkins setup in containers to automating pipelines for various development projects.
How to Set up Jenkins in Docker Container ? A Step-By-Step Guide
The following are the steps that guides in setting up the jenkins in docker container using AWS EC2 instances:
Step 1
First, log in to your AWS account and go to the EC2 service. There launch any free tier Amazon Linux machine. Here is the image attached to refer to.
Make sure your Security inbound rules look like this. So that you don’t have any further issues.
Step 2
After launching, SSH to that EC2 instance. And Install Docker by running the following commands.
To update all packages
sudo yum update -y
To install docker latest version
sudo amazon-linux-extras install docker
To start docker service
sudo service docker start
To check status of docker service. If it's running or not.
sudo service docker status
To ensure that docker service start after each reboot.
sudo systemctl enable docker
To add ec2-user to docker group
sudo usermod -a -G docker ec2-user
Refer to the below images for the output of the above commands.
The following screenshot illustrates on installation of docker on AWS Amazon Linux:
The following screenshot illustrates on the checking and starting the docker service:
Now, to check if docker is installed or not. Run below command and match with an image attached further.
It will give you docker version
docker -v
Step 3
Now we will pull the Jenkins image using docker from the docker hub. Run the following command to pull the docker images such jenkins/jenkins.
To pull the image of jenkins
docker pull jenkins/jenkins
To see if image is downloaded or not
docker images
Please refer to the image attached for a better understanding.
Step 4
Since in docker images we can see our Jenkins image. Now we can make our Jenkins container. But before that make a directory. Run below command
To make directory name jenkins
mkdir jenkins
After making this directory run the below command Please refer to the image attached ahead for a better understanding.
To run a container name jenkins using jenkins image
docker run -d --name jenkins -p 8080:8080 -v $PWD/jenkins/ jenkins/jenkins
To see if container is running or not
docker ps
Now, copy the Public IPv4 address of the EC2 instance. Refer to the below image for any confusion.
After copying, it. Paste it into a new tab and write it with port 8080. Refer the below image for better understanding and resolving confusion.
After running this, you’ll see a screen like this.
Step 5
To get this password. Run the below command in the EC2 instance and copy the password. Refer to the image if there is any confusion.
To see logs of the container name jenkins
docker logs jenkins
After entering this password, Install all the plugins (it may take a while).
Step 6
Once all the plugins are installed you’ll be asked to make a user with a password and After making that user. You’ll be logged into Jenkins.
The following screenshot illustrates on creation of new admin user:
Access to the jenkins web dashboard as shown in the below screenshot.
Congratulations! If you have come this far that’s mean you have successfully set up the Jenkins in Docker container. Now you can easily automate your software using Jenkins.
Difference between Jenkins and Docker
The following are the difference between Jenkins and docker:
Aspect | Jenkins | Docker |
---|---|---|
Purpose | Jenkins is a tool used for Continuous Integration and Continuous Delivery (CI/CD) to automate software development processes | Docker is a containerization platform used for building, deploying, and managing applications within containers |
Functionality | Manages and automates tasks such as building, testing, and deploying code across different stages of the development pipeline | Creates lightweight, portable containers that encapsulate applications with all their dependencies, providing consistent and reproducible environments |
Architecture | Follows a master-slave architecture where the Jenkins master coordinates tasks and distributes work to multiple slave nodes | Follows a client-server architecture where Docker Engine acts as the server managing containers, and Docker CLI interacts with the server to execute commands |
Use Cases | Ideal for automating software development workflows, integrating with version control systems, and orchestrating complex build and deployment pipelines | Suitable for containerizing applications, microservices, and distributed systems, offering flexibility, scalability, and portability across different environments |
Why Jenkins use Docker ?
The following are the reasons for jenkins using docker:
-
Scalability: Docker facilitates the jenkins with dynamically provision features for build agents as Docker containers allowing for efficient scaling of resources based on workload demands.
-
Isolation: Docker provides light weighted isolated containers for running build jobs, ensuring each build runs in its isolated environments.
-
Consistency: By using docker containers, jenkins ensures the consistency between development, testing and production environments.
-
Resource Efficiency: Docker containers consumes fewer resources compared to the traditional virtual machines making them a more efficient choice for running build jobs.