GitLab Runner拉取镜像前需要进行Docker登录

6

我已经设置了一个Docker runner,想要在本地仓库中运行存储的镜像。我的/etc/gitlab-runner/config.toml文件如下:

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800


[[runners]]
  name = "Docker runner"
  url = "https://gitlab.str.corp/"
  token = "*secret*"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "gitlab.example.com:4443/docker:19.03.1-dind"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/certs/client", "/cache"]
    shm_size = 0

当我提交作业时,它会在runner上启动,但是无法拉取镜像。
Running with gitlab-runner 13.4.0 (4e1f20da)
  on Docker runner abcde123

Preparing the "docker" executor
Using Docker executor with image gitlab.example.com:4443/docker:19.03.8-git ...
Starting service gitlab.example.com/docker:19.03.1-dind ...
Authenticating with credentials from /root/.docker/config.json
Pulling docker image gitlab.example.com:4443/docker:19.03.1-dind ...
ERROR: Job failed: Error response from daemon: pull access denied for gitlab.example.com:4443/docker, repository does not exist or may require 'docker login': denied: requested access to the resource is denied (docker.go:142:0s)
docker login 是问题所在。如果我尝试从命令行运行 docker pull <image>,我会得到相同的错误。然后进行 docker login 就可以拉取镜像了。
我很确定只需要在拉取镜像之前运行 echo $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY,但我不知道如何使其运行。
Docker 镜像存储在 GitLab 集成仓库中。 此页面 显示认证应该自动设置,但显然不是这种情况。
我需要做什么才能拉取私有存储的镜像?
1个回答

9
由于Runner在运行任何before_scriptscriptafter_script 部分之前拉取镜像,所以您将无法执行docker login... 从而使其工作。 您需要提供Docker Auth配置文件,以便Runner可以登录。
有关自定义Docker注册表的文档包含所有信息,简而言之,您可以从~/.docker/config.json获取配置。 如果不使用凭据存储(例如MacOS的密钥链),则可以直接复制内容并将其存储为变量,存储在您的.gitlab-ci.yml文件中或以DOCKER_AUTH_CONFIG命名的项目CI变量中。 如果存在该变量,则Runner将自动使用它登录到注册表,然后拉取映像。
在上述文档中,还有其他提供变量的选项以及如何跟随指示如果您的Docker安装使用凭证存储。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接