Jenkins Declarative Pipeline with Docker

Jenkins Declarative Pipeline with Docker

Day 27 Task

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 trainwithshubham/django-app:latest'
            }
        }
    }

Task-01

  • 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

Step 1--> In Jenkins, to create a new pipeline job, click on "New Item" in the Jenkins dashboard, then select "Pipeline" as the project type.

Step 2--> After clicking "OK," you can provide a description for the new pipeline job.

Step 3--> In the configuration of the pipeline job, navigate to the "Pipeline Script" section and define your stages, steps, and parameters accordingly.

pipeline {
    agent any
    stages{
        stage ('Code Clone') {
            steps {
                git url : 'https://github.com/ArnavS1999/django-todo-cicd.git' , branch : 'develop'
            }
        }
        stage ('Build') {
            steps {
                sh 'docker build . -t  django-todo-cicd:latest'
            }
        }
        stage ('Testing') {
            steps {
                echo 'testing'
            }
        }
        stage ('Deploy') {
            steps {
                sh 'docker run -d -p 8000:8000 django-todo-cicd:latest'
            }
        }
    }
}

Inside the stages block:

  • The Code Clone stage uses the git step to clone the code from the specified GitHub repository (https://github.com/ArnavS1999/django-todo-cicd.git) and the develop branch.

  • The Build stage uses the sh step to build a Docker image named django-todo-cicd:latest using the current directory (.).

  • The Deploy stage uses the sh step to run the django-todo-cicd:latest Docker container in detached mode (-d) and maps port 8000 on the host to port 8000 in the container.

Step 4--> "Save" and click on "Build Now" to start the pipeline.

Step 5--> Console Output.

Step 6--> Verify whether we can browse the application or not.

Step 7--> We will face errors in case of running a job twice, as the docker container is already created, so for that do task 2.


Task-02

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

  • Complete your previous projects using this Declarative pipeline approach.

Step 1--> We can address the error by utilizing docker-compose and modify the script to incorporate the docker-compose down and docker-compose up commands.

Step 2--> Click on "Save" and then click on "Build Now".

Step 3--> Once the build is successful, check whether our application is working or not.

This approach helps manage containers more effectively, ensuring a clean environment before deployment and minimizing potential conflicts.


Thank you for reading. I hope you were able to understand and learn something new from my blog.

Happy Learning!

Please follow me on Hashnode and do connect with me on LinkedIn ArnavSingh.

Did you find this article valuable?

Support Arnav Singh by becoming a sponsor. Any amount is appreciated!