Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

Jenkins Docker Pipeline: Build and Push Docker Image

Jenkins Docker Pipeline: Build and Push Docker Image

The Jenkins Docker pipeline simplifies automating the build and deployment of Docker images. In this guide, we’ll walk you through setting up a Jenkins pipeline that fetches code from a private GitHub repository, builds a Docker image, and pushes it to Docker Hub.

Jenkins Docker pipeline flow with GitHub integration and Docker Hub deployment

Setting Up Jenkins for Docker Image Building

Before diving into the pipeline creation, ensure your Jenkins environment is configured for Docker:

  1. Install Docker on Jenkins:
    Follow Docker’s official installation guide to install Docker on Ubuntu 18.
  2. Grant Jenkins Access to Docker:
    Allow Jenkins to access Docker commands by running: sudo usermod -aG docker jenkins sudo systemctl restart jenkins
  3. Verify Docker Access:
    Log into Jenkins as the Jenkins user and check Docker functionality: sudo su - jenkins docker ps

Once Docker access is confirmed, proceed with creating the Jenkins pipeline.

Creating Your Jenkins Docker Pipeline

To automate Docker image creation and pushing, follow these steps to configure your Jenkins pipeline:

  1. Create a New Pipeline Job:
    From the Jenkins dashboard, click “New Item,” select “Pipeline,” and give the job a name.
  2. Configure GitHub Integration:
    In the Build Triggers section, enable “GitHub hook trigger for GITScm polling” to trigger the build on code changes.
  3. Set Up SCM Configuration:
    Select “Pipeline script from SCM” and provide the following details:
    • SCM: Git
    • Credentials: Use the credentials for accessing your private GitHub repo.
    • Branch: Choose the branch you want to build (e.g., master).
    • Script Path: Specify the location of the Jenkinsfile in your repo.

Writing the Jenkinsfile

The Jenkinsfile is a key part of the Jenkins pipeline. Here’s an example pipeline script to build the Docker image:

pipeline {
    agent any

    stages {
        stage('Checkout Source') {
            steps {
                checkout scm
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Docker Build') {
            steps {
                script {
                    docker.build('zippyops01/javaapp-training')
                }
            }
        }
        stage('Docker Push') {
            steps {
                script {
                    docker.login('zippyops01', 'password')
                    docker.push('zippyops01/javaapp-training:latest')
                }
            }
        }
        stage('Cleanup') {
            steps {
                sh 'docker rmi zippyops01/javaapp-training:latest'
            }
        }
    }
}

This pipeline consists of:

  1. Checkout Source: Clones the GitHub repository.
  2. Build: Runs Maven to compile the Java application.
  3. Docker Build: Builds the Docker image using the Dockerfile.
  4. Docker Push: Pushes the Docker image to Docker Hub.
  5. Cleanup: Removes the local Docker image after the push.

Example Dockerfile

Here is a sample Dockerfile you can use to build the Docker image:

FROM tomcat:8.0-alpine
LABEL maintainer="zippyops@gmail.com"
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./target/newapp-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["catalina.sh", "run"]

This Dockerfile:

  • Uses Tomcat as the base image.
  • Removes any default applications in the Tomcat webapps directory.
  • Copies the .war file built by Maven.
  • Exposes port 8080 and runs the Tomcat server.

Pushing the Image to Docker Hub

After building the Docker image, Jenkins will push it to Docker Hub. The following commands are used to log in and push the image:

docker login -u zippyops01 -p ******** https://index.docker.io/v1/
docker push zippyops01/javaapp-training:latest

Cleaning Up Unused Docker Images

To prevent the accumulation of unused images, add a cleanup step to remove the Docker image once it’s pushed:

docker rmi zippyops01/javaapp-training:latest

Conclusion on Creating Jenkins Docker pipeline

Automating Docker image builds and deployments with Jenkins is an effective way to streamline your development pipeline. With this Jenkins Docker pipeline, you can automatically build, test, and push Docker images whenever code changes are pushed to GitHub.

If your organization is looking to optimize DevOps workflows or enhance security through DevSecOps, ZippyOPS provides expert consulting, implementation, and managed services in various fields including Cloud, AIOps, MLOps, Infrastructure, and Security. Contact us at sales@zippyops.com for tailored solutions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top