Skip to main content
In this guide, you’ll learn how to build Docker images inside a GitLab CI/CD pipeline using Docker-in-Docker (DinD). We’ll extend an existing CI configuration to include a containerization stage that compiles and inspects your Docker image.

Prerequisites

  • A GitLab Runner with Docker-in-Docker support enabled
  • CI/CD variables set for DOCKER_USERNAME, M_DB_PASSWORD, and any other secrets
  • A Docker Hub account configured to push images

CI/CD Pipeline Configuration

Add a containerization stage to your existing .gitlab-ci.yml. The new docker_build job will build and list your Docker image:

Variable Definitions

Make sure sensitive values like MONGO_PASSWORD are stored as masked CI/CD variables to avoid exposure in job logs.

Sample Dockerfile

Place this Dockerfile at the root of your repository to containerize a Node.js application:

Pipeline Execution Flow

When you push to main or open a feature merge request, GitLab:
  1. Launches the docker:24.0.5 image for the job.
  2. Starts the Docker-in-Docker service (docker:24.0.5-dind).
  3. Runs the script section to build and list the image:
The image shows a GitLab CI/CD pipeline interface with a job log for a Docker build process. The log details various steps of the build, including pulling Docker images and preparing the environment.
With this setup, your pipeline now builds a Docker image automatically. The next step is to add a job that runs container-based tests before pushing to Docker Hub.

Watch Video