如何从gitlab-ci将docker镜像发布到docker hub

问题描述 投票:15回答:2

Gitlab提供了一个.gitlab-ci.yml模板,用于构建和发布图像到自己的注册表中(单击某个项目中的“新文件”,选择.gitlab-ci.ymldocker)。该文件看起来像这样,它开箱即用:)

# This file is a template, and might need editing before it works on your project.
# Official docker image.
image: docker:latest

services:
  - docker:dind

before_script:
  - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY

build-master:
  stage: build
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"
  only:
    - master

build:
  stage: build
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
  except:
    - master

但默认情况下,这将发布到gitlab的注册表。我们怎样才能发布到docker hub呢?

gitlab-ci docker-registry
2个回答
37
投票

根本不需要改变那个.gitlab-ci.yml,我们只需要在项目的管道设置中添加/替换环境变量。

首先,我们需要知道注册表网址。使用hub.docker.com将无法正常工作,您将收到以下错误:

来自守护程序的错误响应:登录尝试https://hub.docker.com/v2/失败,状态为:404 Not Found

默认的docker hub注册表URL可以像这样找到:

docker info | grep Registry
Registry: https://index.docker.io/v1/

index.docker.io正是我想要的。我想发布gableroux/unity3d image,这是我在管道秘密中使用的内容:

CI_REGISTRY_USER=gableroux
CI_REGISTRY_PASSWORD=********
CI_REGISTRY=docker.io
CI_REGISTRY_IMAGE=index.docker.io/gableroux/unity3d

CI_REGISTRY_IMAGE很重要。 它默认为registry.gitlab.com/<username>/<project> regsitry网址需要更新,所以使用index.docker.io/<username>/<project> 由于docker hub是默认注册表,你也可以使用<username>/<project>,但我更喜欢它的详细信息。所以这个答案也应该涵盖其他注册表,只需相应地更新环境变量。


11
投票

为了扩展GabLeRoux的答案,

我在GitLab CI构建的推动阶段遇到了问题:

denied: requested access to the resource is denied

通过将我的CI_REGISTRY改为docker.io(删除index.),我能够成功推动。

© www.soinside.com 2019 - 2024. All rights reserved.