Search code examples
dockergitlabgitlab-cigitlab-ci-runnerdockerhub

How to authenticate on Dockerhub before pulling docker:dind with Gitlab CI runner


When I run this Gitlab CI job on my own runner “myrunner”

test:
  tags: 
    - myrunner
  image: docker:latest
  stage: build
  services:
    - docker:dind
  script:
    - echo "It works!"
  rules:
    - when: always

I get this error message:

Preparing the "docker" executor
Using Docker executor with image docker:latest ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
ERROR: Preparation failed: Error response from daemon: toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit (docker.go:142:1s)
Will be retried in 3s ...

How can I authenticate before pulling the image docker:dind to avoid the pull rate limit on Dockerhub?

Steps to create own Gitlab runner:

Start runner:

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:latest

Register:

docker run --rm -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register \
  --non-interactive \
  --executor "docker" \
  --docker-image docker:stable \
  --url "DOMAIN" \
  --registration-token "REGISTRATION_TOKEN" \
  --description "docker-runner" \
  --tag-list "myrunner" \
  --run-untagged="true" \
  --locked="false" \
  --access-level="not_protected" \
  --docker-volumes "/certs/client" \
  --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
  --docker-privileged

Solution

  • I added this to my gitlab-ci.yml:

    variables:
        DOCKER_AUTH_CONFIG: '{ "auths": { "https://index.docker.io/v1/": { "auth": "$DOCKER_AUTH" } }}'
    

    The value of $DOCKER_AUTH can be generated with

    echo -n "my_username:my_password" | base64
    

    with the username/password for Dockerhub.