GitLab: How to build Docker images in your GitLab CI
This documentation is part of the CI/CD guide. You can view the complete guide here: How to use GitLab CI/CD.
👋 Welcome to the Stackhero documentation!
Stackhero offers a ready-to-use GitLab cloud solution designed for teams seeking a fast, secure, and scalable environment:
- Unlimited users, repositories, data transfers, and CI/CD processing time for complete flexibility.
- Effortless updates with a single click, keeping your environment up to date with no downtime.
- Custom domain name secured with HTTPS (for example, https://git.your-company.com) for a professional image and enhanced security.
- Consistent performance and robust security on your own private, dedicated infrastructure. No noisy neighbours and full isolation guaranteed.
- Choice of hosting location: 🇪🇺 Europe or 🇺🇸 USA to meet your compliance or latency requirements.
Get started quickly and focus on your code. Stackhero's GitLab cloud hosting solution is up and running in about 5 minutes.
If your project repository contains Dockerfile files, you can automate the building, running, and, if needed, publishing of your Docker images to a registry.
Step 1: Enable Docker in Docker (DinD) support
To begin, enable "Docker in Docker" (DinD) support from your Stackhero dashboard.

Enabling DinD support presents a security risk, especially if you want to isolate your users and prevent them from accessing each other's projects.
Step 2: Configure the GitLab CI pipeline
Next, update your gitlab-ci.yml file to include a pipeline configuration that builds your Dockerfile using DinD. Here is an example configuration:
image: docker:29
build:
stage: build
services:
- name: docker:29-dind
alias: docker
variables:
# Point the Docker CLI at the Docker-in-Docker service, via its plain
# (non-TLS) port. See the note below about why DOCKER_HOST matters.
DOCKER_HOST: "tcp://docker:2375"
DOCKER_TLS_CERTDIR: ""
before_script:
- docker info
script:
# Replace "my-docker-image" with the name of your desired image:
- docker build -t my-docker-image .
# Optionally, test the Docker image:
# - docker run my-docker-image /script/to/run/tests
The docker:29-dind service starts a Docker daemon alongside your job, and DOCKER_HOST tells the docker CLI to use it. Always set DOCKER_HOST when you declare a docker:dind service: if you omit it, the CLI silently connects to a different daemon, and your build may succeed even if the service is misconfigured, masking real issues. DOCKER_TLS_CERTDIR: "" allows the use of the plain, non-TLS port 2375 on the internal job network.
As a simpler alternative, you can remove the services block and both variables entirely: Stackhero's runner already exposes a ready-to-use Docker daemon via a mounted socket, so a simple docker build command works immediately.
For more information on building Docker images with GitLab CI, refer to the official GitLab documentation.