← logbuild #13 passed2f13b31
GitLab Runner in a Container on a Dedicated VM: The Right Way to Run CI/CD
Step-by-step guide to running GitLab Runner as a Docker container on its own Debian 13 VM. It also compares this with a bare-metal install and shows why the container is the better choice.
· 12 min read
You have a VM (virtual machine) only for your GitLab Runner. The runner is the program that runs your CI/CD jobs. Do you install it directly on the VM, or run it in a container?
Both work. A container avoids dependency conflicts and upgrade problems. It also stops the environment from slowly changing over time.
This guide sets up GitLab Runner as a Docker container on Debian 13 (Trixie). It also compares this with a bare-metal install. “Bare-metal” here means the runner is installed directly on the VM, with no container.
In Simple Words#
- The problem
- When the runner is installed directly on the VM, every CI job runs on the same host. After some weeks the host is full of tools and versions that conflict, and pipelines break.
- The root cause
- All jobs share one filesystem. Each job leaves its packages and files behind, and nothing cleans them up.
- The solution
- Install only Docker on the VM. Run the runner as a container. The runner starts a new container for each job. Docker removes it when the job ends.
- Think of it like this
- Think of a shared kitchen where nobody washes the dishes. It gets worse every day. Now give each cook a clean table and clean tools, and clear the table when the cook leaves. The kitchen stays clean.
Bare-Metal vs. Containerized Runner: Why It Matters#
There are two ways to do it. One is clearly better.
Approach 1: Install GitLab Runner Directly on the VM#
You install the gitlab-runner package and register it. The jobs then run on the host.
# The bare-metal approach
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install gitlab-runner
sudo gitlab-runner registerWhat happens over time:
- CI needs Node.js 18, then 20, then 22 — all on one host
- One project needs Python 3.11, another needs 3.12
- A global npm package conflicts with another project
- A system library upgrade breaks three pipelines
- You must plan runner upgrades around the jobs that are running
- The host becomes a unique machine that nobody wants to touch
Approach 2: GitLab Runner as a Container (with Docker Executor)#
The runner lives in a container. Each CI job gets its own container, separate from all others. Only Docker is installed on the host.
# The containerized approach
docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:v18.9.0What happens over time:
- Each job gets a fresh, isolated environment
- Node.js 18 and 22 run side-by-side, no conflicts
- A bad job can’t damage the host or other jobs
- A runner upgrade is one
docker pulland a container restart - The host stays clean and easy to build again
The diagram shows both approaches, one after the other:
- Bare-metal: the runner and all jobs run directly on the VM. Every project leaves its tools on the host, and the versions conflict.
- Container setup: only Docker is installed on the VM. The runner is one container.
- A job arrives. The runner asks Docker to start a new container for this job.
- A second job gets its own container. Node.js 18 and 22 run side by side with no conflict.
- When a job ends, Docker removes its container. Nothing stays on the host.
Side-by-Side Comparison#
| Aspect | Bare-Metal Install | Containerized Runner |
|---|---|---|
| Installation | Package repo + apt install | Docker run (one command) |
| Job isolation | Shared host filesystem | Each job in its own container |
| Dependency conflicts | Build up over time | Impossible - fresh container per job |
| Runner upgrades | apt upgrade, and hope nothing breaks | docker pull + restart container |
| Host contamination | Jobs can modify the host OS | Host stays clean |
| Rollback | Difficult, manual | Change the image tag |
| Multi-version support | Manual version managers (nvm, pyenv) | Just use different Docker images |
| Reproducibility | Changes slowly over time (“works on runner”) | The same environment every run |
| Resource cleanup | Manual, jobs leave artifacts behind | Containers auto-removed after jobs |
| Security | Jobs run with runner user privileges | Jobs isolated in containers |
The VM gives you control over resources. The container gives you isolation, so jobs cannot touch each other or the host. Together you get both.
Prerequisites#
You need:
- A Debian 13 (Trixie) VM with root/sudo access
- A network connection to your GitLab instance
- A GitLab project or group where you register the runner
- A runner registration token (GitLab > Settings > CI/CD > Runners)
Our setup:
| Component | Details |
|---|---|
| VM OS | Debian 13 (Trixie) |
| VM Specs | 4 vCPUs, 8GB RAM, 80GB SSD |
| VM IP | 10.10.1.22 |
| GitLab Instance | Self-hosted at 10.10.1.21 |
Step 1: Prepare the Debian 13 Host#
Update the system.
sudo apt update && sudo apt upgrade -yInstall the basic tools:
sudo apt install -y ca-certificates curl gnupg lsb-releaseStep 2: Install Docker Engine#
Add Docker’s GPG key and repository:
# Add Docker's official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullInstall Docker:
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginCheck that Docker works:
sudo systemctl status docker
sudo docker run --rm hello-worldOptional: add your user to the docker group:
sudo usermod -aG docker $USERStep 3: Create the Runner Configuration Directory#
The config lives outside the container. This way it survives restarts and upgrades.
sudo mkdir -p /srv/gitlab-runner/configStep 4: Start GitLab Runner as a Container#
sudo docker run -d \
--name gitlab-runner \
--restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:v18.9.0What each flag does:
-d- Run in the background (detached)--name gitlab-runner- Name the container--restart always- Start again automatically after a crash or reboot-v /srv/gitlab-runner/config:/etc/gitlab-runner- Keep the config on the host-v /var/run/docker.sock:/var/run/docker.sock- Let the runner start other containers for CI jobs, next to its own
Check that it is running:
sudo docker ps --filter name=gitlab-runnerExpected output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc28bc296cb7 gitlab/gitlab-runner:v18.9.0 "/usr/bin/dumb-init …" 41 seconds ago Up 37 seconds gitlab-runnerStep 5: Register the Runner#
sudo docker exec -it gitlab-runner gitlab-runner registerThe command asks you questions one by one:
Runtime platform arch=amd64 os=linux pid=18 revision=07e534ba version=18.9.0
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://10.10.1.21
Enter the registration token:
glrt-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Verifying runner... is valid runner=e1P1g1DVP
Enter a name for the runner. This is stored only in the local config.toml file:
[bc28bc296cb7]: docker-runner-01
Enter an executor: custom, shell, parallels, virtualbox, docker, instance, ssh, docker-windows, docker+machine, kubernetes, docker-autoscaler:
docker
Enter the default Docker image (for example, ruby:3.3):
node:22-alpine
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"| Prompt | What to enter |
|---|---|
| GitLab instance URL | Your GitLab server URL (use internal IP if self-hosted) |
| Registration token | From GitLab > Settings > CI/CD > Runners |
| Runner name | A descriptive name, e.g. docker-runner-01 |
| Executor | docker |
| Default Docker image | Fallback image for jobs, e.g. node:22-alpine |
Or do it in one command, with no questions:
sudo docker exec gitlab-runner gitlab-runner register \
--non-interactive \
--url "http://10.10.1.21/" \
--token "glrt-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
--description "docker-runner-01" \
--executor "docker" \
--docker-image "node:22-alpine"Step 6: Tune the Runner Configuration#
The config is at /srv/gitlab-runner/config/config.toml on the host.
sudo nano /srv/gitlab-runner/config/config.tomlA config with good settings:
concurrent = 4
check_interval = 3
[[runners]]
name = "docker-runner-01"
url = "http://192.168.1.10/"
clone_url = "http://192.168.1.10/"
token = "your-runner-token"
executor = "docker"
[runners.docker]
image = "node:22-alpine"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
shm_size = 0
pull_policy = ["if-not-present"]
volumes = ["/cache"]
extra_hosts = ["gitlab.example.com:192.168.1.10"]
[runners.cache]
Type = "s3"
Shared = trueKey settings:
concurrent = 4- The most jobs that can run at the same time. Pick a number that fits your VMcheck_interval = 3- Ask GitLab for new jobs every 3 secondsclone_url- Internal IP for git clones. This makes them much fasterpull_policy = ["if-not-present"]- Do not pull an image that is already on the VMextra_hosts- Point the GitLab domain to the internal IP inside job containersvolumes = ["/cache"]- Turn on caching for jobs
Restart the runner to apply the changes:
sudo docker restart gitlab-runnerStep 7: Verify the Runner in GitLab#
- Go to Settings > CI/CD > Runners
- Your runner should show a green circle under “Available specific runners”
Test it with a simple .gitlab-ci.yml:
test-runner:
tags:
- docker
script:
- echo "Runner is working!"
- node --version
- cat /etc/os-releaseManaging the Runner#
Viewing Runner Logs#
# Live logs
sudo docker logs -f gitlab-runner
# Last 50 lines
sudo docker logs --tail 50 gitlab-runnerUpgrading the Runner#
This is where containers are much better.
Bare-metal upgrade (risky):
sudo apt update
sudo apt install gitlab-runner
# Hope nothing breaks
# Hope running jobs finish gracefully
# Hope config is still compatibleContainer upgrade (safe):
# Stop the old container
sudo docker stop gitlab-runner
sudo docker rm gitlab-runner
# Pull the new version
sudo docker pull gitlab/gitlab-runner:v18.10.0
# Start with the same config
sudo docker run -d \
--name gitlab-runner \
--restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:v18.10.0The config stays in /srv/gitlab-runner/config/. If the upgrade goes wrong, switch back to the old tag.
Stopping and Starting#
# Gracefully stop (finishes running jobs)
sudo docker stop gitlab-runner
# Start again
sudo docker start gitlab-runner
# Restart
sudo docker restart gitlab-runnerCleaning Up Docker Resources#
Job containers and images build up over time. Schedule a cleanup:
# Remove stopped containers, unused images, and build cache
sudo docker system prune -af --filter "until=72h"Run it every day with cron:
echo "0 3 * * * root docker system prune -af --filter 'until=72h'" | sudo tee /etc/cron.d/docker-cleanupTroubleshooting#
Runner shows as offline in GitLab#
# Check if the container is running
sudo docker ps --filter name=gitlab-runner
# Check runner logs for connection errors
sudo docker logs --tail 20 gitlab-runner
# Verify network connectivity to GitLab
sudo docker exec gitlab-runner ping -c 3 192.168.1.10Jobs stuck in “Pending”#
Common causes:
- Tag mismatch - Job tags don’t match runner tags
- Runner paused - Check runner status in GitLab UI
- Concurrent limit reached - All job slots are full
# Check how many jobs are currently running
sudo docker exec gitlab-runner gitlab-runner listPermission denied on Docker socket#
If jobs fail with Cannot connect to the Docker daemon:
# Check socket permissions
ls -la /var/run/docker.sock
# The runner container needs access to the socket
# This is handled by the -v /var/run/docker.sock mountSlow git clone operations#
Set clone_url to the internal IP:
[[runners]]
url = "http://192.168.1.10/"
clone_url = "http://192.168.1.10/"See GitLab Runner Performance Optimization for more tips.
Summary#
VM + containerized runner gives you:
- Clean host - Only Docker on the VM, no dependency problems
- Isolated jobs - A new container for each CI job
- Easy upgrades - Pull the new image and restart
- Easy rollback - Go back to the previous image tag
- Reproducibility - The same environment every run
- Resource control - Dedicated CPU, RAM, and disk from the VM
Bare-metal works for quick tests. For real work, the 10 minutes of Docker setup save you more time than that within a week.