1

I try to configure gitlab runner in order to uses docker command into my .gitlab-ci.yml file.

I have run 2 containers to do that :

  • gitlab/gitlab-ce:latest
  • gitlab/gitlab-runner:alpine

I have set my registered runner with privilege mode :

[[runners]]
  name = "first runner"
  url = "http://my.domain.com:8484/"
  token = "mySecretToken"
  executor = "docker"
  clone_url = "http://my.domain.com:8484/"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "node:lts-alpine"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0

My pipeline work fine to build and run tests. Now i want to build nginx docker container with my node builded files.

When i just run docker version command, there was an error :

$ docker version
Client: Docker Engine - Community
 Version:           20.10.1
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        831ebea
 Built:             Tue Dec 15 04:28:35 2020
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
error during connect: Get http://docker:2375/v1.24/version: dial tcp: lookup docker on 8.8.8.8:53: no such host
ERROR: Job failed: exit code 1

Maybe it's an error into my .gitlab-ci.yml :

services:
  - name: docker:dind

stages:
    - construction

variables:
  DOCKER_HOST: tcp://docker:2375/
  DOCKER_DRIVER: overlay2
  # See https://github.com/docker-library/docker/pull/166
  DOCKER_TLS_CERTDIR: ""

deploiement:
  stage: construction
  image: docker:latest
  before_script:
    - docker version
  script:
    - echo "test"

How can i configure my runner to execute docker commands ? Is my runner setup wrong ?

Thanks for your answers !

2
  • 2
    And what's happen if you add volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]. Commented Jan 3, 2021 at 19:02
  • 1
    Maybe this thread can help? Commented Jan 3, 2021 at 19:18

2 Answers 2

1

According to @Dr Claw and @vpalmerini comments, it necessary to add docker.sock into volume and remove variables and service. See this thread.

My .gitlab-ci.yml :

variables:
  DOCKER_DRIVER: overlay2

before_script:
  - df
  - cat /etc/resolv.conf
  - cat /etc/hosts

deploiement:
  image: docker:latest
  before_script:
    - docker version
  script:
    - docker info

My config.toml:

[[runners]]
  name = "Synology premier runner"
  url = "http://my.domain.com:8484/"
  token = "1UsxGPQmxht6zHPb_y2b"
  executor = "docker"
  clone_url = "http://my.domain.com:8484/"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "node:lts-alpine"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
    shm_size = 0

Thanks you very much for your answers @Dr Claw and @vpalmerini !

Sign up to request clarification or add additional context in comments.

1 Comment

Saved me hours as well! No way to run docker in docker on GitLab runners without setup the volumes configuration and running it with privileged = true
0

I like to share my config for docker rootless. It is very similar to the one provided by Jérémy.

  1. Prepare a linux machine with a docker rootless installation.

  2. SSH to your host and become the "docker" user that is allowed to execute docker command.

  3. Find your user id by typing id. Let´s say it gives you a 1004.

  4. Start a gitlab-runner container using a socket mount from your user directory /run/user/1004

    docker run -d --name gitlab-runner --restart always -v /run/user/1004/docker.sock:/var/run/docker.sock -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest
    
  5. Register the runner

docker run --rm -t -i -v /srv/gitlab-runner/config:/etc/gitlab-runner --name gitlab-runner gitlab/gitlab-runner register

6. It will ask for some information.

  • If your repo or group is on gitlab, simply use "https://gitlab.com" as runner address. You don´t need to get more specific.

  • Get your token. Go to your group or project on gitlab. Find the “runners” entry in the “Build” menu.

    Add a new runner and copy the token to an editor or leave the page open for further reference.

  • User docker as executor

7. Now the runner should be connected. You can configure it to your gitlab project project/settings/ci

8. To make docker in docker work I use the following config on the host ~/.local/share/docker/volumes/gitlab-runner-config/_data/config.toml

concurrent = 1 # Bump this up to allow more than one job to run in parallel 
check_interval = 0
shutdown_timeout = 0
[session_server]
  session_timeout = 1800
[[runners]]
  name = "my-runner"
  url = "https://gitlab.com/"
  id = 47413623
  token = "XXX-your-token-here-XXX"
  token_obtained_at = 2025-05-12T06:29:10Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.cache]
    MaxUploadedArchiveSize = 0
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache",,"/run/user/1004/docker.sock:/var/run/docker.sock"]
    shm_size = 0
    network_mtu = 0

9. Example with three files: .gitlab-ci.yml, Dockerfile, index.md

.gitlab-ci.yml

...

Docker Build:
  image: docker:latest 
  services:
    - docker:dind 
  variables:
    DOCKERFILE: Dockerfile
    IMAGE_NAME: $CI_PROJECT_NAME
    IMAGE_TAG: $CI_COMMIT_SHORT_SHA
    IMAGE_URI: ${CI_REGISTRY_IMAGE}/${IMAGE_NAME}:${IMAGE_TAG}
  before_script:
    - unset DOCKER_HOST
    - docker login -u "gitlab-ci-token" -p "$CI_JOB_TOKEN" "$CI_REGISTRY"
  script:
    - docker build --quiet --pull -f $DOCKERFILE -t $IMAGE_NAME:$IMAGE_TAG .
    - docker image tag ${IMAGE_NAME}:${IMAGE_TAG} $IMAGE_URI
    - docker push $IMAGE_URI  
...

Dockerfile

FROM registry.gitlab.com/schnasse-org/docker-build/quarto:0.1.1 AS build
COPY index.md /mysite/
RUN quarto render /mysite --output-dir /usr/share/nginx/html

FROM nginx:latest
COPY --from=build /usr/share/nginx/html /usr/share/nginx/html
EXPOSE 80

index.md

# Hello World 
  1. Docker Compose

docker-compose.yml

services:
  gitlab-runner:
    image: gitlab/gitlab-runner:latest
    restart: always
    container_name: gitlab-runner
    volumes:
      - gitlab-runner-config:/etc/gitlab-runner
      - /run/user/1004/docker.sock:/var/run/docker.sock
volumes:
  gitlab-runner-config:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.