> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Push to Registry

> This tutorial extends a Jenkins CI pipeline to push a Docker image to Docker Hub after building and scanning it for vulnerabilities.

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](https://github.com/aquasecurity/trivy).

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

<Callout icon="lightbulb" color="#1CB2FE">
  Make sure your Jenkins agent has Docker installed and the Docker daemon is accessible by the Jenkins user.
</Callout>

## Stage: Push Docker Image

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

```groovy theme={null}
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.

<Frame>
  ![The image shows a webpage for the Docker Pipeline plugin on the Jenkins website, detailing its documentation, version, installation statistics, and related links.](https://kodekloud.com/kk-media/image/upload/v1752870517/notes-assets/images/Certified-Jenkins-Engineer-Demo-Push-to-Registry/docker-pipeline-plugin-jenkins-docs.jpg)
</Frame>

This plugin provides the following key methods and global variables:

| Method / Variable    | Description                                                 |
| -------------------- | ----------------------------------------------------------- |
| `docker`             | Namespace for Docker operations (build, run, push, etc.)    |
| `withDockerRegistry` | Wraps steps inside a login session for a container registry |
| `registry`           | Reference to a configured registry endpoint                 |
| `image`              | Creates or references a Docker image object in the pipeline |

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870518/notes-assets/images/Certified-Jenkins-Engineer-Demo-Push-to-Registry/jenkins-pipeline-syntax-docker-vars.jpg)
</Frame>

## Generate the withDockerRegistry Snippet

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

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870519/notes-assets/images/Certified-Jenkins-Engineer-Demo-Push-to-Registry/jenkins-snippet-generator-withdockercontainer.jpg)
</Frame>

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

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870520/notes-assets/images/Certified-Jenkins-Engineer-Demo-Push-to-Registry/jenkins-pipeline-docker-registry.jpg)
</Frame>

## Add Docker Hub Credentials

Create Docker Hub credentials in Jenkins:

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870521/notes-assets/images/Certified-Jenkins-Engineer-Demo-Push-to-Registry/jenkins-credentials-provider-interface.jpg)
</Frame>

| Field    | Description                         | Example                  |
| -------- | ----------------------------------- | ------------------------ |
| Kind     | Credentials type                    | Username with password   |
| ID       | Unique Jenkins ID for lookup        | `docker-hub-credentials` |
| Username | Docker Hub account username         | `siddharth67`            |
| Password | Docker Hub password or access token | `••••••••`               |

## Final Jenkinsfile Configuration

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

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

<Callout icon="lightbulb" color="#1CB2FE">
  Leaving `url: ''` uses the default Docker Hub endpoint (`https://index.docker.io/v1/`).
</Callout>

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

```bash theme={null}
$ 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:

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870523/notes-assets/images/Certified-Jenkins-Engineer-Demo-Push-to-Registry/docker-hub-repositories-siddharth67.jpg)
</Frame>

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:

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870524/notes-assets/images/Certified-Jenkins-Engineer-Demo-Push-to-Registry/code-repository-commits-branches.jpg)
</Frame>

## 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

* [Trivy – Aqua Security](https://github.com/aquasecurity/trivy)
* [Jenkins Docker Pipeline Plugin](https://www.jenkins.io/doc/book/pipeline/docker/)
* [Docker Hub](https://hub.docker.com/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/certified-jenkins-engineer/module/e16e4b93-31c4-479b-96b8-f0d26cde31cd/lesson/8aaf01ac-7a64-439d-b3d9-627781e6a7e1" />
</CardGroup>
