Day 27 Task: Jenkins Declarative Pipeline with Docker Project

Vibhuti JainVibhuti Jain
2 min read

Task-01

  • Create a docker-integrated Jenkins declarative pipeline

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

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

  1. Use your Docker Build and Run

  • 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.

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

Git Repository - https://github.com/Vibhuti456/to-do-app

Create a new pipeline project and named it

Pipeline Script -

Console Output -

  • Now in the "Project Configuration" section, we have to define a pipeline script in the definition.
pipeline
{
    agent 
    {
        node 
        {
             label "dev-server"
        }
    }
    stages
    {
        stage("Github Cloning")
        {
            steps 
            {
                git url: "https://github.com/Vibhuti456/to-do-app.git", branch: "master"
                echo "Cloning has completed"
            }
        }
        stage("Networking Creation")
        {
            steps
            {
                sh "docker network create todo-network"
                echo "Network has been created"
            }
        }
        stage("Running MYSQL")
        {
            steps
            {
                sh "docker run -d --name db --network todo-network -e MYSQL_ROOT_PASSWORD=rootpass -e MYSQL_DATABASE=todo mysql:5.7"
                sh "docker ps -a"
                echo "Container has created"
            }
        }
        stage("Build Flask Image")
        {
            steps
            {
                sh "docker build -t todo-app ."
                echo "Image has been created"
            }
        }
        stage("Deploy Application")
        {
            steps
            {
                sh "docker run -d -p 5000:5000 --name flask-app --network todo-network -e MYSQL_HOST=db todo-app:latest"
                sh "docker ps -a"
                echo "Deploy has been completed"
            }
        }


    }
}

Now click on Builld now button -

Application in web browser -

Task-2

  • 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.

To solve this issue we can use a docker image to build the application and for that, we have to install Docker Pipeline & Docker in Jenkins.

pipeline
{
    agent
    {
        node 
        {
            label "dev-server"
        }
    }
    stages
    {
        stage("Cloning")
        {
            steps
            {
                git url: "https://github.com/Vibhuti456/to-do-app.git",  branch: "master"
                echo "Cloned the appliction from github"
            }
        }
        stage("Building Image")
        {
            steps 
            {
                echo "Image started building......"
                script
                {
                    docker.build("todo-app:v1")
                }
                echo "Image build successfully"
            }
        }
        stage("Deploy")
        {
            steps
            {
                sh "docker run -d -p 5001:5000 --name flask-app1 --network todo-network -e MYSQL_HOST=db todo-app:v1"
            }
        }
    }
}

Now click on Builld now button -

0
Subscribe to my newsletter

Read articles from Vibhuti Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vibhuti Jain
Vibhuti Jain

Hi, I am Vibhuti Jain. A Devops Tools enthusiastic who keens on learning Devops tools and want to contribute my knowledge to build a community and collaborate with them. I have a acquired a knowledge of Linux, Shell Scripting, Python, Git, GitHub, AWS Cloud and ready to learn a lot more new Devops skills which help me build some real life project.