services keyword makes ephemeral sidecar containers (like a Docker daemon or a test database) trivially easy. These notes cover the patterns that come up on almost every real project.
.gitlab-ci.yml Anatomy
Every pipeline is defined in a .gitlab-ci.yml at the root of the repository. The top-level keys you’ll use on every project:
Stage order matters — jobs in stage
n+1 only start after all jobs in stage n have passed. Jobs within the same stage run in parallel by default.Key Built-in Variables
GitLab injects these into every job automatically — no configuration required:Docker-in-Docker (DinD)
Runningdocker build inside a GitLab CI job requires a Docker daemon. The recommended approach is to use the official docker:dind service — GitLab spins it up as a sidecar container that the job container connects to over TLS.
How It Works
docker:dind service exposes the Docker socket over TCP (with TLS on port 2376). The job sets DOCKER_HOST to point at it.
Minimal DinD Job
Official Docs
GitLab’sservices keyword is documented at https://docs.gitlab.com/ci/services/. The services feature isn’t Docker-specific — you can use it to spin up PostgreSQL, Redis, or any other daemon your tests need.
Full Docker Build & Push Pipeline
This is a production-ready template for building a Docker image and pushing it to the built-in GitLab Container Registry on every push tomain, plus every MR:
Caching & Artifacts
These two features are often confused. The distinction is important:- cache
- artifacts
- cache + artifacts together
Cache speeds up jobs by persisting files between pipeline runs on the same runner. Use it for dependency directories (
node_modules, .venv, Maven’s ~/.m2).Cache is not guaranteed to be available — it’s a best-effort optimisation. Never rely on cache for correctness, only for speed.
Useful Patterns & Tips
- rules vs only/except
- needs (DAG)
- include & extends
Prefer
rules over the legacy only/except keywords — it’s more expressive and evaluates top-to-bottom:Related Pages
Docker Essentials
Commands, Dockerfiles, Compose, and networking — the building blocks used inside every pipeline.
Kubernetes
Deploy the images your GitLab pipeline builds into a Kubernetes cluster.