Skip to main content
In this lesson, we demonstrate how to build a Docker image within a Jenkins pipeline. After the SonarQube stage, the pipeline includes a stage to build the Docker image using the Docker CLI. The image is tagged with your Docker Hub username (siddharth67), the image name (solar-system), and the Git commit hash obtained from the environment variable GIT_COMMIT.
Ensure that your pipeline has executed a checkout stage before this build step so that the necessary environment variables, including the Git commit hash, are available.

Jenkinsfile Stage for Building the Docker Image

Below is the Groovy snippet for the Docker image build stage. It uses the docker build command to tag the image with the specified username, image name, and Git commit:
The complete pipeline structure, with the new stage added, is shown below:
In this updated pipeline, the printenv command is invoked to display all environment variables available in the pipeline. These variables include custom ones defined in the Jenkinsfile and built-in variables such as the Git commit hash, build ID, job URL, branch name, among others. This output is useful for debugging and ensuring that the correct values are being used.

Dockerfile for Building the Image

Next, review the Dockerfile that is used to build the Docker image. This file performs the following actions:
  • Uses a Node 18 Alpine base image.
  • Sets the working directory to /usr/app.
  • Copies package files and installs dependencies.
  • Copies the remaining source code.
  • Sets placeholder environment variables for MongoDB.
  • Exposes port 3000.
  • Starts the application using npm start.
Use a .dockerignore file to exclude unnecessary files and directories from the Docker build context, improving build performance.

The .dockerignore File

The .dockerignore file functions similarly to a .gitignore file and prevents certain files from being copied to the Docker image during the build. Below is an example of its contents:
This file helps speed up the build process by ensuring only necessary files are sent to the Docker build context.

Build Output

After saving and committing these changes, a new pipeline job is triggered. The build output will display the printenv command output along with the docker build command that uses the Git commit hash to build the image. Below is an example snippet from the build output:
The environment variables printed include key information such as job URL, build number, and workspace paths. The Git commit hash used in the Docker tag should correspond to the most recent commit in your repository. For instance, a GitHub log may display:
An extended build output example that shows image export details is provided below:
After a successful build, the image is ready to be pushed to Docker Hub. In the upcoming session, we will cover vulnerability scanning on the Docker image followed by pushing the image to Docker Hub.
The image shows a webpage displaying a list of global variables and their descriptions for a Jenkins pipeline syntax.
For a comprehensive overview of global environment variables available in Jenkins pipelines—including details such as the job display URL, build number, tag name, and branch name—refer to the Official Jenkins Pipeline Documentation. Thank you.

Watch Video