How to Set Up GitLab CI with Docker Runners

GitLab CI runs your pipeline inside Docker containers, and Docker runners are the workers that do it. Setting up gitlab ci docker runners means registering a runner on your own server, pointing it at your GitLab project, and writing a .gitlab-ci.yml file that builds and tests your code in a fresh container on every push. The whole setup takes about an hour, and it gives you a pipeline you control, on hardware you control, with no per-minute bill from a hosted runner.

A runner is a small agent that polls GitLab for jobs, runs them, and sends the results back. The gitlab runner docker executor runs each job in a fresh container, so nothing is left behind between jobs. The Docker executor runs each job in a container based on an image you choose, so a Node job runs in a Node image and a Python job runs in a Python image, with nothing left behind between jobs. That isolation is the point: every job starts clean, and a bad job cannot poison the next one.

GitLab CI Docker runners: install and register

Install the runner on a server with Docker already running. The official packages cover Debian, Ubuntu, and other distributions, and the install takes a few commands. Then register the runner against your GitLab instance with the token from the project settings under CI/CD, Runners:

sudo gitlab-runner register 
  --url https://gitlab.com 
  --token glrt-xxxxxxxxxxxx 
  --executor docker 
  --docker-image alpine:latest 
  --description "docker-runner-01"

Choose the executor docker and a default image, then answer the tag question. Tags let you route jobs to specific runners, so a job tagged docker only runs on runners with that tag. Keep the runner unprotected at first, run a test job, and only then lock the token down. GitLab shows the runner as active in the project settings once the first job runs, and a runner that never picks up jobs usually has a tag mismatch or the project setting that disables shared runners in the wrong state.

Write your first .gitlab-ci.yml

The pipeline file lives at the root of the repository. The example below runs on every push, installs dependencies, runs tests, and builds the app in a Node image:

stages:
  - test
  - build

test-job:
  stage: test
  image: node:20-alpine
  script:
    - npm ci
    - npm test

build-job:
  stage: build
  image: node:20-alpine
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/

Stages run in order, and jobs in the same stage run in parallel. Artifacts carry files from one job to the next, so the build output can be downloaded or passed to a deploy job. Keep the file short at first. A pipeline with two stages that always passes is worth more than a pipeline with ten stages that the team learns to ignore. If you are comparing GitLab CI with the other hosted options, our guide to the best CI/CD tools in 2026, Jenkins vs GitHub Actions vs GitLab puts the trade-offs side by side.

Build Docker images inside the pipeline

Building an image inside a Docker job needs Docker-in-Docker, because the job container has no Docker daemon of its own. GitLab CI Docker in Docker is the standard pattern for this, and the TLS setting below is the safe default. The standard pattern adds the docker service and uses the docker image for the job:

docker-build:
  stage: build
  image: docker:27
  services:
    - docker:27-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  script:
    - docker build -t my-app:$CI_COMMIT_SHORT_SHA .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

A gitlab-ci.yml docker build job needs the docker service and TLS enabled, and you log in to the registry first with the CI_JOB_TOKEN, which GitLab creates for each job, so no long-lived credential has to be stored. The DOCKER_TLS_CERTDIR setting turns on TLS between the job and the Docker-in-Docker service, which is the safe default. Cache the image layers between jobs with a registry cache or a local cache directory, because a full rebuild on every push wastes minutes and hides real failures behind slow logs. For the Dockerfile itself, the techniques in our guide to multi-stage Dockerfiles that shrink images keep the image small and the build fast.

Cache, tags, and runner sizing

Dependency caching is the single biggest speedup for most pipelines. GitLab CI can cache node_modules between jobs with a cache key based on the lock file, so only a changed dependency triggers a fresh install. Set the cache policy to pull-push on the install job and pull on later jobs, and watch the cache size: a cache that grows without a key change will slow the pipeline instead of speeding it.

Size the runner host for the jobs it will run. One concurrent job per CPU core is a safe start, and memory matters more than CPU for Node and Java builds. Set concurrent in the runner config to match the host, and watch memory during a busy hour before you raise it. A runner that swaps will make every job slow and the failures look random. The same sizing logic applies to the pipeline tool you compare it with, and our guide to building a CI/CD pipeline with GitHub Actions shows how the hosted runner model differs from running your own.

Secure the runner and keep it healthy

A runner that can run any job from the project can also run a malicious job if a merge request comes from a fork, so restrict which jobs untrusted runners can take. Turn off the option to run untagged jobs when you use tags, and keep the runner host patched and isolated from production data. The runner needs Docker socket access or Docker-in-Docker, both of which give jobs real power on the host, so never put a shared runner on the same machine as a production database.

Keep the runner healthy with a short routine. Check the runner status in GitLab once a week, prune old images on the host with docker system prune, and watch disk space, because cached layers and old images fill a small disk in weeks. If your team also trains models, the setup in our guide to setting up CI/CD for machine learning models with GitHub Actions shows the same runner ideas applied to heavier jobs. Start with one runner, one project, and a green pipeline, then add tags, caching, and more runners only when the queue tells you the single runner is the bottleneck.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.