DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Implementing CI/CD Pipelines With Jenkins and Docker
  • How To Use Docker Volume To Build Angular Application With Jenkins Pipeline
  • Java CI/CD: From Local Build to Jenkins Continuous Integration

Trending

  • How AI Agents Are Transforming Enterprise Automation Architecture
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • Unlocking AI Coding Assistants: Generate Unit Tests
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. How To Build Docker Images in Docker Hub Using Jenkins Pipeline

How To Build Docker Images in Docker Hub Using Jenkins Pipeline

Learn how to set up your environment, create your first Jenkins Pipeline, define said pipeline, and run your pipeline and build images.

By 
Sudip Sengupta user avatar
Sudip Sengupta
DZone Core CORE ·
Tariq Siddiqui user avatar
Tariq Siddiqui
·
Updated Mar. 07, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
54.8K Views

Join the DZone community and get the full member experience.

Join For Free

Jenkins Pipeline is a powerful tool to automate your deployments. Flexible and customized actions split between stages are a good reason to try this feature.

Building your own Docker Image and uploading it to Docker Hub to keep your repository updated is a good example of understanding how Jenkins Pipeline can improve your way of work.

Let's walk through the steps for building Docker images inside the Docker Hub using Jenkins Pipeline and see the benefits of using Docker Hub.

Prerequisites

  • A server with Jenkins and Docker running on it (Jenkins user should be allowed to run Docker)
  • GitHub account
  • Docker Hub account

Why Use Docker Hub?

Building Docker images within Docker Hub offers several benefits. It is especially beneficial for implementing Continuous Integration (CI) and Continuous Deployment (CD) systems for software development. The following are some of the advantages of using Docker Hub for deploying Docker images.

  • Collaboration: Custom Docker images can be shared among different developers working on the same project, thus it helps in better collaboration within a team.
  • Automation: Docker Hub simplifies the process of development pipelines and workflows and ensures continuous Docker image updates.
  • Security: Docker Hub provides an essential security to help protect your docker image against known vulnerabilities.
  • Portability: Docker images can be run on any environment or platform that supports Docker.

How To Build Docker Images in Docker Hub Using Jenkins Pipeline

1. Setting Up Your Environment

Install the Docker Pipelines plugin on Jenkins:

Manage Jenkins → Manage Plugins.

Search Docker Pipelines, click on Install without restart, and wait until is done.

Upload your Dockerfile definition to your GitHub repository. Click on the green button, "Clone or Download," and copy the URL because you will need it later.

Example Github repository


On Jenkins, you need to create a new credential with your Docker Hub account details. Go to Credentials → Global → Add credentials, and fill out the form with your username and password. Fill in ID and Descriptions. Note that if you set the ID, you will need this specific ID to refer this credential from your scripts. Here we are just using dockerhub_id.

Docker Hub Id

2. Creating Your First Jenkins Pipeline

Now, we are ready to create our first pipeline. On Jenkins go to New Item → Pipeline, type the name you want for this Pipeline project, and then click OK.

Creating Jenkins pipeline


Following that, you can skip all General and Build Trigger options and go straight to the Pipeline section. Here, you can include a Pipeline definition (usually named Jenkinsfile), or you can refer to an external location like Git or Subversion.

3. Defining a Jenkins Pipeline

Consider the below code snippet where we are declaring Jenkins pipeline to build a Docker image from GitHub repository:

Plain Text
 




x
35


 
1
pipeline { 
2
    environment { 
3
        registry = "YourDockerhubAccount/YourRepository" 
4
        registryCredential = 'dockerhub_id' 
5
        dockerImage = '' 
6
    }
7
    agent any 
8
    stages { 
9
        stage('Cloning our Git') { 
10
            steps { 
11
                git 'https://github.com/YourGithubAccount/YourGithubRepository.git' 
12
            }
13
        } 
14
        stage('Building our image') { 
15
            steps { 
16
                script { 
17
                    dockerImage = docker.build registry + ":$BUILD_NUMBER" 
18
                }
19
            } 
20
        }
21
        stage('Deploy our image') { 
22
            steps { 
23
                script { 
24
                    docker.withRegistry( '', registryCredential ) { 
25
                        dockerImage.push() 
26
                    }
27
                } 
28
            }
29
        } 
30
        stage('Cleaning up') { 
31
            steps { 
32
                sh "docker rmi $registry:$BUILD_NUMBER" 
33
            }
34
        } 
35
    }
36
}



Please note that you need to modify the above code with your specific Docker Hub and GitHub details: Here, the pipeline we are defining has four stages:

  • The first one is to get the Dockerfile from our GitHub repository.
  • The second one will build the image using $BUILD_NUMBER to tag the version.
  • The third one is pushing the built image to your Docker Hub registry.
  • Finally, we will cleanup the previously built image on the local server.

4. Run Your Pipeline and Build Images

Now, we are ready to run the Pipeline and check the output if an error is present on any stage during the run.
Go to your Pipeline project on Jenkins and click on Build Now to run manually. You should get a sequential output of the different stages similar to this one:

Docker pipeline



If everything is fine, you can check your Docker Hub repository for a new image tagged with the Jenkins build version matching with your Docker Hub registry:

Docker Hub repository



This was a basic example of how to work with Pipelines and integrate different components of your deployments.

Final Thoughts

In this article, we illustrated a simple procedure for building up Docker images using Jenkins within Docker Hub. There are possibilities for creating numerous complex integrations through pipelines as you go ahead.

Some ideas to go further with Jenkins:

  • Define a webhook to run the pipeline when a commit is submitted to your GitHub repository.
  • Include several containers in the same pipeline to keep different stages (like backend and frontend) or different environments (dev/prod)
  • Set a notification by Email/Telegram/Slack with the status and/or output of your pipeline.

As you can see, a lot of options are easily achievable. Just keep experimenting, and you will gain a deeper understanding of Jenkins Pipeline orchestration. Thanks for reading!

Docker (software) Pipeline (software) Jenkins (software)

Published at DZone with permission of Sudip Sengupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Implementing CI/CD Pipelines With Jenkins and Docker
  • How To Use Docker Volume To Build Angular Application With Jenkins Pipeline
  • Java CI/CD: From Local Build to Jenkins Continuous Integration

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!