简体   繁体   English

GitLab CI 从私有注册表中提取 docker

[英]GitLab CI pull docker from private registry

The situation情况

I'm currently working on a project where I migrate the CI environment from Jenkins to GitLab CI.我目前正在开展一个项目,我将 CI 环境从 Jenkins 迁移到 GitLab CI。 To get everything working I've built a few docker containers that should serve as base images for the CI pipeline.为了让一切正常工作,我构建了一些 docker 容器,它们应该用作 CI 管道的基础镜像。 These containers cannot be stored in the GitLab internal container registry and should be pushed to a Artifactory instance.这些容器不能存储在 GitLab 内部容器注册表中,应该推送到 Artifactory 实例。

What I already did/tried我已经做过/尝试过的事情

So far I got everything going by using Kaniko for the Docker builds and the resulting images are correctly pushed to the artifactory registry - so far, so good.到目前为止,我通过将 Kaniko 用于 Docker 构建来完成所有工作,并将生成的图像正确推送到工件注册表 - 到目前为止,一切都很好。

However I've now reached a point where I have CI-Jobs that should make use of previously built container-images as their base image, so they should be pulled from the artifactory instance, which serves as private registry.然而,我现在已经到了这样的地步,我有 CI-Jobs 应该使用以前构建的容器映像作为它们的基本映像,因此应该从作为私有注册表的工件实例中提取它们。

example config示例配置

.docker-build-abstract:
  image: custom.kaniko.fork.from.internal.gitlab-registry:<tag>
  script:
    - |>
      echo "build container with image tag: ${IMAGE_TAG}"
      # kaniko default build..

# This job builds an image that is pushed to private registry.
docker-build-1:
  stage: "build"
  variables:
    IMAGE_TAG: some.nice.tag
    BUILD_ARGS: --build-arg foo=bar --build-arg bar=baz
  extends:
    - .docker-build-abstract

# This job should make use of the previously built image
other-job-1:
  stage: "build"
  image: docker.from.docker-build-1:<tag>
  script: #...

The Problem with this is that I cannot make use of DOCKER_AUTH_CONFIG variable in the CI-Settings that is proposed in the official docs .问题是我无法在官方文档中提出的 CI-Settings 中使用DOCKER_AUTH_CONFIG变量。 This is because (my understanding) when providing this variable in the project settings this would overwrite the default registry-settings ( CI_REGISTRY , CI_REGISTRY_USER , CI_REGISTRY_PASSWORD ) but I need to preserve the values behind these internal variables because some of the first jobs make use of Container-images that are only present within this private gitlab instance.这是因为(我的理解)在项目设置中提供此变量时,这将覆盖默认的注册表设置( CI_REGISTRYCI_REGISTRY_USERCI_REGISTRY_PASSWORD ),但我需要保留这些内部变量背后的值,因为一些第一个作业使用仅在此私有 gitlab 实例中存在的容器映像。

Is it possible to provide multiple auth-configs in the CI/CD Variable settings?是否可以在 CI/CD 变量设置中提供多个身份验证配置? And how would I reference the predefined variables in here since this important to keep the internal registry known?我将如何在这里引用预定义的变量,因为这对于保持内部注册表已知很重要?

I would imagine something like below (which is part of the kaniko pre-configuration), but cannot come up with a possible solution for this scenario.我想像下面这样(这是 kaniko 预配置的一部分),但无法为这种情况提出可能的解决方案。

{
  "auths": {
    "$PRIVATE_REGISTRY": {
      "username": "$PRIV_REGISTRY_USER",
      "password": "$PRIV_REGISTRY_API_KEY",
      "email": "$PRIV_REGISTRY_USER_EMAIL"
    },
    "$CI_REGISTRY": {
      "username": "$CI_REGISTRY_USER",
      "password": "$CI_REGISTRY_PASSWORD"
    }
  }
}

In my understanding the variables can be overwritten in the ci-configuration, but this did not work for me.据我了解,变量可以在 ci 配置中被覆盖,但这对我不起作用。 Would this require advanced configuration of the ci-runner to achieve the desired behavior, like outlined here in the docs?这是否需要对 ci-runner 进行高级配置才能实现所需行为,如文档中所述?

Really appreciate your help or hints!非常感谢您的帮助或提示!

You can in fact configure multiple credentials in the "auths" section above.实际上,您可以在上面的“身份验证”部分中配置多个凭据。 That does work fine for us.这对我们来说确实很好。 But I don't think it will work via setting DOCKER_AUTH_CONFIG in the CI pipeline.但我认为它不会通过在 CI 管道中设置 DOCKER_AUTH_CONFIG 来工作。 I think we tried that once and it did not work.我想我们试过一次,但没有奏效。 We configure it in the Runner configuration by setting the environment variable.我们通过设置环境变量在 Runner 配置中进行配置。

If you can't do that, another solution would be to run the docker login command in your CI pipeline, eg in the before_script section.如果您不能这样做,另一种解决方案是在 CI 管道中运行 docker 登录命令,例如在 before_script 部分中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM