Day 27 - Jenkins Declarative Pipeline with Docker

Day 27 - Jenkins Declarative Pipeline with Docker

Welcome back to our Jenkins journey! We've already mastered the basics of Declarative Pipelines in Jenkins. Now, let's take it up a notch by integrating Docker into our pipeline. This will enable us to build and run our applications in a containerized environment. In this article, we'll walk through two tasks to achieve this: one using the sh command for Docker commands and another using Docker's Groovy syntax within the stage block.

Task 01: Using sh Command for Docker

In this task, we will use the familiar sh command to execute Docker build and run commands within our Jenkins Declarative Pipeline.

pipeline {
    agent any

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

        stage('Run') {
            steps {
                sh 'docker run -d trainwithshubham/django-app:latest'
            }
        }
    }
}        

In the above code, we define a simple pipeline with two stages: 'Build' and 'Run.' In the 'Build' stage, we use the docker build command to create an image tagged as trainwithshubham/django-app:latest. In the 'Run' stage, we then use docker run to start a container from the built image.

Note: Running this pipeline multiple times may lead to errors as the Docker container might already exist. We'll address this in Task 02.

Task 02: Using Docker Groovy Syntax

To overcome the potential errors of running the job twice, we can leverage Docker's Groovy syntax within the stage block. This allows us to manage Docker containers more efficiently.

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    docker.image('trainwithshubham/django-app:latest').build()
                }
            }
        }

        stage('Run') {
            steps {
                script {
                    docker.image('trainwithshubham/django-app:latest').run('-d')
                }
            }
        }
    }
}        

In this version, we use the docker.image syntax to manage Docker images. The build() method is used for building the Docker image, and the run('-d') method is used for running the container in detached mode.

By adopting this approach, we ensure that the Docker container is managed more gracefully, preventing errors when the pipeline is run multiple times.

This powerful combination allows us to containerize your applications efficiently, providing consistency and reproducibility in your CI/CD workflows.

要查看或添加评论,请登录

Muhammad Shaheryar的更多文章

  • Day 30 - Terraform

    Day 30 - Terraform

    Introduction Terraform is a powerful open-source tool that falls under the category of Infrastructure as Code (IaC). It…

  • Day 29 - Jenkins Important interview Questions

    Day 29 - Jenkins Important interview Questions

    Mastering Jenkins for Docker-Centric CI/CD: A Comprehensive Interview Guide In the ever-evolving landscape of DevOps…

  • Day 28 - Jenkins Agents

    Day 28 - Jenkins Agents

    Introduction Jenkins, an open-source automation server, plays a crucial role in orchestrating workflows through…

  • Day 26 - Jenkins Declarative Pipeline

    Day 26 - Jenkins Declarative Pipeline

    Introduction In the realm of DevOps and Continuous Integration/Continuous Deployment (CICD), Jenkins plays a pivotal…

    3 条评论
  • Day 25 - Complete Jenkins CI/CD Project - Continued with Documentation

    Day 25 - Complete Jenkins CI/CD Project - Continued with Documentation

    Continuous Integration (CI) and Continuous Deployment (CD) are crucial components of modern software development…

  • Day 24 - Complete Jenkins CI/CD Project

    Day 24 - Complete Jenkins CI/CD Project

    Introduction Welcome to this comprehensive guide on setting up a Jenkins CI/CD pipeline for deploying a Node.js To-Do…

  • Day 23 Task: Jenkins Freestyle Project for DevOps Engineers.

    Day 23 Task: Jenkins Freestyle Project for DevOps Engineers.

    Introduction In the dynamic realm of software development, Continuous Integration (CI) and Continuous Delivery (CD)…

  • Day 22 - Getting Started with Jenkins

    Day 22 - Getting Started with Jenkins

    Understanding Jenkins: At its core, Jenkins is an open-source automation server, a DevOps tool crafted in the Java…

  • Day 21 - Docker Important Interview Questions

    Day 21 - Docker Important Interview Questions

    Introduction In the dynamic landscape of DevOps engineering, Docker has emerged as a cornerstone technology. Docker…

  • Day 20 - Docker Cheat Sheet

    Day 20 - Docker Cheat Sheet

    Docker is a powerful platform that enables developers to create, deploy, and run applications in containers. Whether…