TL;DR
A comprehensive guide to setting up, configuring, and optimizing GitLab Runners for efficient CI/CD pipelines, including Docker, Kubernetes, and shell executors.
Mastering GitLab Runners: Setup, Configuration, and Best Practices
Executive Summary
A GitLab Runner is the agent that actually executes your pipeline jobs. GitLab schedules and reports; runners do the work, on infrastructure you choose, with executors that range from a shell on the host to an ephemeral Kubernetes pod per job. Runner decisions determine pipeline speed, isolation, security, and cost more than any pipeline keyword does. This guide covers runner scope, installation and registration, the executor trade-offs that matter, the configuration file settings for concurrency, cache, and timeouts, and the operational practices for scaling and hardening. You will finish able to choose an executor deliberately, size a runner fleet from evidence, and avoid the privileged-Docker and shared-cache mistakes that create security incidents.
Why This Matters
Every job your team runs executes on a runner with some level of privilege on some machine. A misconfigured runner is therefore both a performance problem and a security boundary failure. Pipelines that queue for minutes are usually a runner capacity or concurrency problem, not a pipeline problem. A Docker executor running privileged, or a shell executor shared across projects, means any job, including one from a merge request from a fork, can potentially reach the host or another project's secrets. Runner fleets also decay: versions drift from the GitLab instance, caches fill disks, and nobody owns upgrades until something breaks during a release. Treating runners as production infrastructure, with an owner, a patching cadence, and monitoring, is the difference between CI that is trusted and CI that is worked around.
Core Concepts
Runner Scope: Instance, Group, and Project
Runners are registered at one of three scopes. Instance runners serve every project on the instance and suit central platform teams providing a standard fleet. Group runners serve all projects in a group and are the usual unit for a department or product area. Project runners serve one project and fit specialised hardware, licensed software, or strict isolation requirements. A runner also carries tags, and jobs select runners by tag, which is how you route GPU work, high-memory builds, or jobs that must run in a particular network. Protected runners only run jobs from protected branches, which matters for deployment jobs holding production credentials. Decide scope and tagging before registering anything; retrofitting a tag taxonomy onto a live fleet is painful.
Installation and Registration
On a Linux host, install the package or binary, create the service user, and register the runner against your GitLab instance with an authentication token obtained from the admin, group, or project runner settings.
# Download the binary sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 # Give it permissions sudo chmod +x /usr/local/bin/gitlab-runner # Create a GitLab CI user sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash # Install and start the service sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner sudo gitlab-runner start
sudo gitlab-runner register \ --non-interactive \ --url "https://gitlab.com/" \ --registration-token "YOUR_REGISTRATION_TOKEN" \ --description "docker-runner" \ --executor "docker" \ --docker-image alpine:latest
Registration writes the runner's configuration to config.toml. Everything after this point, concurrency, cache, executor options, limits, is edited there or supplied through the Helm values for Kubernetes installations. Keep config.toml under configuration management; a fleet whose configuration lives only on the hosts cannot be rebuilt or audited.
Executors: The Decision That Matters Most
The shell executor runs jobs directly on the host as the runner user. It is the simplest to set up and the hardest to isolate: jobs share the filesystem, installed tooling, and any credentials the user can reach. Reserve it for single-purpose machines, such as a build host for a specific platform, never for a shared multi-project fleet.
The Docker executor runs each job in a container from a specified image, with services as sibling containers. It is the default choice for most fleets: reproducible job environments, reasonable isolation, and straightforward scaling on one host.
[[runners]]
name = "docker-runner"
url = "https://gitlab.com"
executor = "docker"
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = false
disable_cache = false
volumes = ["/cache"]The Kubernetes executor creates a pod per job, which gives the cleanest isolation and natural autoscaling on cluster capacity, at the cost of pod startup time and more moving parts to configure, including namespace, service account, and image pull configuration.
apiVersion: v1
kind: ConfigMap
metadata:
name: gitlab-runner
namespace: gitlab
data:
config.toml: |
[[runners]]
[runners.kubernetes]
namespace = "gitlab"
image = "ubuntu:20.04"In current practice the runner is usually installed into the cluster with the Helm chart rather than configured by hand, but the executor settings are the same. Autoscaling executors built on Docker Machine historically filled the elastic-VM role; fleets being built today should evaluate the newer autoscaling options and the Kubernetes executor first, and treat Docker Machine as legacy.
Concurrency, Limits, and Timeouts
Three numbers govern throughput. The global concurrent setting caps total jobs across the host. A per-runner limit caps jobs for that runner. Request concurrency controls how aggressively the runner asks GitLab for new jobs.
concurrent = 4 [[runners]] name = "optimized-runner" limit = 2
Set these from the host's actual CPU and memory and the resource profile of your jobs, not from optimism; overcommitting produces jobs that time out under load, which looks like flakiness and is actually arithmetic. Timeouts belong in the same conversation: a per-job maximum runtime in the project settings and runner-level timeouts stop a hung job from occupying a slot indefinitely.
[[runners]] name = "timeout-runner" execution_timeout = 3600 timeout = 3600
Cache
Runner cache stores files between jobs, most often dependencies, and can live on the host or in S3-compatible object storage shared by the fleet.
[[runners]]
[runners.cache]
Type = "s3"
Path = "cache"
Shared = false
[runners.cache.s3]
ServerAddress = "s3.amazonaws.com"
AccessKey = "ACCESSKEY"
SecretKey = "SECRETKEY"
BucketName = "runners-cache"
BucketLocation = "eu-west-1"Shared object storage is what makes an autoscaled or multi-host fleet behave consistently; host-local cache silently stops helping the moment a job lands on a different machine. Cache is an optimisation, never a correctness dependency, and it must be treated as untrusted input across projects: scope cache keys so one project cannot poison another's dependencies.
Tags and Job Routing
Tags are how pipelines express placement requirements.
job:
tags:
- docker
- high-cpu
script:
- echo "Running on tagged runner"Keep the tag vocabulary small and documented: executor or environment tags, plus capability tags for genuinely specialised hardware. A job that specifies a tag no runner carries will sit pending forever, so monitor for stuck jobs as part of fleet operations.
Security Configuration
Isolation settings live in the executor configuration. For Docker, privileged mode is the critical switch: jobs that build images often request it, and it grants the job effective control of the host.
[[runners]]
[runners.docker]
privileged = false
disable_cache = true
volumes = ["/builds:/builds:rw"]Prefer rootless or daemonless image build tools where you can, so privileged mode stays off. Restrict which images and services untrusted jobs may use.
[[runners]]
[runners.docker]
allowed_images = ["alpine:*", "ruby:*"]
allowed_services = ["postgres:*", "redis:*"]Separate the fleet by trust: runners that accept jobs from forks or unprotected branches must never be the runners that hold deployment credentials or network access to production. Masked and protected variables, protected runners, and network segmentation enforce that separation in depth.
Monitoring and Maintenance
A runner fleet needs the same care as any service: expose and scrape the runner's metrics endpoint, alert on runners offline and on job queue time, and watch disk, since caches, images, and build directories fill volumes predictably.
#!/bin/bash # monitor-runners.sh STATUS=$(gitlab-runner status) if [[ $STATUS != *"is running"* ]]; then echo "Runner is down, restarting..." gitlab-runner restart fi
Upgrade runners on a cadence matched to the GitLab instance version, oldest fleets first, and rehearse the upgrade on one runner before rolling it across the fleet.
Decision Framework
| Situation | Recommendation |
|---|---|
| General multi-project fleet | Docker executor on dedicated hosts, or Kubernetes executor if you already run a cluster |
| Jobs need strong isolation between projects | Kubernetes executor with a pod per job, or separate Docker hosts per trust level |
| Image builds in jobs | Daemonless or rootless build tools first; privileged Docker only on an isolated, dedicated fleet |
| Elastic, spiky demand | Kubernetes executor on an autoscaling cluster, or a current autoscaling executor, with S3 cache |
| One specialised build, such as a licensed compiler or attached hardware | Project runner with the shell executor on that dedicated machine |
| Pipelines queue at peak times | Measure queue time, then raise concurrency within host capacity or add runners; do not guess |
| Jobs from forks or external contributors | A separate, unprivileged runner pool with no production network access or protected variables |
Implementation Checklist
Trade-offs
The shell executor is fast and simple because it skips isolation, which is exactly its risk. Docker adds startup and image-pull overhead per job and buys reproducibility. Kubernetes adds the most isolation and elasticity and the most operational surface: cluster upgrades, pod eviction, and image pull behaviour all become CI concerns. Central shared fleets are efficient and create a single contention and failure domain for every team's pipelines; per-team fleets isolate impact and multiply maintenance. Autoscaling cuts idle cost and adds cold-start latency and configuration complexity. Every fleet design is a choice among these, made explicit or made by accident.
When Not to Use
Do not self-host runners when GitLab's hosted runners meet your needs; running a fleet is real infrastructure work with patching, capacity, and security obligations. Do not use the shell executor on any host that also runs other workloads or holds credentials, and do not give a general fleet privileged Docker because one team's image build asks for it; give that team an isolated runner instead. Do not place runners inside a production network segment to make deployments convenient; use deployment-specific credentials and protected runners at the boundary. Finally, do not scale the fleet to fix slow pipelines whose real problem is job design; capacity hides, and then funds, inefficiency.
Conclusion
Runners are production infrastructure that happens to run CI jobs. Choose executors for isolation first and convenience second, size concurrency from measurements, separate trust levels physically, and operate the fleet with monitoring and upgrades. Pipelines built on that foundation are fast because the fleet is right, not because the YAML is clever.
References
Why This Matters
Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.
Trade-offs to Consider
Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.
When NOT to Use This
Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.
Decision Framework
Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.