Day 27 of 90 Days of DevOps Challenge: Jenkins Declarative Pipeline with Docker

Welcome to Day 27 of the 90 Days of DevOps challenge! 🎉 Today, we’re taking our Jenkins Declarative Pipeline skills to the next level by integrating Docker. This step is critical because combining Jenkins with Docker allows us to streamline our build and deployment process.

Let’s dive in! 🚀


Task-01: Create a Docker-Integrated Jenkins Declarative Pipeline

Now that we’ve worked with Declarative Pipelines, it’s time to leverage our Docker knowledge. Docker allows us to containerize applications and Jenkins lets us automate the build, test, and deploy process.

Docker Build Command in Jenkins Pipeline

You can use the sh block in Jenkins to execute shell commands. To build a Docker image in your pipeline, you’ll run the following command inside a stage:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    sh 'docker build -t hello-world .'
                }
            }
        }       
        stage('Run') {
            steps {
                script {
                    sh 'docker run --rm hello-world'
                }
            }
        }
    }
}

Here is how your Dockerfile might look like:

# Use an official lightweight base image
FROM alpine:latest

# Set working directory (optional)
WORKDIR /app

# Create a simple shell script directly within the Dockerfile
RUN echo '#!/bin/sh\n echo "Hello, World!"' > hello.sh

# Make the script executable
RUN chmod +x hello.sh

# Default command to run when container starts
CMD ["sh", "./hello.sh"]

What’s Happening Here?

  • Docker Build: In the "Build" stage, the command docker build -t <image-name> . creates a Docker image from your Dockerfile. The -t flag tags the image.

  • Docker Run: In the "Run" stage, docker run -d <image> runs the container using the image you just built and maked sure the container is removed as soon as it ends executing.

Important Note: If you try to run the pipeline multiple times without cleaning up, you’ll likely encounter errors because the container already exists. We’ll address this in the next task.

Task-02: Use Docker Groovy Syntax in Jenkins Pipeline

A better approach to avoid container-related issues is to use Jenkins’ Docker Groovy syntax. This method will automatically handle the container lifecycle, ensuring you don’t run into issues when running the pipeline multiple times.

Here’s how your pipeline might look:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    docker.build('hello-world')
                }
            }
        }

        stage('Run') {
            steps {
                script {
                    docker.image('hello-world').run('--rm')
                }
            }
        }
    }
}

Why Use the Docker Groovy Syntax?

  • The docker Groovy syntax integrates Docker into your pipeline more seamlessly.

  • It automatically pulls the image, builds it, and runs it within the pipeline, avoiding conflicts with pre-existing containers.

  • You no longer have to worry about cleaning up containers manually, as Jenkins takes care of it.


Conclusion

Today’s task was all about integrating Docker into your Jenkins Declarative Pipeline. You’ve learned how to build Docker images and run containers within the Jenkins pipeline using both basic sh commands and the more advanced Docker Groovy syntax.

Take some time to revisit previous projects and refactor them using this approach. Doing so will solidify your skills and prepare you for more complex CI/CD pipelines.

Feel free to share your progress with the DevOps community using #90DaysOfDevOps.

Keep pushing forward and happy learning! 🚀