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 that provides a host of benefits, including:

  • Unlimited users, repositories, transfers, and CI/CD processing time.
  • Effortless updates with just a click.
  • Customisable domain name secured with HTTPS (for example, https://git.your-company.com).
  • Optimal performance and robust security powered by a private and dedicated VM.
  • Available in 🇪🇺 Europe and 🇺🇸 USA.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's GitLab cloud hosting solution!

If your project repository includes Dockerfile files, you can automate the process of building, running, and, if needed, publishing Docker images to a registry.

To start, enable "Docker in Docker" (DinD) support in your Stackhero dashboard.

Enabling DinD support presents a security risk, especially if you want to isolate your users and avoid them accessing each other's projects.

Next, update your gitlab-ci.yml file to include a pipeline configuration that builds your Dockerfile using DinD. Below is an example configuration:

image: docker:20.10.21

variables:
  DOCKER_TLS_CERTDIR: "/certs"

services:
  - docker:20.10.21-dind

before_script:
  - docker info

build:
  stage: build
  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

For additional guidance on building Docker images with GitLab CI, consult the official GitLab documentation.