Build a Docker Jenkins Pipeline to Implement CI/CD Workflow

Âdithya K
4 min readFeb 17, 2022

--

In this tutorial, we are going to see the continuous integration and continuous delivery workflow. How the code commit in GitHub automatically trigger the job in Jenkins Creates the docker image to push the Docker image to the Docker hub repository. It should also pull the Docker image and run it as a Docker container.

I am going to use the public repo of the docker hub.

In this article, we are going to see the following topics.

  1. Creating Repo in GitHub
  2. Creating Webhook on GitHub
  3. Configuring Docker Credentials in Jenkins
  4. Creating Jenkins Pipeline

Creating Repo in GitHub

First, we need a GitHub account. It is free. So use the below link to create GitHub account.

Once you successfully Created a account, create a new repository by providing some name under text box Repository name.

Once the repository is Created Create two files as mentioned below

GitHub Repo

Dockerfile

FROM ubuntu:latest
RUN apt update && apt upgrade -y
RUN apt install nodejs -y
RUN node -v

The above of Dockerfile will pull the latest ubuntu image and install NodeJS and print the node version

Jenkinsfile

pipeline {
environment {
imagename = “adithyak21/jenkins-docker”
registryCredential = ‘adithya-docckerhub’
dockerImage = ‘’
}
agent any
stages {
stage(‘Cloning Git’) {
steps {
git([url: ‘https://github.com/adithyak21/simple-docker.git', branch: ‘main’])
}
}
stage(‘Building image’) {
steps{
script {
dockerImage = docker.build imagename
}
}
}
stage(‘Running image’) {
steps{
script {
sh “docker run ${imagename}:latest”
}
}
}
stage(‘Deploy Image’) {
steps{
script {
docker.withRegistry( ‘’, registryCredential ) {
dockerImage.push(“$BUILD_NUMBER”)
dockerImage.push(‘latest’)
}
}
}
}
}
}

The above job is segmented as five stages including environment .

variable imagename need to be entered as <Docker hub username>/<image name>

Creating Webhook on GitHub

Go to the Settings->Webhooks in your repo and Add Webhook

Type your https://jenkinsURL/github-webhook/

If you are using Jenkins in local host change the jenkins URL with address:port number. The final URL will look like in this format http://address:port/github-webhook/.

Choose Content-Type as application/json and choose Let me select individual events select Pull Requests and Pushes

Trigger events

Whenever you do a pull or push in the repo, GitHub will inform Jenkins. Ensure that your Jenkins URL is accessible from GitHub.

Update the changes in the repo and push the changes

git add . ; git commit -m “jenkins test” ; git push

Configuring Docker Credentials in Jenkins

Before creating Jenkins pipeline need to configure Credential in Jenkins

Then click to Global credentials

Click Add Credentials on the left menu.

Choose Username and Password for dropdown kind.

Create username and password credentials for Docker Hub login.

credentials

Creating Jenkins pipeline

Click on New item for creating pipeline.

New Jenkins Pipeline

Give a name and select Pipeline

Configure Pipeline and enable GitHub hook trigger for GITScm polling at Build Triggers.

pipeline settings
pipeline settings

After a successful job the docker image can be found in docker hub

docker hub

Summary

In this tutorial, we have seen how to push the Docker image to Docker Hub. Docker follows the Domain registry concept to push the images. Docker hub is one of the providers for Docker images. In DevOps, we need to automate the image build and pushing to the cloud repository provider. Jenkins is a better choice to automate the Docker image build and pushing.

I planned to write more about DevOps and associated tools. I hope this article will be helpful. Stay tuned for more articles.

Thank you for reading…

--

--