0
0
JenkinsHow-ToBeginner · 4 min read

How to Build Docker Image in Jenkins: Step-by-Step Guide

To build a Docker image in Jenkins, use a Jenkins pipeline with docker build command inside a sh step or use Jenkins Docker Pipeline plugin. This runs the Docker build process on the Jenkins agent and creates the image from your Dockerfile.
📐

Syntax

The basic syntax to build a Docker image in Jenkins pipeline is:

  • docker build -t <image-name> <path>: Builds the Docker image with a tag.
  • sh step: Runs shell commands on the Jenkins agent.
  • pipeline: Defines the Jenkins pipeline script.
groovy
pipeline {
  agent any
  stages {
    stage('Build Docker Image') {
      steps {
        sh 'docker build -t my-image:latest .'
      }
    }
  }
}
💻

Example

This example shows a Jenkins pipeline that builds a Docker image named my-app from the current directory's Dockerfile.

groovy
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }
    stage('Build Docker Image') {
      steps {
        sh 'docker build -t my-app:1.0 .'
      }
    }
  }
}
Output
[Pipeline] sh + docker build -t my-app:1.0 . Sending build context to Docker daemon 8.192kB Step 1/3 : FROM alpine:latest ---> a24bb4013296 Step 2/3 : RUN echo "Hello from Docker" ---> Running in 123abc456def Removing intermediate container 123abc456def ---> 789xyz123uvw Step 3/3 : CMD ["echo", "Hello from Docker"] ---> Running in 456def789abc Removing intermediate container 456def789abc ---> 321cba654fed Successfully built 321cba654fed Successfully tagged my-app:1.0
⚠️

Common Pitfalls

Common mistakes when building Docker images in Jenkins include:

  • Not having Docker installed or running on the Jenkins agent.
  • Missing permissions to run Docker commands.
  • Using docker build without specifying the correct context path.
  • Not checking out the source code before building.
groovy
pipeline {
  agent any
  stages {
    stage('Build Docker Image') {
      steps {
        // Wrong: no checkout, no context
        sh 'docker build -t my-app .'
      }
    }
  }
}

// Correct approach:
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }
    stage('Build Docker Image') {
      steps {
        sh 'docker build -t my-app .'
      }
    }
  }
}
📊

Quick Reference

Tips for building Docker images in Jenkins:

  • Ensure Docker is installed and Jenkins user has permission to run Docker.
  • Always checkout your code before building.
  • Use tags to version your images.
  • Use Jenkins pipeline sh step to run Docker commands.
  • Consider using Jenkins Docker Pipeline plugin for advanced features.

Key Takeaways

Use Jenkins pipeline with sh step to run docker build command.
Always checkout source code before building the Docker image.
Make sure Docker is installed and accessible on the Jenkins agent.
Tag your Docker images for easy versioning and tracking.
Check permissions to avoid Docker command failures in Jenkins.