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
相关推荐
tingting01198 小时前
Anolis OS release 8.10安装gitlab
gitlab
shchojj8 小时前
gitlab推送触发jekins编译
运维·gitlab
独隅1 天前
Git/GitHub/GitLab/Gitee 核心对比指南
git·gitlab·github
开开心心就好1 天前
清理重复文件释放C盘空间的工具
安全·智能手机·pdf·gitlab·音视频·intellij idea·1024程序员节
李少兄1 天前
IDE 提示 “GitLab versions older than 14.0 are not supported” 的深度排查与解决方案
ide·gitlab
惜年_night3 天前
Docker部署05-GitLab的CI-CD发布
ci/cd·docker·gitlab
无人生还别怕4 天前
搭建gitlab服务并接入openldap认证
git·gitlab·github·openldap·ldap·统一认证
*_潇_*4 天前
0095__日常--记一次gitlab Runner配置与CI/CD环境搭建流程
ci/cd·gitlab
开发者联盟league5 天前
在ubuntu上使用apt方式安装gitlab
linux·ubuntu·gitlab
张小凡vip5 天前
gitlab的ci配置文件yaml参数说明
git·ci/cd·gitlab