Day 27 Task: Jenkins Declarative Pipeline with Docker

Day 27 Task: 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 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

Prerequisite for this task 01

Please follow the Day 26 and Day 24 task blog. In this two blog, you will see how to connect GitHub repository to Jenkins to a server and how to write decalarative pipeline.

Step 1: You need to install docker on your server

sudo apt-get install docker.io -y

to confirm run these command

sysytemctl status docker

Step 2: Give permission to jenkins by running these commands and also reboot the server and reconnect it

sudo usermod -aG docker jenkins
sudo reboot

when you do this Jenkins automatically logouts login to it again

Step 3: In the pipeline run these commands and save

pipeline {
    agent any
    stages {
        stage("code") {
            steps {
                git url: "https://github.com/Gurucharan716/react_django_demo_app.git", branch: "main"
            }
        }
        stage("build") {
            steps {
                sh "docker build . -t react-django-app"
            }
        }
    }
}

Step 5: Now click on build now you can see the pipeline is running successfully

now go to console output you can see we have successfully created docker image through Jenkins pipeline

to confirm run the commands in terminal

sudo docker images

we have completed task 01 now move on to task 02


Task-02

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

  • You won't face errors, you can Follow this documentation

  • Complete your previous projects using this Declarative pipeline approach

  • In case of any issues feel free to post on any Groups, Discord or Telegram

Step 1: Now we will continue the task 01 by running the docker image in the pipeline area add this commands

pipeline {
    agent any
    stages {
        stage("code") {
            steps {
                git url: "https://github.com/Gurucharan716/react_django_demo_app.git", branch: "main"
            }
        }
        stage("build") {
            steps {
                sh "docker build . -t react-django-app"
            }
        }
        stage("run") {
            steps {
                sh "docker run -d -p 8001:8001 react-django-app:latest"
            }
        }
    }
}

Step 2: Now click on Build now you can see its running successfully

Step 3: Go to console output here you can see it's successfully finished

Now go to the instance and open port 8001 because in docker file we have used 8001 port number

Now copy the instance IP address and add ":8001" and paste in google chrome like this

Now you can see we have successfully deployed react django app and also complete task 02 as well


Thank you for reading this blog and if any queries or if any corrections to be done in this blog please let me know.

contact us in Linkedin ,Twitter or email-id