Certified Jenkins Engineer

Containerization and Deployment

Demo Push to Registry

In this tutorial, you’ll extend your Jenkins CI pipeline to push a Docker image to Docker Hub. We assume you have already:

  • Built the Docker image.
  • Scanned it for vulnerabilities using Trivy.

Let’s configure Jenkins to authenticate with Docker Hub and push the image.

Prerequisite

Make sure your Jenkins agent has Docker installed and the Docker daemon is accessible by the Jenkins user.

Stage: Push Docker Image

Begin by adding a Push Docker Image stage to your Jenkinsfile. A minimal example:

stage('Push Docker Image') {
  steps {
    sh 'docker push siddharth67/solar-system:$GIT_COMMIT'
  }
}

If you run this now, Jenkins will error out because it’s not logged in to Docker Hub.

Install the Docker Pipeline Plugin

To enable registry authentication and image operations in a Jenkins Pipeline, install the Docker Pipeline plugin.

The image shows a webpage for the Docker Pipeline plugin on the Jenkins website, detailing its documentation, version, installation statistics, and related links.

This plugin provides the following key methods and global variables:

Method / VariableDescription
dockerNamespace for Docker operations (build, run, push, etc.)
withDockerRegistryWraps steps inside a login session for a container registry
registryReference to a configured registry endpoint
imageCreates or references a Docker image object in the pipeline

The image shows a webpage displaying documentation for Jenkins Pipeline Syntax, specifically focusing on global variables related to Docker functions. It includes descriptions of methods like `withRegistry`, `withServer`, and `image`.

Generate the withDockerRegistry Snippet

  1. In Jenkins, navigate to Pipeline Syntax > Snippet Generator.
  2. Under Docker Pipeline, select withDockerRegistry.

The image shows a Jenkins interface with a "Snippet Generator" for creating pipeline scripts, highlighting the "withDockerContainer" option to run build steps inside a Docker container.

  1. Switch to the Docker Registry snippet. Configure the registry URL and credentials.

The image shows a Jenkins Pipeline Syntax page with options for configuring a Docker registry endpoint, including fields for the Docker registry URL and registry credentials.

Add Docker Hub Credentials

Create Docker Hub credentials in Jenkins:

The image shows a Jenkins Credentials Provider interface where a user is configuring credentials with options for kind, scope, username, and password. The interface includes fields for entering an ID and description.

FieldDescriptionExample
KindCredentials typeUsername with password
IDUnique Jenkins ID for lookupdocker-hub-credentials
UsernameDocker Hub account usernamesiddharth67
PasswordDocker Hub password or access token••••••••

Final Jenkinsfile Configuration

Update your Push Docker Image stage to wrap the push command in withDockerRegistry:

stage('Push Docker Image') {
  steps {
    withDockerRegistry(credentialsId: 'docker-hub-credentials', url: '') {
      sh 'docker push siddharth67/solar-system:$GIT_COMMIT'
    }
  }
}

Note

Leaving url: '' uses the default Docker Hub endpoint (https://index.docker.io/v1/).

Commit and push these changes. Jenkins will now authenticate and push the image:

$ docker push siddharth67/solar-system:cf1715a460f1bcb02618528326bd84f70f6a0
The push refers to repository [docker.io/siddharth67/solar-system]
4a0d352d35f4: Preparing
0a471d608574: Preparing
804d07a05ede: Layer already exists
f3b328347c79: Layer already exists

Verifying on Docker Hub

After the pipeline finishes, log in to Docker Hub to confirm your repository and tag:

The image shows a Docker Hub interface displaying a list of repositories under the user "siddharth67," with options to search, create repositories, and manage organizations.

You should see the new image tag matching your git commit hash.

Confirm in GitHub

You can also verify the commit ID in your source repository:

The image shows a code repository interface with a list of commits, branches, and files. It highlights a recent commit on the "feature/enabling-cicd" branch.

Summary

With this configuration, your CI pipeline now:

  • Builds a Docker image.
  • Scans it with Trivy for vulnerabilities.
  • Authenticates and pushes the image to Docker Hub.

Next, we’ll cover automated deployment in a future guide.

References

Watch Video

Watch video content

Previous
Demo Vulnerability Scan using Trivy