gitlab高级功能之容器镜像仓库

今天给大家介绍一个gitlab的高级功能 - Container Registry,该功能可以实现docker镜像的仓库功能,将gitlab上的代码仓的代码通过docker构建后并推入到容器仓库中,好处就是无需再额外部署一套docker仓库。

文章目录

    • [1. 参考文档](#1. 参考文档)
    • [2. Container Registry配置](#2. Container Registry配置)
    • [3. 实战应用](#3. 实战应用)
      • [3.1 流水线镜像打包推送](#3.1 流水线镜像打包推送)
      • [3.2 本地拉取项目镜像](#3.2 本地拉取项目镜像)
    • [4. Container Registry镜像命名约定](#4. Container Registry镜像命名约定)

1. 参考文档

2. Container Registry配置

  • 修改/etc/gitlab/gitlab.rb

    registry_external_url 'https://gitlab.example.com:5050'
    registry_nginx['ssl_certificate'] = "/path/to/certificate.pem"
    registry_nginx['ssl_certificate_key'] = "/path/to/certificate.key"

  • 重载配置

    gitlab-ctl reconfigure

3. 实战应用

3.1 流水线镜像打包推送

cat .gitlab-ci.yml

复制代码
docker-build:
  image: docker:latest
  stage: build
  services:
    - docker:dind
  variables:
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""
  before_script:
    - echo  $CI_REGISTRY_USER $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE:${CI_COMMIT_SHORT_SHA}" .
    - docker push "$CI_REGISTRY_IMAGE:${CI_COMMIT_SHORT_SHA}"
  rules:
    - if: $CI_COMMIT_BRANCH
      exists:
        - Dockerfile

说明:

  • CI_REGISTRY_USER、CI_REGISTRY_PASSWORD、CI_REGISTRY、CI_REGISTRY_IMAGE、CI_COMMIT_SHORT_SHA均是内置变量
  • CI_REGISTRY_PASSWORD只在当前项目下有效。

3.2 本地拉取项目镜像

复制代码
docker login registry_external_url:5050

通过命令行登录gitlab的镜像仓库并推送镜像时,有一下几种认证方式:

  • gitlab用户名密码的方式

  • 令牌的方式(个人令牌、项目令牌、群组令牌),基于令牌的方式,用户名可以是任意的字符串作为占位符即可。

    docker tag busybox:latest kube.bdeet.top:5050/ci-file/container-package:latest
    docker tag busybox:latest kube.bdeet.top:5050/ci-file/container-package/busybox:latest
    docker tag busybox:latest kube.bdeet.top:5050/ci-file/container-package/my/busybox:latest

    docker push kube.bdeet.top:5050/ci-file/container-package:latest
    docker push kube.bdeet.top:5050/ci-file/container-package/busybox:latest
    docker push kube.bdeet.top:5050/ci-file/container-package/my/busybox:latest


4. Container Registry镜像命名约定

镜像遵循以下命名约定:

plaintext 复制代码
<registry URL>/<namespace>/<project>/<image>

例如,如果您的项目是 kube.bdeet.top:5050/ci-file/container-package,那么您的镜像必须至少命名为 kube.bdeet.top:5050/ci-file/container-package/my-app

您可以将其他名称附加到镜像名称的末尾,深度最多为三层。

例如,这些都是名为 container_registry 的项目中镜像的所有有效镜像名称:

bash 复制代码
kube.bdeet.top:5050/ci-file/container-package:some-tag
bash 复制代码
kube.bdeet.top:5050/ci-file/container-package/image:latest
bash 复制代码
kube.bdeet.top:5050/ci-file/container-package/my/image:rc1
相关推荐
裁二尺秋风20 小时前
CI/CD — Pipeline的使用以及Blue Ocean多分支流水线的使用方法
ci/cd·gitlab·jenkins
北海之灵1 天前
docker desktop部署本地gitlab服务
docker·容器·gitlab
兔老大RabbitMQ1 天前
GitHub vs GitLab 全面对比报告(2025版)
gitlab·github
CC码码2 天前
管理你的多个 Git 密钥(多平台多账号)
git·gitlab·github
CC码码2 天前
管理你的多个 Git 密钥(单平台多账号)
git·gitlab·github
兔老大RabbitMQ2 天前
GitLab详细分析
gitlab
小道仙9715 天前
gitlab对接,gitlabRestApi,gitlab4j-api
java·git·gitlab
tswddd16 天前
项目:Gitlab HSD CI/CD总结
ci/cd·kubernetes·gitlab
许_安16 天前
docker中部署gitlab
docker·eureka·gitlab
Haoea!16 天前
持续集成 CI/CD-Jenkins持续集成GitLab项目打包docker镜像推送k8s集群并部署至rancher
ci/cd·gitlab·jenkins·rancher