Building a Docker Image with a Jenkins Pipeline: A Step-by-Step Guide
Creating an efficient CI/CD pipeline is crucial for DevOps teams. In this guide, we’ll walk you through the process of building a Docker image from a private GitHub repository and pushing it to Docker Hub using a Jenkins pipeline. Whether you’re working with microservices or cloud-native applications, this process ensures seamless integration and deployment.

Prerequisites: Setting Up Your Environment
Before diving into the pipeline, make sure your Jenkins environment is configured properly. Follow these steps to set up Jenkins with Docker support:
- Install Docker Engine
Install Docker-CE on your Jenkins instance. You can follow the official guide for installing Docker on Ubuntu. - Install Necessary Plugins
Ensure that you have installed the Git, Docker, and Docker Pipeline plugins in Jenkins. - Allow Jenkins User to Access Docker Socket
To enable Jenkins to interact with Docker, run the following commands:sudo usermod -aG docker jenkins sudo systemctl restart jenkins - Test Docker Access
Test whether the Jenkins user has Docker access:sudo su - jenkins docker ps
Creating Your First Jenkins Pipeline
Once the environment is set up, you’re ready to create your first Jenkins pipeline. Follow these steps:
- In Jenkins, go to New Item > Pipeline, name your project, and click OK.
- In the Build Triggers section, enable the GitHub hook trigger for GITScm polling.
- For the Pipeline Definition, choose Pipeline script from SCM.
- Select Git as the SCM, and enter your repository URL and credentials.
- Specify the branch you want to build (e.g.,
origin/master) and the Script Path, which is where yourJenkinsfileis located.
Now you’re ready to define the steps in your pipeline.
Defining the Jenkins Pipeline in the Jenkinsfile
A simple Jenkinsfile will guide Jenkins through the necessary steps to build the Docker image. Here’s an example of how your Jenkinsfile might look:
pipeline {
agent any
environment {
imagename = 'yourusername/yourapp'
registryCredential = 'dockerhub'
}
tools {
git 'Default'
maven 'maven-3'
dockerTool 'docker'
}
stages {
stage('Checkout Source') {
steps {
checkout scm
}
}
stage('Build') {
steps {
script {
sh "mvn clean install"
}
}
}
stage('Docker Build') {
steps {
script {
docker.build(imagename)
}
}
}
stage('Push Docker Image') {
steps {
script {
docker.withRegistry('', registryCredential) {
docker.image(imagename).push("${env.BUILD_ID}")
}
}
}
}
stage('Clean Up') {
steps {
sh "docker rmi ${imagename}:${BUILD_ID}"
}
}
}
}
Explanation of the Pipeline Stages:
- Checkout Source: This stage checks out your code from GitHub using Jenkins’ built-in SCM feature.
- Build: It uses Maven to build the application.
- Docker Build: This stage builds a Docker image using the
docker.build()command. - Push Docker Image: Pushes the built Docker image to Docker Hub.
- Clean Up: Removes the Docker image from the Jenkins environment to free up resources.
Deploying with Kubernetes
If you’re using Kubernetes for deployment, the next step is to configure your pipeline to deploy the application. Here’s how to integrate Kubernetes deployment into the pipeline:
- Create a
deploy.ymlKubernetes configuration file with the necessary specifications. - Use the
kubernetesDeploystep in the pipeline:stage('Deploy to Kubernetes') { steps { script { sh "sed -i s/latest/$BUILD_ID/g $WORKSPACE/deploy.yml" kubernetesDeploy(configs: 'deploy.yml', kubeConfig: [path: 'kubeconfig'], secretName: 'my-k8s-secret') } } }
This step ensures that your application is automatically deployed to Kubernetes after the Docker image is pushed.
Test and Monitor Your Pipeline
Once you’ve configured your Jenkins pipeline, it’s time to run it. Test the pipeline by making changes to your repository and pushing them. Jenkins will automatically trigger the pipeline and provide feedback in the console output.
Why ZippyOPS Can Help with DevOps Pipelines
Building, testing, and deploying applications with Jenkins pipelines can be complex. ZippyOPS offers consulting, implementation, and managed services for teams looking to streamline their DevOps processes. Whether you’re building microservices, managing cloud infrastructure, or optimizing your security posture, ZippyOPS can assist with DevOps, Cloud, Automated Ops, AIOps, and more.
ZippyOPS also offers customized MLOps, DataOps, and DevSecOps solutions to help teams improve their software development lifecycle and ensure smooth operations. You can explore our solutions and services on our website:
For more insights, check out our ZippyOPS YouTube Channel.
Conclusion
In this tutorial, we’ve demonstrated how to set up a Jenkins pipeline to build and push Docker images from a private GitHub repository to Docker Hub. Integrating Kubernetes deployment into this pipeline ensures that your application is quickly and efficiently deployed to your desired environment.
Need help with optimizing your DevOps workflows? Reach out to ZippyOPS for expert consulting and managed services.
Contact us at: sales@zippyops.com



