Jenkins Docker Pipeline: Build and Push Docker Image
The Jenkins Docker pipeline simplifies automating the build and deployment of Docker images. In this guide, we’ll walk you through setting up a Jenkins pipeline that fetches code from a private GitHub repository, builds a Docker image, and pushes it to Docker Hub.

Setting Up Jenkins for Docker Image Building
Before diving into the pipeline creation, ensure your Jenkins environment is configured for Docker:
- Install Docker on Jenkins:
Follow Docker’s official installation guide to install Docker on Ubuntu 18. - Grant Jenkins Access to Docker:
Allow Jenkins to access Docker commands by running:sudo usermod -aG docker jenkins sudo systemctl restart jenkins - Verify Docker Access:
Log into Jenkins as the Jenkins user and check Docker functionality:sudo su - jenkins docker ps
Once Docker access is confirmed, proceed with creating the Jenkins pipeline.
Creating Your Jenkins Docker Pipeline
To automate Docker image creation and pushing, follow these steps to configure your Jenkins pipeline:
- Create a New Pipeline Job:
From the Jenkins dashboard, click “New Item,” select “Pipeline,” and give the job a name. - Configure GitHub Integration:
In the Build Triggers section, enable “GitHub hook trigger for GITScm polling” to trigger the build on code changes. - Set Up SCM Configuration:
Select “Pipeline script from SCM” and provide the following details:- SCM: Git
- Credentials: Use the credentials for accessing your private GitHub repo.
- Branch: Choose the branch you want to build (e.g.,
master). - Script Path: Specify the location of the
Jenkinsfilein your repo.
Writing the Jenkinsfile
The Jenkinsfile is a key part of the Jenkins pipeline. Here’s an example pipeline script to build the Docker image:
pipeline {
agent any
stages {
stage('Checkout Source') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Docker Build') {
steps {
script {
docker.build('zippyops01/javaapp-training')
}
}
}
stage('Docker Push') {
steps {
script {
docker.login('zippyops01', 'password')
docker.push('zippyops01/javaapp-training:latest')
}
}
}
stage('Cleanup') {
steps {
sh 'docker rmi zippyops01/javaapp-training:latest'
}
}
}
}
This pipeline consists of:
- Checkout Source: Clones the GitHub repository.
- Build: Runs Maven to compile the Java application.
- Docker Build: Builds the Docker image using the
Dockerfile. - Docker Push: Pushes the Docker image to Docker Hub.
- Cleanup: Removes the local Docker image after the push.
Example Dockerfile
Here is a sample Dockerfile you can use to build the Docker image:
FROM tomcat:8.0-alpine
LABEL maintainer="zippyops@gmail.com"
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./target/newapp-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["catalina.sh", "run"]
This Dockerfile:
- Uses Tomcat as the base image.
- Removes any default applications in the Tomcat
webappsdirectory. - Copies the
.warfile built by Maven. - Exposes port 8080 and runs the Tomcat server.
Pushing the Image to Docker Hub
After building the Docker image, Jenkins will push it to Docker Hub. The following commands are used to log in and push the image:
docker login -u zippyops01 -p ******** https://index.docker.io/v1/
docker push zippyops01/javaapp-training:latest
Cleaning Up Unused Docker Images
To prevent the accumulation of unused images, add a cleanup step to remove the Docker image once it’s pushed:
docker rmi zippyops01/javaapp-training:latest
Conclusion on Creating Jenkins Docker pipeline
Automating Docker image builds and deployments with Jenkins is an effective way to streamline your development pipeline. With this Jenkins Docker pipeline, you can automatically build, test, and push Docker images whenever code changes are pushed to GitHub.
If your organization is looking to optimize DevOps workflows or enhance security through DevSecOps, ZippyOPS provides expert consulting, implementation, and managed services in various fields including Cloud, AIOps, MLOps, Infrastructure, and Security. Contact us at sales@zippyops.com for tailored solutions.



