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 it is 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 devops/django-app:latest'
            }
        }
    }

Task-01:

1)Create a docker-integrated Jenkins declarative pipeline

Step 1: In Jenkins, Click on New Item, and select "Pipeline" as the new job type.

Step 2: Configure Pipeline:

After selecting Pipeline, you will select a section where you can define the pipeline configuration.

  • Pipeline Script: This is a simple script editor where you can write your pipeline script directly.

  • Pipeline from SCM: You can define your pipeline script in a version control repository (e.g., Git) and have Jenkins fetch and execute it.

    Step 3: Now go to Jenkins Pipeline and Build the Pipeline.

    Step 4: Access your application on the browser as shown below

    Step 5: It's working. Now if we run the job again it will fail because Jenkins tries to build a container but as we already have a running container with the same name and the same exposed port, creates a conflict. To see this click on build now and see the console output. Ref below screenshot

    To overcome this we use a docker-compose configuration file. Let's look at how to create a project of docker-compose with declarative pipelines in the next task.

Task-02:

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

We already have a docker-compose.yml configuration file in our repository, so we have to use docker-compose commands to up and down the service containers.

  1. Click on Save and then click on Build Now

  1. Check the "Console Output"

This is how we create declarative pipelines in Jenkins.

Happy Learning!!!!!!!!