无法访问来自 Gitlab CI 的私有图像 MySQL Docker

Can't Access Private MySQL Docker Image From Gitlab CI

我一直在尝试将私有(自定义)MySQL 图像从我的 Docker Hub 存储库拉入 gitlab-ci.yml 管道作为服务。我添加了一个 before_script 尝试使用我的用户名和密码(CI 变量)登录到 dockerhub。失败的构建日志中没有输出表明登录 Docker Hub 是否成功,但我假设不是,因为我的图像拉取失败并显示以下消息(编辑:或者它甚至从未尝试过,因为 gitlab试图在运行之前的脚本之前获取服务?):

存储库不存在或可能需要 'docker login' (executor_docker.go:168:0s)

我正在使用共享跑步者(因为我相信这是我使用 gitlab.com 的唯一选择?) 我已经看到很多关于 docker 的 gitlab ci 令牌的提及,但我没有找到任何文档来解释如何 facilitate this.

我确定我只是忽略了 something/not 理解或在我的搜索中遇到了适当的解决方案,如果我只是缺乏经验,我深表歉意,在此先感谢您的帮助。

我的 gitlab-ci(maven 变量是因为这个项目的构建依赖于私有 maven 仓库。数据库和 redis 主机变量在运行时注入到我的应用程序中,因此它们知道要使用哪个容器指向)

image: maven:3.5.0-jdk-8

before_script:
  - "docker login -u$DOCKER_USER -p$DOCKER_PASS" #pipeline variables

variables:
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  DATABASE_HOST: mysql
  REDIS_HOST: redis

services:
  - name: privaterepo/private-mysql-schema
    alias: mysql
  - name: redis:latest
    alias: redis

stages:
  - build

maven-build:
  stage: build
  script: "mvn $MAVEN_CLI_OPTS package -B"
  artifacts:
    paths:
      - target/*.jar

第一件事是设置 GitLab CI 以在需要时提供私有 docker 注册表的凭据。要做到这一点,您应该遵循 specific section in docs,完整的答案是

  1. 获取docker注册表url,用户名和密码使用docker login 或其他方式(我不得不花一些时间来弄清楚注册表 docker 枢纽)
  2. 在 GitLab 中定义 DOCKER_AUTH_CONFIG 变量 CI 变量部分看起来像
{
    "auths": {
        "registry.hub.docker.com": {
            "auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" // base 64 encoded username:password
        }
    }
}
  1. .gitlab-ci.yml
  2. 中声明image/serviceimage: registry.hub.docker.com/ruwanka/helloworld:0.1

这应该完全满足拉取镜像的要求。 another section in docs 列出了运行程序允许服务列表的要求。如果它没有指定任何内容,那么它应该没问题,如果它不起作用,您可能需要调整它。

最终的 yaml 如下所示

image: registry.hub.docker.com/ruwanka/helloworld:0.1

build:
  script:
   - echo "hello"
# more steps   
services:
  - registry.hub.docker.com/ruwanka/helloworld:0.1

GitLab 作业日志片段