0
0
Jenkinsdevops~5 mins

Building Docker images in pipeline in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Building Docker images in a pipeline automates creating container images from your app code. This helps package your app with all its parts so it runs the same everywhere.
When you want to automatically create a Docker image every time you update your app code.
When you need to test your app inside a container before sending it to production.
When you want to store your app image in a registry for easy sharing and deployment.
When you want to speed up your development by automating image builds.
When you want to ensure consistency by building images in a controlled environment.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build Docker Image') {
      steps {
        script {
          docker.build('my-app-image:1.0')
        }
      }
    }
  }
}

This Jenkinsfile defines a pipeline with one stage called 'Build Docker Image'.

The docker.build command builds a Docker image named my-app-image with tag 1.0 from the Dockerfile in the current directory.

The agent any means Jenkins can run this pipeline on any available agent.

Commands
Check that Jenkins CLI or Jenkins environment is ready before running the pipeline.
Terminal
jenkins-jobs --version
Expected OutputExpected
jenkins-jobs 3.3.1
Manually build the Docker image locally to verify the Dockerfile works before automating in Jenkins.
Terminal
docker build -t my-app-image:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 8.192kB Step 1/5 : FROM python:3.9-slim ---> 123abc456def Step 2/5 : COPY . /app ---> Using cache Step 3/5 : WORKDIR /app ---> Using cache Step 4/5 : RUN pip install -r requirements.txt ---> Using cache Step 5/5 : CMD ["python", "app.py"] ---> Using cache Successfully built abcdef123456 Successfully tagged my-app-image:1.0
-t - Assigns a name and tag to the image
Trigger the Jenkins pipeline named 'my-pipeline' that contains the Docker build step.
Terminal
jenkins build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Build Docker Image) [Pipeline] script [Pipeline] { [Pipeline] docker.build [Pipeline] } [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
Verify that the Docker image was successfully built and is available locally after the pipeline runs.
Terminal
docker images | grep my-app-image
Expected OutputExpected
my-app-image 1.0 abcdef123456 2 minutes ago 120MB
Key Concept

If you remember nothing else from this pattern, remember: automating Docker image builds in Jenkins pipelines ensures consistent, repeatable packaging of your app.

Common Mistakes
Not having a Dockerfile in the project directory before running the build.
Docker build fails because it needs instructions from the Dockerfile to create the image.
Always include a valid Dockerfile in the root or specified directory before building.
Using 'docker.build' without the correct image name and tag format.
Jenkins will fail to tag the image properly, causing confusion or overwriting images.
Use a clear image name and tag like 'my-app-image:1.0' in the docker.build command.
Running the Jenkins pipeline on an agent without Docker installed.
The build step will fail because Docker commands cannot run without Docker engine.
Ensure the Jenkins agent has Docker installed and configured before running the pipeline.
Summary
Write a Jenkinsfile with a stage that uses docker.build to create your Docker image.
Test your Dockerfile locally with 'docker build' to catch errors early.
Run the Jenkins pipeline to automate image building on code changes.
Verify the image exists locally after the pipeline completes.