📜Day 27- Jenkins Declarative Pipeline with Docker

Day 26 was all about a Declarative pipeline, now its time to level up things, let's integrate Docker and your Jenkins declarative pipeline

📚Use your Docker Build and Run Knowledge

docker build - you can use sh 'docker build . -t <tag>' in your pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.

docker run: you can use sh 'docker run -d <image>' in your pipeline stage block to build the container.

How will the stages look

stages {
        stage('Build') {
            steps {
                sh 'docker build -t moudekarvivek/django-app:latest'
            }
        }
    }

✔Task-01:Create a Docker-Integrated Jenkins Declarative Pipeline

  • Create a docker-integrated Jenkins declarative pipeline

  • Use the above-given syntax using sh inside the stage block

  • You will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2

📍For Jenkins Installation You need to Install Java.

sudo apt update    #update index file
sudo apt install fontconfig openjdk-17-jre   #Java Installation

📍 Install Jenkins, Docker & Docker-compose.

# This Installation command also available on official documentation

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
#  Docker & Docker-Compose

sudo apt-get install docker.io docker-compose -y

📍Enable Jenkins & Docker to run them even after we restart our system

sudo systemctl enable jenkins
sudo systemctl enable docker

📍Add User and jenkins in docker group other wise while building it will through errors

sudo usermod -aG docker $USER
sudo usermod -aG docker jenkins

cat /etc/group #command use to check users are added or not

📍Go to security group, open port 8080 for Jenkins & 8000 for Docker.

📍Access Jenkins Portal using your instance's public-ip:8080.

📍Now, creating new pipeline job. Go to Create a job -> name of Project ->select Pipeline->Click OK.

📍Configure you pipeline.

📍Go to last, select Pipeline script-> write this script-> Save it.

pipeline {
    agent any

    stages {
        stage("code") {
            steps {
                git url: "https://github.com/moudekarvivek/node-todo-cicd.git", branch: "master"
                echo "code cloned successfully"
            }
        }

        stage("build") {
            steps {
                sh 'docker build . -t todo-app'
                echo "code build successfully"
            }
        }

        stage("deploy") {
            steps {
                sh "docker run -p 8000:8000 -d todo-app" 
                echo "Node-app deployed successfully"
            }
        }
    }
}

Click on Build Now.

📍On First Click your Pipeline runs successfully.

📍But you will face errors(port no. 8000 is already allocated) in case of running a job twice, as the docker container will be already created.

For that kill the container first and then build it will work

✔Task 2: Enhance Pipeline with Docker Groovy Syntax

  • Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.

📍Now you just go back to the configuration of your pipeline job.

📍Modify the Pipeline Script.

pipeline {
    agent any

    stages {
        stage("code") {
            steps {
                git url: "https://github.com/moudekarvivek/node-todo-cicd.git", branch: "master"
                echo "code cloned successfully"
            }
        }

         stage("build and test"){
            steps{
                sh "docker build -t node-app-test-new ."
                echo 'Code Build Successfully'
            }
        }

        stage("deploy") {
            steps {
                sh "docker-compose down && docker-compose up -d"
                echo "Node-app deployed successfully"
            }
        }
    }
}

Congratulations, your pipeline is running successfully.🎊

🚧Conclusion

In conclusion, using a declarative CI/CD pipeline with Docker commands in Groovy syntax offers a efficient way to automate your build, test, and deployment processes. By defining your pipeline stages and steps within a Jenkinsfile, you can easily manage and version control your CI/CD workflow.

Happy Learning :)

0
Subscribe to my newsletter

Read articles from Vivek Ashok Moudekar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vivek Ashok Moudekar
Vivek Ashok Moudekar

👋 Hello there! I'm Vivek, a DevOps enthusiast with a keen interest in streamlining software delivery. I hold a Master's degree in Computer Applications and have a solid foundation in key technologies such as Linux, Git, Docker, Kubernetes, and AWS. 💻 My passion lies in automation, ensuring efficient and seamless processes throughout the software development lifecycle. I thrive on creating robust CI/CD pipelines that empower teams to deliver high-quality software with confidence. 🚀 Beyond the code, I enjoy the ever-evolving world of DevOps and the challenges it brings. Join me on this journey as I explore new ways to enhance software delivery and foster a culture of continuous improvement. Let's connect, collaborate, and make the world of DevOps even more exciting together