Introduction
Self-hosting a GitLab Runner on a Linux VPS gives you control over build resources, image caching, and network access to private registries. The runner executes jobs defined in your .gitlab-ci.yml, and for Docker builds you need an executor that can run Docker commands inside a job. This guide walks through installing the runner, registering it with your GitLab instance, choosing between Docker-in-Docker and socket binding, and configuring a working pipeline. It also covers the errors you will hit first and how to fix them.
Prerequisites
You need a Linux VPS with root or sudo access, a working Docker installation, and a GitLab project where you can create a runner registration token. This guide assumes Ubuntu 22.04 or 24.04, but the commands work on Debian with minor changes. You also need outbound internet access from the VPS to reach GitLab and the container registries you use.
Verify Docker is healthy before you start:
docker versiondocker run --rm hello-worldIf docker run fails with a permission error, add your user to the docker group and start a new shell:
sudo usermod -aG docker $USERnewgrp dockerGitLab Runner will run as its own gitlab-runner user, so that user also needs Docker access. You will handle that during installation.
Installing GitLab Runner
The recommended method on Debian-based systems is the official package repository. It keeps the runner updated through apt and installs the systemd service automatically.
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bashsudo apt-get install gitlab-runnerConfirm the service is running:
sudo gitlab-runner statussudo systemctl status gitlab-runner --no-pagerThe package creates a gitlab-runner system user with a home directory at /home/gitlab-runner. That user owns the working directory where jobs clone your repository. Add it to the docker group so it can talk to the Docker daemon:
sudo usermod -aG docker gitlab-runnerRestart the service so the new group membership takes effect:
sudo systemctl restart gitlab-runnerIf your distribution does not have a package, or you want a specific version, install the binary directly:
sudo curl -L --output /usr/local/bin/gitlab-runner \ https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64sudo chmod +x /usr/local/bin/gitlab-runnersudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bashsudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runnersudo gitlab-runner startBoth methods produce the same result: a gitlab-runner daemon listening for jobs and a configuration file at /etc/gitlab-runner/config.toml.
Registering the Runner with Your GitLab Instance
Registration links the runner to a GitLab project or group. You need the GitLab URL and a registration token. In GitLab, go to your project, then Settings, then CI/CD, then Runners, and copy the registration token under “Set up a specific runner manually.”
Run the registration command interactively:
sudo gitlab-runner registerYou will be prompted for:
- GitLab instance URL, for example
https://gitlab.com/or your self-hosted URL. - Registration token.
- Description, such as
docker-build-vps. - Tags, such as
docker,linux,vps. - Executor. Choose
docker. - Default Docker image, such as
docker:24.0.5.
The tags matter because your .gitlab-ci.yml can target runners by tag. If you tag the runner docker, a job with tags: [docker] will run on it.
Non-interactive registration is useful for automation. The equivalent command looks like this:
sudo gitlab-runner register \ --non-interactive \ --url "https://gitlab.com/" \ --registration-token "$REGISTRATION_TOKEN" \ --executor "docker" \ --docker-image "docker:24.0.5" \ --description "docker-build-vps" \ --tag-list "docker,linux,vps" \ --run-untagged="false" \ --locked="false"After registration, the runner appears in the GitLab UI under CI/CD Runners with a green circle if it is online.
Understanding config.toml
The runner writes its settings to /etc/gitlab-runner/config.toml. Open it to see what registration produced:
sudo cat /etc/gitlab-runner/config.tomlA typical Docker executor entry looks like this:
[[runners]] name = "docker-build-vps" url = "https://gitlab.com/" token = "glrt-xxxxxxxxxxxxxxxx" executor = "docker" [runners.custom_build_dir] [runners.cache] [runners.cache.s3] [runners.cache.gcs] [runners.cache.azure] [runners.docker] tls_verify = false image = "docker:24.0.5" privileged = false disable_entrypoint_overwrite = false oom_kill_disable = false disable_cache = false volumes = ["/cache"] shm_size = 0 network_mode = "bridge"Two fields control most Docker build behavior: privileged and volumes. You will edit them depending on the build strategy you choose. After editing, restart the runner:
sudo gitlab-runner restartChoosing a Docker Build Strategy
There are two common ways to build Docker images inside a GitLab job. Each has trade-offs.
Docker-in-Docker, often called dind, runs a separate Docker daemon inside a service container. The job container talks to that daemon over TCP. It is isolated and works well on shared runners, but it is slower because layer caching does not persist between jobs unless you configure a registry cache.
Socket binding mounts the host Docker socket into the job container. The job talks directly to the host daemon, so builds are faster and layer caching is shared with the host. The downside is weaker isolation: a job can see and manipulate every container and image on the host.
Comparison:
| Aspect | Docker-in-Docker | Socket binding |
|---|---|---|
| Isolation | Strong, separate daemon | Weak, shared host daemon |
| Build speed | Slower, cold cache per job | Faster, shared cache |
| Security risk | Lower | Higher, job can control host Docker |
| Setup complexity | Moderate, needs privileged mode | Simple, one volume mount |
| Best for | Multi-tenant or untrusted code | Single-owner VPS, speed matters |
On a self-hosted VPS that only you control, socket binding is usually the better choice because of speed and simplicity. Docker-in-Docker fits when you run untrusted code or want strict separation between jobs.
Configuring Docker-in-Docker
To use dind, set privileged = true in the Docker runner section. The job container needs privileged mode to run its own Docker daemon.
Edit /etc/gitlab-runner/config.toml:
[runners.docker] tls_verify = false image = "docker:24.0.5" privileged = true volumes = ["/cache"]Restart the runner:
sudo gitlab-runner restartA pipeline using dind looks like this:
image: docker:24.0.5
services: - docker:24.0.5-dind
variables: DOCKER_HOST: tcp://docker:2375 DOCKER_TLS_CERTDIR: ""
stages: - build
build-image: stage: build script: - docker build -t registry.example.com/myapp:$CI_COMMIT_SHA . - docker push registry.example.com/myapp:$CI_COMMIT_SHADOCKER_TLS_CERTDIR is empty to disable TLS between the job and the dind service. For production, configure TLS instead of disabling it. The dind service exposes the daemon on port 2375 by default when TLS is off.
The main pain point with dind is caching. Each job starts a fresh daemon, so docker build cannot reuse layers from previous jobs unless you push intermediate images or use a registry cache. You can add --cache-from to reuse pushed images:
script: - docker pull registry.example.com/myapp:latest || true - docker build --cache-from registry.example.com/myapp:latest -t registry.example.com/myapp:$CI_COMMIT_SHA . - docker push registry.example.com/myapp:$CI_COMMIT_SHAConfiguring Socket Binding
Socket binding mounts /var/run/docker.sock into every job container. Edit the runner config:
[runners.docker] tls_verify = false image = "docker:24.0.5" privileged = false volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]Restart the runner:
sudo gitlab-runner restartNow a job can run docker commands directly against the host daemon:
image: docker:24.0.5
stages: - build
build-image: stage: build script: - docker build -t myapp:$CI_COMMIT_SHA . - docker imagesBecause the host daemon persists between jobs, layer caching works automatically. Builds are noticeably faster after the first run.
The security trade-off is real. A malicious or buggy job can stop host containers, delete images, or read secrets from other containers. Mitigate this by running only your own code on the runner, restricting who can modify .gitlab-ci.yml, and avoiding shared runners for untrusted merge requests. You can also set allowed_images in the runner config to restrict which images jobs may use.
Writing a Complete .gitlab-ci.yml for Docker Builds
A practical pipeline builds the image, runs a quick smoke test, and pushes to a registry. Here is a complete example using socket binding:
image: docker:24.0.5
variables: IMAGE_NAME: registry.example.com/myapp IMAGE_TAG: $CI_COMMIT_SHORT_SHA
stages: - build - test - push
build: stage: build script: - docker build -t $IMAGE_NAME:$IMAGE_TAG . tags: - docker
test: stage: test script: - docker run --rm $IMAGE_NAME:$IMAGE_TAG /app/healthcheck.sh tags: - docker
push: stage: push script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD registry.example.com - docker push $IMAGE_NAME:$IMAGE_TAG - docker tag $IMAGE_NAME:$IMAGE_TAG $IMAGE_NAME:latest - docker push $IMAGE_NAME:latest only: - main tags: - dockerUse $CI_REGISTRY_USER and $CI_REGISTRY_PASSWORD for the GitLab Container Registry, or define your own variables for an external registry. Store secrets as masked CI/CD variables in GitLab, never in the repository.
If you use Docker-in-Docker, add the service block and set DOCKER_HOST as shown earlier. The build and push commands stay the same.
Caching Images and Layers
Even with socket binding, pulling base images on every job wastes bandwidth. You can cache the Docker layer cache directory, but with socket binding the host daemon already caches layers. The bigger win is caching the build context and dependency downloads inside your Dockerfile.
For dind, enable the registry cache pattern. Build with --cache-from and push the result so the next job can reuse it. Combine with GitLab’s built-in cache for package managers:
build: stage: build cache: paths: - .npm/ script: - docker pull $IMAGE_NAME:latest || true - docker build --cache-from $IMAGE_NAME:latest -t $IMAGE_NAME:$CI_COMMIT_SHORT_SHA . - docker push $IMAGE_NAME:$CI_COMMIT_SHORT_SHAKeep the cache key stable. A changing cache key invalidates the cache and forces a cold build.
Securing the Runner
A self-hosted runner is a long-lived process with access to your VPS. Treat it like production infrastructure.
Restrict which projects can use it. In GitLab, you can lock a runner to specific projects, which prevents other projects from scheduling jobs on your VPS. Set locked = true during registration or in the UI.
Limit the images jobs can run. In config.toml, add an allowed_images list:
[runners.docker] allowed_images = ["docker:24.0.5", "alpine:3.19"] allowed_services = ["docker:24.0.5-dind"]This prevents a job from pulling an arbitrary image and running it on your host.
Keep the runner updated. GitLab Runner releases frequently, and older versions may fail against newer GitLab instances. With the package method, updates are simple:
sudo apt-get updatesudo apt-get install gitlab-runnerRun the runner as a non-root user, which the package does by default. Never run the daemon as root in production.
For socket binding, consider using a dedicated Docker daemon on a separate socket or a rootless Docker setup. Rootless Docker reduces the blast radius if a job escapes the container, at the cost of some compatibility with privileged operations.
Troubleshooting Common Errors
These are the errors you will see first. Each entry includes the message, cause, and fix.
Cannot connect to the Docker daemon at unix:///var/run/docker.sock
The gitlab-runner user cannot access the socket. Check group membership:
sudo -u gitlab-runner docker versionIf it fails, add the user to the docker group and restart the service:
sudo usermod -aG docker gitlab-runnersudo systemctl restart gitlab-runnerAlso confirm the socket exists and has group read-write permissions:
ls -l /var/run/docker.sockdocker: command not found
The job image does not include the Docker CLI. Use docker:24.0.5 or a similar image that ships the client. If you use a custom image, install the CLI in the Dockerfile or switch to a base image that includes it.
error during connect: Post http://docker:2375/v1.41/… dial tcp: lookup docker on … no such host
The dind service is not reachable. Check that the services block is present and the service name matches DOCKER_HOST. The hostname docker comes from the service alias. If you renamed the service, update DOCKER_HOST accordingly:
variables: DOCKER_HOST: tcp://my-dind:2375x509: certificate signed by unknown authority
The job cannot verify the registry or GitLab TLS certificate. For a self-hosted GitLab with a self-signed certificate, mount the CA into the runner. Set tls_verify and volume mounts for certificates in config.toml, or add the CA to the job image. A simpler fix for internal networks is to configure the Docker daemon to trust the registry:
{ "insecure-registries": ["registry.example.com:5000"]}Place this in /etc/docker/daemon.json and restart Docker. Only do this on trusted networks.
job failed: exit code 137
The job was killed by the OOM killer. Increase the VPS memory or reduce parallelism. Check container memory limits in the runner config:
[runners.docker] memory = "4g" memory_swap = "4g"Runner shows offline in GitLab UI
The daemon is not running or cannot reach GitLab. Check the service status and logs:
sudo systemctl status gitlab-runnersudo journalctl -u gitlab-runner -n 50 --no-pagerVerify network access from the VPS to the GitLab URL:
curl -I https://gitlab.com/Maintenance and Upgrades
Runners accumulate disk usage from images and build caches. Schedule periodic cleanup:
docker system prune -af --volumesBe careful with --volumes because it removes unused volumes, which may include data you want to keep. Run it during a maintenance window.
Monitor runner health with GitLab’s runner metrics endpoint if you enable it, or simply watch job duration trends. A sudden increase in build time usually means cache invalidation or disk pressure.
Rotate registration tokens periodically. You can revoke a token in the GitLab UI and re-register the runner with a new one. Keep the runner config in version control or back it up so recovery is fast.
Conclusion
A self-hosted GitLab Runner on a Linux VPS is a straightforward setup once you pick a build strategy. Socket binding gives you speed and simplicity on a single-owner machine, while Docker-in-Docker gives you isolation at the cost of cache complexity. Start with socket binding if you control the VPS, validate your first pipeline end to end, and then layer on security restrictions like allowed images and project locking. Keep the runner updated, clean up Docker periodically, and store secrets in CI/CD variables rather than in your repository.