Containerizing Jenkins: Effortless CI/CD with Docker

Containerizing Jenkins: Effortless CI/CD with Docker

Prerequisites

  • Docker installed on your machine/server

  • Basic knowledge of Docker commands

As you can see I am using an Amazon Linux EC2 instance for setting up Jenkins in Docker.

I have installed the docker as you can see.

Now let's start with setting up Jenkins in the docker

1. Pull the Jenkins Image

Open your terminal or command prompt and run the following command to pull the official Jenkins image from Docker Hub:

sudo docker pull jenkins/jenkins:lts

2. Create a Docker Volume

Jenkins stores its data in /var/jenkins_home inside the container. To persist this data, create a Docker volume:

sudo docker volume create jenkins_data

3. Run Jenkins Container

Use the following command to run a Jenkins container

sudo docker run -d -p 80:8080 -p 50000:50000 -v jenkins_data:/var/jenkins_home --name jenkins jenkins/jenkins:lts
  • -d: Run the container in detached mode

  • -p 80:8080: Map port 80 on the host to port 8080 on the container (Jenkins web UI)

  • -p 50000:50000: Map port 50000 on the host to port 50000 on the container (Jenkins agent)

  • -v jenkins_data:/var/jenkins_home: Mount the jenkins_data volume to persist Jenkins data

  • --name jenkins: Name the container as "jenkins" Access Jenkins

    As you can see, our container has started so let's try to hit from the browser :

If you hit this endpoint from your browser http://base_url_for_your_ec2_instence:80
simple: - http://ec25484121515-9.ap-south-1.compute.amazonaws.com:80

Then you will find this kind of webpage :

4. Now we need to pass an Initial Administrator Password

Run the below command and replace [container_id/name] of Jenkins contain

sudo docker exec -it [container_id/name] /bin/sh -c "cd /var/jenkins_home/secrets && cat initialAdminPassword"

sample :

sudo docker exec -it jenkins /bin/sh -c "cd /var/jenkins_home/secrets && cat initialAdminPassword"

After this command, you will find an alphanumeric string that is your Initial Administrator Password.

So put that initial password into the webpage's input box and press the continue button then you will land on this webpage

Click on the selected option andand it will load some plugins automatically and then you will land on this page

So fill out your details and save and finally, your Jenkins setup is done.

Happy coding!