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
相关推荐
daemon.qiang10 小时前
国内虚拟机对接 Freedesktop:Fork xserver、自建 Runner 与提交 MR 实战
linux·ubuntu·centos·gitlab·开源软件
snow@li5 天前
服务器运维:K3S 下 Jenkins ↔ GitLab 的 CI/CD 闭环 / 访问成功
运维·gitlab·jenkins
旧梦星轨7 天前
Git Flow 工作流实践
git·gitee·gitlab·github
旧梦星轨7 天前
Github Flow 与 Gitlab Flow工作流实践解读
gitlab·github
极小狐10 天前
干掉流水线里的长期密钥:用 OIDC 让 CI/CD 与云以临时凭证对接
microsoft·ci/cd·gitlab·devops·ci·cd·oidc
snow@li10 天前
服务器运维:阿里云安装软件gitlab/智能体协助安装/智能体协同运维
运维·gitlab
极小狐11 天前
CI 流水线提速实战:缓存设计与依赖代理的工程实践
缓存·ci/cd·gitlab·devops
DevOps老兵12 天前
AI Infra实战11:模型部署Pipeline——CI/CD自动化
人工智能·ci/cd·ai·自动化·gitlab
极小狐12 天前
从复制粘贴到可复用组件:极狐GitLab CI/CD 组件目录实战
java·ci/cd·gitlab·devops·组件化
极小狐12 天前
用 Flow 编排 Agent 智能体:极狐GitLab Duo 自定义工作流实战
ai·gitlab·agent·devops·flow·duo