Step-by-Step Guide to Setting Up Jenkins with Docker Agent

Step-by-Step Guide to Setting Up Jenkins with Docker Agent

·

3 min read

Today, I dived deep into Continuous Integration and Continuous Deployment (CI/CD) and Jenkins Declarative Pipelines using a Docker agent. To make things more exciting, I created my first project and documented every step so that you can follow along and set it up yourself. Let’s get started!


Step-by-Step Guide to Setting Up Jenkins with Docker Agent

Here’s a simple walkthrough of everything I did:

1. Launch an AWS EC2 Instance

I created an Ubuntu-based EC2 instance to serve as my Jenkins master node. Ensure you have port 8080 open in the security group’s inbound rules to access Jenkins from your browser.


2. Install Java

Jenkins requires Java, so I installed OpenJDK 17 using these commands:

sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version

Verify the Java version to ensure it’s installed correctly.


3. Install Jenkins

Next, I installed Jenkins by adding its repository and installing the package:

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

Restart Jenkins for good measure:

sudo systemctl restart jenkins
sudo systemctl enable jenkins

4. Install Docker and Grant Permissions

To use Docker as an agent in Jenkins, I installed Docker and granted the necessary permissions:

sudo apt install docker.io -y
sudo usermod -aG docker $USER && newgrp docker

Restart Docker and Jenkins:

sudo systemctl restart docker
sudo systemctl restart jenkins

5. Access Jenkins and Complete Setup

Access Jenkins in your browser using http://<your-ec2-public-ip>:8080. Follow the setup wizard and install the recommended plugins.


6. Install Docker Pipeline Plugin

In Jenkins, navigate to Manage Jenkins → Plugins → Available Plugins and install the Docker Pipeline Plugin.

This plugin is crucial as it allows Jenkins to run pipelines in a Docker agent specified in the Jenkinsfile.

Restart Jenkins again to apply changes.


Creating My First Declarative Pipeline Project

Here’s where the magic begins:

  1. Create a New Pipeline Project: Go to Jenkins and create a pipeline project.

  1. Add a Jenkinsfile: Define your pipeline in a declarative format. The key part is using the Docker agent, so the pipeline runs inside a container.

     pipeline {
       agent {
         docker { image 'node:16-alpine' }
       }
       stages {
         stage('Test') {
           steps {
             sh 'node --version'
           }
         }
       }
     }
    
  2. Build and Execute: When you run the pipeline, Jenkins will:

    • Create a Docker container.

    • Execute the pipeline steps inside the container.

    • Delete the container after execution.

It creates docker container, executed pipeline and delete the docker container as well


Why This Setup is Awesome

  • Scalable: Each build runs in an isolated container, ensuring consistency.

  • Resource-Efficient: Containers are lightweight and ephemeral.

  • Beginner-Friendly: Jenkins’ GUI simplifies pipeline management.


My Results

Here’s what I achieved:

  • Jenkins successfully pulled and spun up a Docker container.

  • The pipeline executed flawlessly within the container.

  • The container was deleted after the build, keeping my setup clean.

(Visuals for each step included in the images!)


Key Takeaways

Learning about CI/CD, Jenkins, and Docker agents might seem overwhelming initially, but breaking it into steps makes it manageable. This setup is the foundation for advanced pipelines and workflows.

If you're starting your DevOps journey, this is a great project to try out. Don’t forget to share your results in the comments or tag me on LinkedIn! 🚀