GitLab Runner: Building a Docker image with Docker-in-Docker

This documentation is part of the Building Docker images guide. You can view the complete guide here: Build and push Docker images from your GitLab CI/CD pipelines using your Stackhero runner and Docker-in-Docker.

👋 Welcome to the Stackhero documentation!

Stackhero offers you an easy-to-use GitLab Runner cloud solution, designed to efficiently run your GitLab CI/CD jobs. Here’s what you can expect:

  • Unlimited CI/CD minutes: there’s no per-minute billing, so your pipelines can run whenever you need them.
  • Concurrent jobs: run multiple jobs in parallel to speed up your entire pipeline.
  • The Docker executor with Docker-in-Docker support: simplify building and pushing your container images.
  • Compatible with GitLab.com as well as any self-managed GitLab instance.
  • A private, dedicated VM powered by fast NVMe/SSD disks for consistent, reliable builds.
  • Available in both 🇪🇺 Europe and 🇺🇸 USA regions.

Save time: connect your first GitLab Runner and start running pipelines in just a few minutes!

Here’s a sample .gitlab-ci.yml you can add to your repository. This configuration builds the Dockerfile located at the root of your project:

build-image:
  stage: build
  image: docker:27
  services:
    - docker:27-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  before_script:
    - docker info
  script:
    # Replace "my-image" with the name you want:
    - docker build -t my-image .
    # Optionally, you can run a quick smoke test on the built image:
    # - docker run --rm my-image /path/to/tests

In this configuration, the docker:27-dind service starts the Docker daemon. The variable DOCKER_TLS_CERTDIR: "/certs" enables a secure TLS connection between your job and the Docker daemon.