Skip to main content
In this lesson, you’ll learn how to configure a Jenkins pipeline integrated with Docker, enabling continuous integration and deployment for your applications. Before you start, ensure Docker is installed on your Jenkins server so that it can access the Docker CLI. For installation instructions, please refer to the Docker documentation page.
Make sure Docker is installed and properly configured on your Jenkins server before proceeding.

Setting Up Jenkins Credentials

The first step is to configure Jenkins with your Docker Hub credentials. Create credentials within Jenkins using the “Username with password” type. Enter your Docker Hub account name as the username and your Docker Hub password as the password. In this example, the credentials are named “Docker creds”.
The image shows a Jenkins interface where new credentials are being added, including fields for username, password, and ID. The username is filled in as "sanjeevkr720" and the ID as "docker".

Creating a New Pipeline

Next, navigate back to the Jenkins dashboard and create a new pipeline named “Docker pipeline”. In the pipeline configuration screen, make the following selections:
  • Enable “GitHub hook trigger for GITScm polling”.
  • Choose “Pipeline script from SCM”.
  • Select Git as your SCM and provide the URL of your repository.
  • Set the branch to “main” and keep the default path to your Jenkinsfile.
The image shows a configuration screen for setting up a Docker pipeline, with options to define a pipeline script from SCM using Git, and a prompt to enter a Git repository URL.

The Jenkinsfile Pipeline Script

Your repository must contain a Jenkinsfile that defines the pipeline stages. In this example, the pipeline executes the following tasks:
  • Installs dependencies using pip.
  • Runs tests with pytest.
  • Logs into Docker Hub using the credentials configured earlier.
  • Builds a Docker image.
  • Pushes the Docker image to Docker Hub.
Below is an improved and consolidated version of the Jenkinsfile:
Once you commit this Jenkinsfile to your repository and push your changes, Jenkins will automatically trigger the pipeline. The pipeline will:
  • Check out your code.
  • Install required packages.
  • Run unit tests using pytest.
  • Log into Docker Hub.
  • Build a Docker image.
  • Push the newly built image to your Docker Hub repository.

Build Output Examples

During the build process, you may see console outputs such as the following when installing dependencies:
When building the Docker image, you may see output similar to the following:
Once the image is built, the pipeline pushes it to Docker Hub. You can also push the image manually with a command like:
A typical push output might include:

Verifying the Commit and Docker Image Tag

Tagging your Docker images with the Git commit hash creates a direct link between the image and a specific commit in your repository. This makes it easy to track deployments back to the source code changes. For example, checking the commit history on GitHub can help you verify that the correct changes have been deployed.
The image shows a GitHub repository page displaying a list of commits for a project named "course-jenkins-project" under the user "kodekloudhub." Each commit entry includes a message, author, and timestamp.
If you view a commit diff, you might see something like this:
The image shows a GitHub commit page for a project, displaying changes in files with additions and deletions highlighted in a diff view.
After making changes (for example, updating the version to “version four”), commit and push the modifications. Jenkins will trigger another build, and your updated Docker image—now tagged with the new commit hash—will be pushed to Docker Hub.

Viewing the Docker Image on Docker Hub

Finally, you can verify the pushed Docker image on Docker Hub. The Docker Hub interface displays details such as the manifest digest, OS/architecture, compressed size, and image layers.
The image shows a Docker Hub page displaying details of a Docker image, including its manifest digest, OS/architecture, compressed size, and image layers.
This completes the process of integrating Docker into your Jenkins CI/CD pipeline, enabling streamlined testing, building, and deployment of your applications.

Watch Video

Practice Lab