Skip to main content

Command Palette

Search for a command to run...

Project - 1. Push Docker Image to Docker Hub using Jenkins Pipeline

Updated
•2 min read
Project - 1. Push Docker Image to Docker Hub using Jenkins Pipeline
S

👋 Hello! I'm passionate about DevOps & AWS Cloud. I have over 1.4 years of experience in IT Security and Cloud field. I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!

OS: Linux/Unix, Mac, Windows Cloud: AWS Containers: Docker Version Control: GIT, GitHub, GitLab, Bitbucket. CI/CD Tools: Jenkins Container Orchestration: Kubernetes Languages: Python, PowerShell, Shell scripting IAC: Terraform, Ansible, Chef, AWS Cloud Formation Web: Apache, Nginx, Tomcat

Open to opportunities. 🌱 #DevOps #AWS #CI/CD #Containers #Python #IaC #Cloud #Blogging #OpenToOpportunities

📢🎡Pre-requisites

  1. EC2 Instance t2.micro

  2. Install Docker

  3. Docker Hub Account

  4. Install Jenkins

  5. Install the Plugin and Set the Credentials

  6. Create a job

Working Steps:-

Step-1. Launch (Ubuntu) Ec2 instance t2.micro

A. Go to AWS Console and Click on search box and type Ec2

B. Click on Launch Instance

C. Type the Instance Name and Choose the Ubuntu Machine

D . Create Key

E. Check (HTTP, HTTPS) box and click launch instance

F. Copy Public Ip and Access your Instance

ssh ubuntu@IP -i "key_name"

Step-2. Install Docker

sudo apt install docker.io
sudo chmod 777 /var/run/docker.sock
sudo usermod -aG docker jenkins

Step-3. Create a Docker Hub Account

Step-4. Install Jenkins

sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Debian-1deb12u1, mixed mode, sharing)
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

Access Jenkins on browser

# your instance IP and port 8080
241.15.54.55:8080

Step-5. Install the plugins and set the Credentials

A. Go to Manage Jenkins and Click on Plugins

B. Choose required Plugins

C. Successfully Installed

D. Set Credentials

Step-6. Create a Job

A. Click to new item

B. Give job name and choose pipeline options

C. Write pipeline Code and paste the pipeline box

pipeline{
    agent any

    environment {
        APP_NAME = "complete-pro"
        RELEASE = "latest"
        DOCKER_USER = "fir3eye"
        DOCKER_PASS = 'dockerhub'
        IMAGE_NAME = "${DOCKER_USER}" + "/" + "${APP_NAME}"
        IMAGE_TAG = "${RELEASE}-${BUILD_NUMBER}"

    }
    stages{
        stage("Cleanup Workspace"){
            steps {
                cleanWs()
            }
        }
        stage("Checkout from SCM"){
            steps {
                git branch: 'master', credentialsId: 'github', url: 'https://github.com/shazforiot/nodeapp_test.git'
            }

        }
        stage("Build & Push Docker Image") {
            steps {
                script {
                    docker.withRegistry('',DOCKER_PASS) {
                        docker_image = docker.build "${IMAGE_NAME}"
                    }

                    docker.withRegistry('',DOCKER_PASS) {
                        docker_image.push("${IMAGE_TAG}")
                        docker_image.push('latest')
                    }
                }
            }

        }
    }
}

D. Click on the Build Button

E. See the Result

More from this blog

Sudheer sen's blog

17 posts

👋 Hello! I'm passionate about DevOps & AWS Cloud. I have over 2 years of experience in IT Security and Cloud field. I'm proficient in a variety of cutting-edge technologies.