如何启用GitLab容器注册表并将Docker镜像推送到其中

huangapple go评论86阅读模式
英文:

How to enable GitLab Container Registry and push docker image in it

问题

如何在GitLab Community Edition 14.2.4中使用域名方法启用容器注册表,并如何将Docker镜像推送到注册表?另外,如何将GitLab CI与注册表集成?

只需按照“配置容器注册表使用其域名”中所述,将以下字符串指定为:

  1. registry_external_url 'registry.gitlab.example.com'
英文:

What are the exact steps to enable Container Registry in GitLab Community Edition 14.2.4 using the domain method, and how can I push a docker image to the registry? Also, how can I integrate GitLab CI with the registry?

Specifying only the following string as described in "Configure Container Registry under its domain" not working for GitLab Community Edition 14.2.4

  1. registry_external_url 'registry.gitlab.example.com'

答案1

得分: 1

这是一个自我回答的主题,我将指导您完成在GitLab社区版14.2.4中启用容器注册表以及如何推送Docker映像的步骤。

第1步: 为了启用容器注册表,我使用了域方法(在现有域的配置下,详见此处)。编辑您的/etc/gitlab/gitlab.rb文件并添加以下代码行:

  1. registry_external_url 'https://registry.example.com';
  2. gitlab_rails['registry_enabled'] = true
  3. gitlab_rails['registry_host'] = "registry.example.com"
  4. registry['enable'] = true
  5. registry['registry_http_addr'] = "localhost:5000"
  6. registry['log_directory'] = "/var/log/gitlab/registry"
  7. registry['env_directory'] = "/opt/gitlab/etc/registry/env"
  8. registry_nginx['enable'] = true
  9. registry_nginx['listen_port'] = 443
  10. registry_nginx['ssl_certificate'] = "/etc/gitlab/ssl/registry.example.com/fullchain.pem"
  11. registry_nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/registry.example.com/privkey.pem"

在添加这些行之后,运行 gitlab-ctl reconfigure。请注意,我尝试了不同的方法,包括仅指定registry_external_url URL,但直到我使用上述设置才起作用。在本文中,我跳过了安全认证步骤,这一步可以采用不同的方式完成。我使用了预生成的Let's Encrypt证书,并为注册表指定了更多信息,请查看此处

第2步: 启用容器注册表后,您可以在GitLab中找到它,方法是导航到项目 -> 软件包和注册表 -> 容器注册表。在空白页面上,您将找到有关如何开始使用注册表的帮助命令。

第3步: 要将GitLab CI与容器注册表集成,将以下代码添加到您的.gitlab-ci.yml文件:

  1. create-image:
  2. stage: build
  3. tags:
  4. - shell
  5. variables:
  6. VER: 1.0-${CI_PIPELINE_ID}
  7. ID: ${CI_COMMIT_SHORT_SHA}
  8. GIT_SUBMODULE_STRATEGY: recursive
  9. before_script:
  10. - echo "Docker registry url is $CI_REGISTRY"
  11. - echo "Docker registry username is $CI_REGISTRY_USER"
  12. - echo "Docker registry repo is $CI_REGISTRY_IMAGE"
  13. timeout: 12h
  14. script:
  15. - docker build -t ${CI_REGISTRY_IMAGE}:$VER ${CI_PROJECT_DIR}
  16. upload-to-registry:
  17. stage: deploy
  18. when: manual
  19. tags:
  20. - shell
  21. script:
  22. - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  23. - docker push ${CI_REGISTRY_IMAGE}:$VER
英文:

This is a self-answering topic, I will guide you through the steps to enable Container Registry in GitLab Community Edition 14.2.4 and how to push a Docker image.

Step 1: To enable Container Registry, I used the domain method(configuration under existing domain described here). Edit your /etc/gitlab/gitlab.rb file and add the following lines of code:

  1. registry_external_url 'https://registry.example.com'
  2. gitlab_rails['registry_enabled'] = true
  3. gitlab_rails['registry_host'] = "registry.example.com"
  4. registry['enable'] = true
  5. registry['registry_http_addr'] = "localhost:5000"
  6. registry['log_directory'] = "/var/log/gitlab/registry"
  7. registry['env_directory'] = "/opt/gitlab/etc/registry/env"
  8. registry_nginx['enable'] = true
  9. registry_nginx['listen_port'] = 443
  10. registry_nginx['ssl_certificate'] = "/etc/gitlab/ssl/registry.example.com/fullchain.pem"
  11. registry_nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/registry.example.com/privkey.pem"

After adding these lines, run gitlab-ctl reconfigure. Note that I tried different approaches, including specifying only the registry_external_url URL, but nothing worked until I used the above setup. In this article I skipped the security certification steps, this step could be done differently. I have used Let's Encrypt certificates pre-generated and specified for the registry. More information here

Step 2: Once Container Registry is enabled, you can find it in GitLab by navigating to Project -> Packages & Registries -> Container Registry. On the empty page, you will find help commands to start working with the registry.

Step 3: To integrate GitLab CI with Container Registry, add the following code to your .gitlab-ci.yml file:

  1. create-image:
  2. stage: build
  3. tags:
  4. - shell
  5. variables:
  6. VER: 1.0-${CI_PIPELINE_ID}
  7. ID: ${CI_COMMIT_SHORT_SHA}
  8. GIT_SUBMODULE_STRATEGY: recursive
  9. before_script:
  10. - echo "Docker registry url is $CI_REGISTRY"
  11. - echo "Docker registry username is $CI_REGISTRY_USER"
  12. - echo "Docker registry repo is $CI_REGISTRY_IMAGE"
  13. timeout: 12h
  14. script:
  15. - docker build -t ${CI_REGISTRY_IMAGE}:$VER ${CI_PROJECT_DIR}
  16. upload-to-registry:
  17. stage: deploy
  18. when: manual
  19. tags:
  20. - shell
  21. script:
  22. - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  23. - docker push ${CI_REGISTRY_IMAGE}:$VER

huangapple
  • 本文由 发表于 2023年3月31日 17:59:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定