Zum Inhalt springen

Day 27: Jenkins Declarative Pipeline with Docker

Day 27 is where Jenkins pipelines meet Docker. I’ll break this down step by step to complete both tasks (with shell commands and with Groovy Docker syntax).

Task-01: Jenkins Declarative Pipeline using sh (shell commands)

Here we manually run Docker commands inside the pipeline.

Steps:

  • Pre-checks

    • Jenkins server must have Docker installed (docker --version).
    • Jenkins user must have Docker permissions (either part of the docker group or run as sudo).
    • Have a Dockerfile in your project repo.
  • Jenkinsfile (sh approach)

    Create a Jenkinsfile in your repo with this content:

pipeline {
    agent any

    stages {
        stage('Build Docker Image') {
            steps {
                sh 'docker build -t myapp:latest .'
            }
        }
        stage('Run Container') {
            steps {
                // If already running, stop it first
                sh '''
                    docker ps -q --filter "name=myapp" | grep -q . && docker stop myapp && docker rm myapp || true
                    docker run -d --name myapp -p 8080:8080 myapp:latest
                '''
            }
        }
    }

    post {
        success {
            echo '✅ Docker image built and container running!'
        }
        failure {
            echo '❌ Something went wrong with Docker pipeline.'
        }
    }
}
  • docker build -t myapp:latest . → builds your image from Dockerfile.
  • docker run ... → runs container (stops/removes old one to avoid duplicate container errors).

Task-02: Jenkins Declarative Pipeline using Docker Groovy Syntax

Instead of shell commands, Jenkins Pipeline DSL provides Docker syntax which handles cleanup automatically.

Steps:

  • Jenkins needs the Docker Pipeline plugin installed.

    • Dashboard → Manage Jenkins → Manage Plugins → Install „Docker Pipeline“.
  • Jenkinsfile (Docker DSL approach)

pipeline {
    agent any

    stages {
        stage('Build and Run with Docker DSL') {
            steps {
                script {
                    // Pull or build an image
                    def myImage = docker.build("myapp:latest")

                    // Run inside a container
                    myImage.inside {
                        sh 'echo "✅ Running inside Docker container"'
                        sh 'python --version || echo "Python not installed in base image"'
                    }
                }
            }
        }
    }
}

Here’s what’s happening:

  • docker.build("myapp:latest") → builds the image from Dockerfile in the repo.
  • .inside {} → runs your build/test steps inside the container.
    This avoids container name conflicts because Jenkins cleans up automatically.

Comparison

  • Task-1 (sh commands): More control, but prone to container conflicts (needs manual stop/rm).
  • Task-2 (Docker DSL): Cleaner, avoids conflicts, recommended for pipelines with multiple builds.

The goal of this task is to

Use your Docker Build and Run Knowledge

  • docker build – we can use sh ‚docker build . -t ‚ in the pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.

  • docker run: we can use sh ‚docker run -d ‚ in the pipeline stage block to build the container.

Next steps

  • Run both pipelines (day27-sh-docker and day27-dsl-docker jobs).
  • Confirm container is running → docker ps on Jenkins server.
  • Extend the pipeline later with Test and Deploy stages.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert