Skip to main content
In this guide, we will walk through the process of pushing a Docker image to a container registry using Jenkins. After building the image and performing a vulnerability scan with Trivy, follow these steps to update your Jenkins pipeline and securely push your image to Docker Hub.

Updating the Jenkins Pipeline

To begin, modify your Jenkinsfile by adding a new stage dedicated to pushing the Docker image. Start with a simple stage that builds the Docker image:
Building the image is just the first step. In order to push your image to Docker Hub, you’ll need to log in with proper credentials. This is achieved through the use of the Docker Pipeline plugin, which simplifies credential management and registry interactions.

Installing the Docker Pipeline Plugin

To take full advantage of Jenkins’ capabilities with Docker, install the Docker Pipeline plugin by navigating to the Plugins section in Jenkins. This plugin provides functions for handling Docker images, configuring registries, and accessing global variables.
The image shows a webpage from the CloudBees documentation, specifically focusing on the "Global Variable Reference" for the Docker Pipeline plugin. It includes a sidebar with navigation links and detailed text about Docker-related functions in a pipeline script.
For more detailed information, refer to the global variable reference documentation provided by the plugin.
The image shows a webpage displaying documentation for Jenkins Pipeline Syntax, specifically focusing on global variables related to Docker. It includes descriptions of methods like withRegistry, withServer, and image.

Configuring Docker Registry Credentials

For secure image pushes, configure your Docker Hub credentials in Jenkins:
  1. Go to the Credentials section.
  2. Create a new entry of type “Username with password.”
  3. Label the description as “Docker Hub credentials” for easy identification.
The default registry URL is set to index.docker.io/v1. If you are using a different container registry, make sure to specify its endpoint in your Jenkins configuration.
The image shows a Jenkins Credentials Provider interface where a user is configuring credentials with a username and password. Various credential options are visible in a dropdown menu.
Refer to the following documentation for configuring a Docker registry endpoint if needed:
The image shows a Jenkins Pipeline Syntax page for configuring a Docker registry endpoint, with options to set the Docker registry URL and credentials.

Modifying the Pipeline Stage for Registry Authentication

Once the credentials are in place, update your Jenkins pipeline stage to include Docker Hub authentication. Wrap the push command within a withDockerRegistry block, as seen below:
This configuration leverages the default Docker Hub registry and your pre-configured credentials to authenticate and push your Docker image.

Triggering and Verifying the Pipeline

After updating the Jenkinsfile, commit and push your changes to the repository. This action triggers the pipeline. During execution, you might see commands similar to the following in the build log:
Upon successful execution, log entries will confirm that the image has been pushed. An example log output might be:
In Docker Hub, verify the updated image tag, which commonly corresponds to the Git commit ID:
A sample output is as follows:
You can also verify the associated commit in GitHub:
The image shows a code repository interface with a list of files and recent commits. It highlights a branch named "feature/enabling-cicd" with a recent commit titled "Push Docker Image."

Summary

This article demonstrated how to build a CI pipeline in Jenkins that:
  • Builds a Docker image.
  • Executes a vulnerability scan using Trivy.
  • Pushes the Docker image to Docker Hub using the Docker Pipeline plugin with securely configured credentials.
These steps complete the continuous integration process, which also includes dependency installation and security testing. The next phase will cover deployment procedures. Happy building and automating your container workflows!

Watch Video