How to push a Docker Image to the Docker Hub using Jenkins Pipeline – CI CD

by Aug 16, 2022IoT Continuous Integration

Why do you need to use a Jenkins pipeline to push your docker image into your docker hub? Assuming you have a docker file and you build your docker images manually and push it to your hub. Suppose your developer needs to make changes or commits in the GitHub repository once in a while; the current application might not be in sync with the one that is in the Docker hub. If the developer commits in the repository periodically, for instance, on a daily basis, then you need to automate building a new docker image every time. Remember, your docker file is constant, but the docker image is not. This changes every time when you build a new image. The docker image comes with a tag and an ID. This blog explains the whole process of building a docker image and pushing it to the docker hub using the Jenkins pipeline. You can also automate this periodically.

Click here for Jenkins tutorials.

I have created a ReactJs application with 3 web pages and provided it in the GitHub repository.

Click this link for your Repository.

We will use this repository for our Jenkins pipeline as SCM. You also need accounts for GitHub, docker, and Jenkins.

Step 1: Jenkins – Plugins to download

You need to install plugins for the docker in Jenkins. Go to Manage Jenkins > Manage Plugins > Available and install 5 plugins: search for these following plugins and install them,

  1. Docker API Plugin
  2. Docker Commons Plugin
  3. Docker Pipeline
  4. Docker Plugin
  5. docker-build-step

Step 2: Jenkins – Add your Credentials

You have to add your docker account’s username and password to your Jenkins account.

Go to Manage Jenkins > Manage Credentials > System > Global credentials (unrestricted) > Add Credentials

Choose Kind as Username with password and Scope as Global; provide your docker username and password with a unique ID of your own. This ID is used for referring the username with a password later in your pipelines.

You have to add GitHub credentials and give your GitHub repository URL. Make sure you have a Dockerfile on your repository. (The repository link from this blog has one already!)

Step 3: Jenkins – Create a Pipeline

Select Pipeline from the list, and give a suitable name for your pipeline project. Click OK.

Step 4: Write your Pipeline Script

  1. On your pipeline project, go to Configure for writing your pipeline script.
  2. We will now write this script in the pipeline box, Select your Definition, as Pipeline.

node {   
    stage('Clone repository') {
        git credentialsId: 'git', url: 'https://github.com/Monishamacharla/reactapp'
    }
    
    stage('Build image') {
       dockerImage = docker.build("monishavasu/my-react-app:latest")
    }
    
 stage('Push image') {
        withDockerRegistry([ credentialsId: "dockerhubaccount", url: "" ]) {
        dockerImage.push()
        }
    }    
}
  1. First of all, you are cloning your git repo from the URL mentioned. This can be done by adding the credentials of your git account in your Jenkins, the Same as Docker.
  2. Next, You will build the image, assuming you already have your dockerfile on your repository on the main directory, also you have to create a repository on your docker account, here I have given the name “my-react-app”. This will build your docker image
  3. After this, you have to push the image into your docker hub. Now is the time to provide your docker credentials ID that you have created before.
  4. You can schedule your jobs periodically too.

Basically, you can also write a separate file for your pipeline script and add it to your repository, this file is called as Jenkinsfile. The Jenkinsfile is like a text file similar to the contents present on the pipeline script.

Step 5: Build

Save your Pipeline script, and then click Build Now.

Go to your console output and check the messages.

Step 5: Check your Docker Hub

Go to your repository in the docker hub, and check your latest docker image built and pushed by the Jenkins.

The next step would be deploying the container in a testing environment, you can use Amazon Elastic Container Services or even Kubernetes.

Happy learning!

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Comparison Between MongoDB and MySQL

Comparison Between MongoDB and MySQL

In this article, we are going to discuss comparison between MongoDB and MySQL. World is experiencing fourth industrial revolution. These revolution comprised of Artificial Intelligence (AI), Block chain, Cloud Computing and Internet of Thing (IoT) technologies. Data...

Plotly R vs Python Dash: Best of the bunch

Plotly R vs Python Dash: Best of the bunch

Introduction Plotly R vs Python Dash both are managed and developed by Plotly for data visualization. But there are some differences in the way you use them. The R graphing library from Plotly allows users to create interactive, publication-quality graphs. Line plots,...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....