Build Docker Images with Jenkins Pipeline and Docker Hub
Automating Docker image building and deployment is a critical step in modern development workflows. Using Jenkins Pipeline, you can seamlessly build, tag, and push Docker images to Docker Hub, optimizing your Continuous Integration (CI) and Continuous Deployment (CD) processes. In this guide, we’ll walk through the steps of using Jenkins to build Docker images and upload them to Docker Hub, all while highlighting the benefits of using Docker Hub for automation and collaboration.

Prerequisites for Building Docker Images
Before we dive into the steps, ensure you have the following set up:
- Jenkins and Docker installed: Your Jenkins server should have Docker running, with appropriate permissions for the Jenkins user to run Docker commands.
- GitHub account: You’ll need a repository to store your Dockerfile.
- Docker Hub account: This will allow you to store and share your built Docker images.
Why Use Docker Hub?
Using Docker Hub for managing and deploying Docker images provides several advantages, especially for implementing CI/CD pipelines. Here’s why Docker Hub is a great choice:
- Collaboration: Docker Hub allows you to share custom Docker images with team members, making collaboration easier in multi-developer environments.
- Automation: It streamlines development workflows by automating the process of updating Docker images and integrating them into your CI/CD pipelines.
- Security: Docker Hub offers essential security features, helping protect your images from known vulnerabilities.
- Portability: Docker images built on Docker Hub can be easily run on any platform that supports Docker, ensuring consistent application deployment across environments.
How to Build Docker Images in Docker Hub Using Jenkins Pipeline
Step 1: Set Up Your Environment
- Install the Docker Pipelines Plugin on Jenkins
First, ensure that Jenkins has the Docker Pipelines plugin installed. To do this:- Go to Manage Jenkins > Manage Plugins.
- Search for Docker Pipelines and click Install without restart.
- Upload Your Dockerfile to GitHub
Ensure your Dockerfile is stored in a GitHub repository. Click on the green “Clone or Download” button on GitHub and copy the repository URL. - Create Jenkins Credentials for Docker Hub
In Jenkins, add your Docker Hub credentials:- Go to Credentials > Global > Add Credentials.
- Fill in your Docker Hub username and password, and assign an ID (e.g., dockerhub_id) for easy reference in your pipeline script.
Step 2: Create Your First Jenkins Pipeline
- Create a New Pipeline
- In Jenkins, go to New Item and choose Pipeline.
- Name your pipeline project and click OK.
- Configure the Pipeline
Skip the General and Build Trigger options, and proceed to the Pipeline section. Here, you can either define your pipeline directly or reference an external Jenkinsfile stored in Git or Subversion.
Step 3: Define the Jenkins Pipeline
Here’s a basic Jenkins pipeline script to build and deploy Docker images from a GitHub repository:
pipeline {
environment {
registry = "YourDockerhubAccount/YourRepository"
registryCredential = 'dockerhub_id'
dockerImage = ''
}
agent any
stages {
stage('Cloning our Git') {
steps {
git 'https://github.com/YourGithubAccount/YourGithubRepository.git'
}
}
stage('Building our image') {
steps {
script {
dockerImage = docker.build registry + ":$BUILD_NUMBER"
}
}
}
stage('Deploy our image') {
steps {
script {
docker.withRegistry( '', registryCredential ) {
dockerImage.push()
}
}
}
}
stage('Cleaning up') {
steps {
sh "docker rmi $registry:$BUILD_NUMBER"
}
}
}
}
This pipeline has four stages:
- Cloning: Pulls the Dockerfile from GitHub.
- Building: Uses Jenkins to build the Docker image and tag it with the build number.
- Deploying: Pushes the image to Docker Hub.
- Cleanup: Removes the local image to free up space.
Step 4: Run Your Pipeline and Build Images
Once you’ve defined your pipeline, you can run it manually:
- Go to your pipeline project on Jenkins and click Build Now.
- The build process will start, and you can check the output for errors during each stage.
Once the pipeline completes successfully, you should find the new Docker image tagged with the Jenkins build number in your Docker Hub repository.
Final Thoughts on Jenkins Pipelines
This guide provided a simple workflow for using Jenkins Pipeline to automate Docker image building and deployment to Docker Hub. However, there are many ways to expand and enhance this process, such as:
- Webhooks: Set up a webhook to automatically trigger the pipeline whenever a new commit is pushed to your GitHub repository.
- Multi-container Pipelines: Manage complex applications by adding multiple containers to the same pipeline for different environments (e.g., backend and frontend).
- Notifications: Integrate email, Slack, or Telegram notifications to alert your team of build statuses.
Jenkins provides a flexible and powerful tool for automating your deployment workflows. The more you experiment with pipelines, the more you’ll learn about the orchestration and customization possible.
ZippyOPS Services
If you’re looking for expert assistance in DevOps, DataOps, or Cloud-based solutions, ZippyOPS offers a wide range of consulting, implementation, and managed services to optimize your infrastructure. Our services include:
- Microservices and Infrastructure management for scalable architectures
- AIOps and Automated Ops for improved operational efficiency
- Security solutions to safeguard your applications
Explore our Services, Solutions, and Products, or check out our YouTube channel for more demo videos.
For tailored advice, email us at sales@zippyops.com.



