Jenkins + Docker Hub: Automate Build & Push Effortlessly

Jenkins + Docker Hub: Automate Build & Push Effortlessly

So far, we've explored various Docker concepts like how Docker works, Docker images, building images, Dockerfiles, Docker Hub, networks, volumes, layers, and caching.

In this article, we'll take it a step further by automating the entire process. Imagine no longer having to run commands repeatedly to build and push Docker images—Jenkins does it all with just one click!

Let’s dive into how we can automate the build and push process using Jenkins 🚀.

Requirements for Setup

We’ll need the following tools and accounts to get started:

1️⃣ Docker installed on our local machine.
2️⃣ A Docker Hub account to create a repository for storing Docker images.
3️⃣ GitHub, where we’ll maintain our project repository containing the Dockerfile and other project files.
4️⃣ Jenkins, which will handle the automation process.

In my previous articles, we’ve already covered setting up Docker and creating a Docker Hub repository. Now, let’s focus on setting up and maintaining the GitHub repository and configuring Jenkins for automation.

GitHub Project Setup:

To set up your project on GitHub, start by creating a new repository to host your project files. Once the repository is created, upload your Dockerfile along with any necessary project files.

This repository will serve as the source for Jenkins, where we will clone the code and build the Docker image directly from it.

Jenkins Setup:

  1. Install Jenkins: Install Jenkins locally by downloading it from the official Jenkins site.

  2. Start Jenkins: Access Jenkins through your browser, http://localhost:8080 or http://<your-server-ip>:8080

  3. Install Required Plugins:

    1. Login to the Jenkins Dashboard

    2. Navigate to Manage JenkinsManage Plugins

    3. Install the following plugins

      • Git Plugin (for repository integration)

      • Docker Pipeline Plugin (for Docker integration)

      • Credentials Binding Plugin (for securely managing the credentials)

  4. Setup Docker Hub Credentials

    • Go to Manage JenkinsManage Credentials.

    • Select the appropriate scope (e.g., Global credentials (unrestricted)).

    • Click on Add Credentials.

    • Select Username with password as the type.

    • Enter your Docker Hub username and password.

    • Add a unique ID (e.g., docker-hub-credentials) for reference in the Jenkins pipeline.

Configure New Pipeline Job

The Jenkins setup is now complete! We're ready to dive into the automation process. The first step is to create a new pipeline.

1️⃣ Create a new Job:

  • From the Jenkins dashboard, click on New Item.

  • Enter a name (e.g., Docker Pipeline).

  • Choose Pipeline as the project type.

2️⃣ Configure the Pipeline:

  • In the Pipeline section, select Pipeline script

  • Write your pipeline script (Jenkins file) to clone the GitHub repository, build the Docker image, and push it to Docker Hub.

3️⃣ Save the pipeline job

Example Jenkins file with Credentials

Here is an example of a Jenkins pipeline script:

pipeline {
    agent any
    environment {
        DOCKER_CREDENTIALS = credentials('docker-hub-credentials')
    }
    stages {
        stage('Clone Repository') {
            steps {
                git branch: 'main', url: 'https://github.com/your-repo/project.git'
            }
        }
        stage('Build Docker Image') {
            steps {
                sh 'docker build -t your-dockerhub-username/project-name:latest .'
            }
        }
        stage('Push to Docker Hub') {
            steps {
                sh '''
                docker login -u $DOCKER_CREDENTIALS_USR -p $DOCKER_CREDENTIALS_PSW
                docker push your-dockerhub-username/project-name:latest
                '''
            }
        }
    }
}

Accessing Docker Credentials
In the environment, Docker credentials are accessed using their ID for secure authentication.

Breaking Down the Steps:

1️⃣ First Stage: Cloning the Repository
In this stage, we define the GitHub branch to be cloned into our environment.

2️⃣ Second Stage: Building the Docker Image
Here, we build the Docker image from the cloned repository using the Dockerfile provided.

3️⃣ Third Stage: Pushing to Docker Hub
This stage involves logging into Docker Hub using credentials and pushing the built image to your Docker Hub repository.

Result

Done! Now, let's check the results and output of the build. Click on Build Now to trigger the pipeline, and monitor the logs for updates.

As we can see, Finished: SUCCESS indicates that the pipeline was executed successfully and all the stages were completed.

Each stage completed successfully:

✅ The repository is cloned.

✅ Docker image is built.

✅ Image is pushed to Docker Hub.

Outro

That's it, everyone! We've learned how to automate the build and push process, a crucial step in DevOps automation. Automating these tasks not only streamlines workflows but also enhances efficiency across teams.

If you think something is missing or if you have any questions, feel free to let me know!

Thank you for reading!! ✨