0
0
Jenkinsdevops~7 mins

Docker Pipeline plugin in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Building and running Docker containers inside Jenkins pipelines can be complex. The Docker Pipeline plugin simplifies this by providing easy commands to build, run, and manage Docker images and containers directly in your Jenkins pipeline scripts.
When you want to build a Docker image as part of your Jenkins build process.
When you need to run tests inside a Docker container during a Jenkins pipeline.
When you want to push a Docker image to a registry automatically after a successful build.
When you want to clean up Docker containers and images after pipeline execution to save space.
When you want to use Docker containers as build agents inside Jenkins pipelines.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build Docker Image') {
      steps {
        script {
          docker.build('my-app-image:1.0')
        }
      }
    }
    stage('Run Container') {
      steps {
        script {
          docker.image('my-app-image:1.0').inside {
            sh 'echo Running inside Docker container'
          }
        }
      }
    }
    stage('Push Image') {
      steps {
        script {
          docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
            docker.image('my-app-image:1.0').push()
          }
        }
      }
    }
  }
}

This Jenkinsfile defines a pipeline with three stages:

  • Build Docker Image: Uses docker.build to create a Docker image named my-app-image:1.0.
  • Run Container: Runs commands inside a container created from the built image using docker.image(...).inside.
  • Push Image: Pushes the Docker image to Docker Hub using docker.withRegistry with stored credentials.
Commands
This command builds a Docker image named 'my-app-image' with tag '1.0' from the current directory. It is the manual equivalent of the Jenkins docker.build step.
Terminal
docker build -t my-app-image:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 5.12kB Step 1/3 : FROM alpine:3.14 ---> a24bb4013296 Step 2/3 : RUN echo Hello from Dockerfile ---> Running in 123abc456def Removing intermediate container 123abc456def ---> 789xyz123abc Step 3/3 : CMD ["echo", "Hello World"] ---> Running in 456def789xyz Removing intermediate container 456def789xyz Successfully built 789xyz123abc Successfully tagged my-app-image:1.0
-t - Tags the image with a name and version
Runs a container from the 'my-app-image:1.0' image and removes it after it exits. This simulates the Jenkins docker.image(...).inside step.
Terminal
docker run --rm my-app-image:1.0
Expected OutputExpected
Hello World
--rm - Automatically removes the container after it exits
Logs into Docker Hub so you can push images. In Jenkins, this is handled by docker.withRegistry with stored credentials.
Terminal
docker login -u myusername -p mypassword
Expected OutputExpected
Login Succeeded
Pushes the 'my-app-image:1.0' image to Docker Hub or another registry.
Terminal
docker push my-app-image:1.0
Expected OutputExpected
The push refers to repository [docker.io/library/my-app-image] 1.0: Pushed
Key Concept

If you remember nothing else from this pattern, remember: the Docker Pipeline plugin lets you build, run, and push Docker images inside Jenkins pipelines with simple commands that wrap Docker CLI operations.

Common Mistakes
Not configuring Docker credentials in Jenkins before pushing images.
Without credentials, the push command will fail with authentication errors.
Store Docker registry credentials in Jenkins and reference them in docker.withRegistry.
Using docker.build without specifying a tag.
The image will be built with the default 'latest' tag, which can cause confusion or overwrite existing images.
Always specify a meaningful tag like 'my-app-image:1.0' to track versions.
Running docker.image(...).inside without ensuring the Docker daemon is accessible to Jenkins agents.
The container will fail to start if Jenkins cannot communicate with Docker.
Make sure Jenkins agents have Docker installed and configured properly.
Summary
Use docker.build in Jenkinsfile to build Docker images during pipeline runs.
Use docker.image(...).inside to run commands inside containers from those images.
Use docker.withRegistry with stored credentials to push images to Docker registries.