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
相关推荐
dleei12 小时前
使用docker创建gitlab仓库
前端·docker·gitlab
hasnum18 小时前
探索 GitLab-Connect:一个连接 GitLab 的轻量级工具
gitlab
极小狐2 天前
极狐GitLab 17.9 正式发布,40+ DevSecOps 重点功能解读【四】
gitlab·devsecops·devops
极小狐3 天前
极狐GitLab 17.9 正式发布,40+ DevSecOps 重点功能解读【三】
gitlab·devsecops·devops
极小狐3 天前
极狐GitLab 17.9 正式发布,40+ DevSecOps 重点功能解读【二】
gitlab·devsecops·devops
lion_zjg3 天前
DOCKER模式部署GITLAB
docker·容器·gitlab
RedCong4 天前
Gitlab配置personal access token
gitlab
Super_man541884 天前
gitlab+jenkins+harbor+k8s安装操作流程之harbor
运维·容器·kubernetes·gitlab·jenkins
极小狐4 天前
极狐GitLab 正式发布安全版本17.9.1、17.8.4、17.7.6
安全·gitlab·devsecops·devops
XMYX-05 天前
代码托管平台对比分析:Gitee与GitLab
gitee·gitlab