# GitLab Container Registry
> 原文:[https://docs.gitlab.com/ee/user/packages/container_registry/](https://docs.gitlab.com/ee/user/packages/container_registry/)
* [Enable the Container Registry for your project](#enable-the-container-registry-for-your-project)
* [Control Container Registry from within GitLab](#control-container-registry-from-within-gitlab)
* [Control Container Registry for your project](#control-container-registry-for-your-project)
* [Control Container Registry for your group](#control-container-registry-for-your-group)
* [Image Repository details page](#image-repository-details-page)
* [Use images from GitLab Container Registry](#use-images-from-gitlab-container-registry)
* [Authenticating to the GitLab Container Registry](#authenticating-to-the-gitlab-container-registry)
* [Build and push images from your local machine](#build-and-push-images-from-your-local-machine)
* [Build and push images using GitLab CI/CD](#build-and-push-images-using-gitlab-cicd)
* [Authenticating to the Container Registry with GitLab CI/CD](#authenticating-to-the-container-registry-with-gitlab-cicd)
* [Container Registry examples with GitLab CI/CD](#container-registry-examples-with-gitlab-cicd)
* [Using a Docker-in-Docker image from your Container Registry](#using-a-docker-in-docker-image-from-your-container-registry)
* [Delete images](#delete-images)
* [Delete images from within GitLab](#delete-images-from-within-gitlab)
* [Delete images using the API](#delete-images-using-the-api)
* [Delete images using GitLab CI/CD](#delete-images-using-gitlab-cicd)
* [Delete images by using a cleanup policy](#delete-images-by-using-a-cleanup-policy)
* [Cleanup policy](#cleanup-policy)
* [Managing project cleanup policy through the UI](#managing-project-cleanup-policy-through-the-ui)
* [Troubleshooting cleanup policies](#troubleshooting-cleanup-policies)
* [Managing project cleanup policy through the API](#managing-project-cleanup-policy-through-the-api)
* [Use with external container registries](#use-with-external-container-registries)
* [Regex pattern examples](#regex-pattern-examples)
* [Use the Container Registry to store Helm Charts](#use-the-container-registry-to-store-helm-charts)
* [Limitations](#limitations)
* [Troubleshooting the GitLab Container Registry](#troubleshooting-the-gitlab-container-registry)
* [Docker connection error](#docker-connection-error)
* [Troubleshoot as a GitLab server admin](#troubleshoot-as-a-gitlab-server-admin)
* [Unable to change path or transfer a project](#unable-to-change-path-or-transfer-a-project)
# GitLab Container Registry[](#gitlab-container-registry "Permalink")
版本歷史
* 在 GitLab 8.8 中[引入](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/4040) .
* 在 GitLab 8.9 中添加了 Docker Registry manifest `v1`支持,以支持 1.10 之前的 Docker 版本.
* 從 GitLab 8.12 開始,如果您的帳戶中啟用了 2FA,則需要傳遞[個人訪問令牌(](../../profile/personal_access_tokens.html)而不是密碼)才能登錄到 GitLab 的 Container Registry.
* 在 GitLab 9.1 中添加了多級圖像名稱支持.
* 組級容器注冊表是在 GitLab 12.10 中[引入的](https://gitlab.com/gitlab-org/gitlab/-/issues/23315) .
* 在 GitLab 13.0 中[引入](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/31322)了按圖像存儲庫名稱搜索.
**注意:**本文檔是用戶指南. 要了解如何在整個 GitLab 實例上啟用 GitLab 容器注冊表,請訪問[管理員文檔](../../../administration/packages/container_registry.html) .
通過將 Docker 容器注冊表集成到 GitLab 中,每個項目都可以擁有自己的空間來存儲其 Docker 映像.
您可以在[https://docs.docker.com/registry/introduction/上](https://s0docs0docker0com.icopy.site/registry/introduction/)了解有關 Docker Registry 的更多信息.
[](img/container_registry_repositories_v13_1.png)
## Enable the Container Registry for your project[](#enable-the-container-registry-for-your-project "Permalink")
**警告:**容器注冊表遵循項目的可見性設置. 如果項目是公開的,則容器注冊表也是如此.
If you cannot find the **包和注冊表>容器注冊表** entry under your project’s sidebar, it is not enabled in your GitLab instance. Ask your administrator to enable GitLab Container Registry following the [administration documentation](../../../administration/packages/container_registry.html).
如果您使用的是 GitLab.com,則默認情況下啟用此功能,因此您可以立即開始使用注冊表. 當前,作為[存儲庫大小限制的](../../project/repository/index.html)一部分,GitLab.com 上的 Registry 有一個軟的(10GB)大小限制.
為您的 GitLab 實例啟用后,要為您的項目啟用 Container Registry:
1. 轉到項目的**"設置">"常規"**頁面.
2. 展開" **可見性","項目功能","權限"**部分,并在項目上啟用" **容器注冊表"**功能. 對于新項目,默認情況下可以啟用. 對于現有項目(GitLab 8.8 之前的版本),您必須顯式啟用它.
3. 按**保存更改**以使更改生效. 現在,您應該能夠在側欄中看到**Packages&Registries> Container Registry**鏈接.
## Control Container Registry from within GitLab[](#control-container-registry-from-within-gitlab "Permalink")
GitLab 提供了一個簡單的 Container Registry 管理面板. 該管理面板可用于項目和組.
### Control Container Registry for your project[](#control-container-registry-for-your-project "Permalink")
導航到您項目的 **包裹和注冊>集裝箱注冊** .
[](img/container_registry_repositories_with_quickstart_v13_1.png)
該視圖將:
* 顯示屬于該項目的所有圖像存儲庫.
* 允許您按圖像存儲庫的名稱過濾.
* 允許您[刪除](#delete-images-from-within-gitlab)一個或多個圖像存儲庫.
* 允許您導航到圖像存儲庫詳細信息頁面.
* 顯示具有最常用命令的**快速入門**下拉列表,以登錄,構建和推送
* (可選)如果為該項目啟用了[清理策略](#cleanup-policy) ,則將顯示橫幅.
### Control Container Registry for your group[](#control-container-registry-for-your-group "Permalink")
導航到您小組的 **包裹和注冊>集裝箱注冊** .
[](img/container_registry_group_repositories_v13_1.png)
該視圖將:
* Show all the image repositories of the projects that belong to this group.
* 允許[刪除](#delete-images-from-within-gitlab)一個或多個圖像存儲庫.
* 允許導航到特定的圖像存儲庫詳細信息頁面.
### Image Repository details page[](#image-repository-details-page "Permalink")
單擊任何圖像存儲庫的名稱將導航到詳細信息.
[](img/container_registry_repository_details_v13.0.png)
**注意:**下一頁在**組級別容器注冊表**和**項目級別容器注冊表中**具有相同的功能.
此視圖:
* 顯示所有圖像存儲庫詳細信息.
* 顯示圖像存儲庫的所有標記.
* 允許您快速復制標簽路徑(通過單擊標簽名稱附近的剪貼板按鈕).
* 允許您[刪除一個或多個標簽](#delete-images-from-within-gitlab) .
## Use images from GitLab Container Registry[](#use-images-from-gitlab-container-registry "Permalink")
要從 GitLab 容器注冊表中托管的映像下載并運行容器,請使用`docker run` :
```
docker run [options] registry.example.com/group/project/image [arguments]
```
有關運行 Docker 容器的更多信息,請訪問[Docker 文檔](https://s0docs0docker0com.icopy.site/engine/userguide/intro/) .
## Authenticating to the GitLab Container Registry[](#authenticating-to-the-gitlab-container-registry "Permalink")
如果訪問項目菜單下的**Packages&Registries> Container Registry**鏈接,則可以看到使用 GitLab 憑據登錄到 Container Registry 的明確說明.
例如,如果注冊表的 URL 是`registry.example.com` ,那么您應該能夠使用以下命令登錄:
```
docker login registry.example.com
```
**注意:**如果您的帳戶中啟用了[2 因子身份驗證](../../profile/account/two_factor_authentication.html) ,則需要傳遞[個人訪問令牌(](../../profile/personal_access_tokens.html)而不是密碼)才能登錄到 GitLab 的 Container Registry.
如果項目是私有項目,則需要提供憑據進行授權. 有兩種方法可以做到這一點:
* 通過使用[個人訪問令牌](../../profile/personal_access_tokens.html) .
* 通過使用[部署令牌](../../project/deploy_tokens/index.html) .
兩者所需的最小范圍是`read_registry` .
使用令牌的示例:
```
docker login registry.example.com -u <username> -p <token>
```
## Build and push images from your local machine[](#build-and-push-images-from-your-local-machine "Permalink")
建立和發布圖像應該是一個簡單的過程. 只需確保您將注冊表 URL 與 GitLab 上托管的名稱空間和項目名稱一起使用即可:
```
docker build -t registry.example.com/group/project/image .
docker push registry.example.com/group/project/image
```
您的圖片將按照以下方案命名:
```
<registry URL>/<namespace>/<project>/<image>
```
GitLab 最多支持三個級別的圖像存儲庫名稱. 以下圖像標簽示例有效:
```
registry.example.com/group/project:some-tag
registry.example.com/group/project/image:latest
registry.example.com/group/project/my/image:rc1
```
## Build and push images using GitLab CI/CD[](#build-and-push-images-using-gitlab-cicd "Permalink")
While you can build and push your images from your local machine, the true power of the Container Registry comes when you combine it with GitLab CI/CD. You can then create workflows and automate any processes that involve testing, building, and eventually deploying your project from the Docker image you created.
在深入研究細節之前,您應該注意一些事項:
* 運行任何命令之前,必須[先向容器注冊表](#authenticating-to-the-container-registry-with-gitlab-cicd)進行[身份驗證](#authenticating-to-the-container-registry-with-gitlab-cicd) . 如果有多個作業依賴于它,則可以在`before_script`執行此操作.
* 使用`docker build --pull`在構建之前獲取對基礎映像的所有更改,以防緩存過時. 它花費的時間稍長一些,但這意味著如果沒有基礎映像的安全補丁,您將不會陷入困境.
* 在每次`docker run` docker 之前進行顯式`docker pull`都會獲取剛剛構建的最新映像. 如果您正在使用多個在本地緩存圖像的運行程序,則這一點尤其重要. 在圖像標簽中使用 Git SHA 使得此操作變得不必要,因為每個作業都是唯一的,并且您永遠都不會過時的圖像. 但是,如果在依賴項發生更改后重新構建給定的提交,則仍然可能會有陳舊的映像.
* 如果有多個作業同時發生,則您不想直接構建到`latest`標記.
### Authenticating to the Container Registry with GitLab CI/CD[](#authenticating-to-the-container-registry-with-gitlab-cicd "Permalink")
通過[GitLab CI / CD](../../../ci/yaml/README.html)向容器注冊表進行身份驗證的三種方法取決于您項目的可見性.
適用于所有項目,但更適合公共項目:
* **使用特殊的`CI_REGISTRY_USER`變量** :為您創建此變量指定的用戶,以便推送到連接到您的項目的注冊表. 它的密碼是使用`CI_REGISTRY_PASSWORD`變量自動設置的. 這使您可以自動構建和部署 Docker 映像,并具有對注冊表的讀/寫訪問權限. 這是短暫的,因此僅適用于一項工作. 您可以按原樣使用以下示例:
```
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
```
對于私人和內部項目:
* **使用個人訪問令牌** :如果您的項目是私有的,則可以創建和使用[個人訪問令牌](../../profile/personal_access_tokens.html) :
* 對于讀取(拉)訪問,范圍應為`read_registry` .
* 要進行讀/寫(拉/推)訪問,請使用`api` .
Replace the `<username>` and `<access_token>` in the following example:
```
docker login -u <username> -p <access_token> $CI_REGISTRY
```
* **使用 GitLab 部署令牌** :您可以在私有項目中創建和使用[特殊的部署令牌](../../project/deploy_tokens/index.html#gitlab-deploy-token) . 它提供對注冊表的只讀(拉)訪問. 創建后,您可以使用特殊的環境變量,GitLab CI / CD 將為您填充它們. 您可以按原樣使用以下示例:
```
docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
```
### Container Registry examples with GitLab CI/CD[](#container-registry-examples-with-gitlab-cicd "Permalink")
如果您在 Runners 上使用 Docker-in-Docker,則`.gitlab-ci.yml`外觀應與此類似:
```
build:
image: docker:19.03.12
stage: build
services:
- docker:19.03.12-dind
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $CI_REGISTRY/group/project/image:latest .
- docker push $CI_REGISTRY/group/project/image:latest
```
您還可以利用[其他變量](../../../ci/variables/README.html)來避免硬編碼:
```
build:
image: docker:19.03.12
stage: build
services:
- docker:19.03.12-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG
```
在這里, `$CI_REGISTRY_IMAGE`將解析為與此項目`$CI_REGISTRY_IMAGE`的注冊表的地址. 由于`$CI_COMMIT_REF_NAME`解析為分支名稱或標記名稱,并且您的分支名稱可以包含正斜杠(例如,feature / my-feature),因此將`$CI_COMMIT_REF_SLUG`用作圖像標記`$CI_COMMIT_REF_SLUG`安全. 這是因為圖像標記不能包含正斜杠. 我們還聲明了自己的變量`$IMAGE_TAG` ,將兩者結合起來可以節省一些在`script`部分鍵入的內容.
這是一個更詳細的示例,將任務分為 4 個管道階段,包括兩個并行運行的測試. 內部`build`存儲在容器注冊表中,供后續階段使用,并在需要時下載映像. 對`master`所做的更改也將被標記為`latest`并使用特定于應用程序的部署腳本進行部署:
```
image: docker:19.03.12
services:
- docker:19.03.12-dind
stages:
- build
- test
- release
- deploy
variables:
# Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build:
stage: build
script:
- docker build --pull -t $CONTAINER_TEST_IMAGE .
- docker push $CONTAINER_TEST_IMAGE
test1:
stage: test
script:
- docker pull $CONTAINER_TEST_IMAGE
- docker run $CONTAINER_TEST_IMAGE /script/to/run/tests
test2:
stage: test
script:
- docker pull $CONTAINER_TEST_IMAGE
- docker run $CONTAINER_TEST_IMAGE /script/to/run/another/test
release-image:
stage: release
script:
- docker pull $CONTAINER_TEST_IMAGE
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
- docker push $CONTAINER_RELEASE_IMAGE
only:
- master
deploy:
stage: deploy
script:
- ./deploy.sh
only:
- master
```
**注意:**此示例顯式調用`docker pull` . 如果您希望使用`image:`隱式拉出構建的映像,并使用[Docker](https://docs.gitlab.com/runner/executors/docker.html)或[Kubernetes](https://docs.gitlab.com/runner/executors/kubernetes.html)執行程序,請確保[`pull_policy`](https://docs.gitlab.com/runner/executors/docker.html)設置為`always` .
### Using a Docker-in-Docker image from your Container Registry[](#using-a-docker-in-docker-image-from-your-container-registry "Permalink")
如果要為 Docker-in-Docker 使用自己的 Docker 映像,除了[Docker-in-Docker](../../../ci/docker/using_docker_build.html#use-docker-in-docker-workflow-with-docker-executor)部分中的步驟外,還需要做一些其他事情:
1. 更新`image`和`service`以指向您的注冊表.
2. 添加服務[別名](../../../ci/yaml/README.html#servicesalias) .
以下是您的`.gitlab-ci.yml`外觀示例:
```
build:
image: $CI_REGISTRY/group/project/docker:19.03.12
services:
- name: $CI_REGISTRY/group/project/docker:19.03.12-dind
alias: docker
stage: build
script:
- docker build -t my-docker-image .
- docker run my-docker-image /script/to/run/tests
```
如果您忘記設置服務別名,則`docker:19.03.12`映像將找不到`dind`服務,并且將引發類似以下的錯誤:
```
error during connect: Get http://docker:2376/v1.39/info: dial tcp: lookup docker on 192.168.0.1:53: no such host
```
## Delete images[](#delete-images "Permalink")
您可以通過多種方式從 Container Registry 中刪除圖像.
**警告:**刪除圖像是一種破壞性行為,無法撤消. 要還原已刪除的圖像,必須重建并重新上傳它.**注意:**管理員應查看如何[垃圾收集](../../../administration/packages/container_registry.html#container-registry-garbage-collection)刪除的圖像.
### Delete images from within GitLab[](#delete-images-from-within-gitlab "Permalink")
要從 GitLab 中刪除圖像:
1. Navigate to your project’s or group’s **包和注冊表>容器注冊表**.
2. 在" **容器注冊表"**頁面上,可以通過以下任一方法選擇要刪除的內容:
* 通過單擊紅色刪除整個存儲庫及其包含的所有標簽 **垃圾桶**圖標.
* 導航到存儲庫,然后通過單擊紅色單獨或批量刪除標簽 您要刪除的標簽旁邊的**垃圾桶**圖標.
3. 在對話框中,點擊**刪除標簽** .
[](img/container_registry_repository_details_v13.0.png)
### Delete images using the API[](#delete-images-using-the-api "Permalink")
如果要自動執行刪除圖像的過程,GitLab 提供了一個 API. 有關更多信息,請參見以下端點:
* [Delete a Registry repository](../../../api/container_registry.html#delete-registry-repository)
* [Delete an individual Registry repository tag](../../../api/container_registry.html#delete-a-registry-repository-tag)
* [Delete Registry repository tags in bulk](../../../api/container_registry.html#delete-registry-repository-tags-in-bulk)
### Delete images using GitLab CI/CD[](#delete-images-using-gitlab-cicd "Permalink")
**警告:** GitLab CI / CD 沒有提供刪除圖像的內置方法,但是本示例使用了名為[reg](https://github.com/genuinetools/reg)的第三方工具,該工具與 GitLab Registry API 進行通信. 您應對自己的行為負責. 有關此工具的幫助,請參見[reg 的發行隊列](https://github.com/genuinetools/reg/issues) .
以下示例定義了兩個階段: `build`和`clean` . `build_image`作業將為分支構建 Docker 映像,而`delete_image`作業將其刪除. `reg`可執行文件已下載并用于刪除與`$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG` [環境變量](../../../ci/variables/predefined_variables.html)匹配的映像.
要使用此示例,請更改`IMAGE_TAG`變量以符合您的需求:
```
stages:
- build
- clean
build_image:
image: docker:19.03.12
stage: build
services:
- docker:19.03.12-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG
only:
- branches
except:
- master
delete_image:
image: docker:19.03.12
stage: clean
services:
- docker:19.03.12-dind
variables:
IMAGE_TAG: $CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG
REG_SHA256: ade837fc5224acd8c34732bf54a94f579b47851cc6a7fd5899a98386b782e228
REG_VERSION: 0.16.1
before_script:
- apk add --no-cache curl
- curl --fail --show-error --location "https://github.com/genuinetools/reg/releases/download/v$REG_VERSION/reg-linux-amd64" --output /usr/local/bin/reg
- echo "$REG_SHA256 /usr/local/bin/reg" | sha256sum -c -
- chmod a+x /usr/local/bin/reg
script:
- /usr/local/bin/reg rm -d --auth-url $CI_REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $IMAGE_TAG
only:
- branches
except:
- master
```
**提示:**您可以從[發布頁面](https://github.com/genuinetools/reg/releases)下載最新的`reg`版本,然后通過更改`delete_image`作業中定義的`REG_SHA256`和`REG_VERSION`變量來更新代碼示例.
### Delete images by using a cleanup policy[](#delete-images-by-using-a-cleanup-policy "Permalink")
您可以創建每個項目的[清理策略,](#cleanup-policy)以確保定期從 Container Registry 中刪除較舊的標記和圖像.
## Cleanup policy[](#cleanup-policy "Permalink")
版本歷史
* 在 GitLab 12.8 中[引入](https://gitlab.com/gitlab-org/gitlab/-/issues/15398) .
* 在 GitLab 13.2 中從"過期策略" [重命名](https://gitlab.com/gitlab-org/gitlab/-/issues/218737)為"清理策略".
對于特定項目,如果要刪除不再需要的標簽,則可以創建清除策略. 應用該策略時,將刪除與正則表達式模式匹配的標記. 底層和圖像仍然保留.
要刪除不再與任何標簽關聯的基礎層和圖像,實例管理員可以通過`-m`開關使用[垃圾回收](../../../administration/packages/container_registry.html#removing-unused-layers-not-referenced-by-manifests) .
**注意:**對于 GitLab.com,清理策略不適用于將該功能部署到生產之前(2020 年 2 月)創建的項目. 計劃在 GitLab.com 上[為](https://gitlab.com/gitlab-org/gitlab/-/issues/196124)現有項目提供支持. 對于自我管理的實例,管理員可以在[GitLab 應用程序設置中](../../../api/settings.html#change-application-settings)通過將`container_expiration_policies_enable_historic_entries`設置為 true 來啟用清除策略. 注意[涉及](./index.html#use-with-external-container-registries)的固有[風險](./index.html#use-with-external-container-registries) .
清理策略算法首先從列表中收集給定存儲庫的所有標記,然后進行從標記中排除標記的過程,直到僅保留要刪除的標記為止:
1. 在列表中收集給定存儲庫的所有標記.
2. 排除名為標簽`latest`從列表中.
3. 評估`name_regex` ,從列表中排除不匹配的名稱.
4. 排除所有沒有清單的標簽(不是選項的一部分).
5. 按`created_date`其余標簽.
6. 從列表中排除基于`keep_n`值的 N 個標簽(要保留的標簽數).
7. 從列表中排除比`older_than`值(清除間隔)更新的標簽.
8. 從列表中排除與`name_regex_keep`值匹配的所有標簽(要保留的圖像).
9. 最后,列表中的其余標簽將從 Container Registry 中刪除.
### Managing project cleanup policy through the UI[](#managing-project-cleanup-policy-through-the-ui "Permalink")
要管理項目清理策略,請導航至 **設置> CI / CD>容器注冊表標記清除策略** .
用戶界面允許您配置以下內容:
* **清理策略:**啟用或禁用清理策略.
* **清除間隔:**標簽可以刪除多長時間.
* **清理計劃:**檢查標簽的 cron 作業應運行的頻率.
* **要保留的標簽數量:**每個圖像*始終*保留多少個標簽.
* **名稱與該正則表達式模式匹配的 Docker 標簽將過期:**用于確定應清除哪些標簽的正則表達式. 要使所有標簽都合格,請使用默認值`.*` .
* **名稱與該正則表達式模式匹配的 Docker 標簽將被保留:**用于確定應保留哪些標簽的正則表達式. 要保留所有標簽,請使用默認值`.*` .
#### Troubleshooting cleanup policies[](#troubleshooting-cleanup-policies "Permalink")
如果看到以下消息:
"更新清理策略時出了點問題."
檢查正則表達式模式以確保它們有效.
您可以使用[Rubular](https://rubular.com/)檢查您的正則表達式. 查看一些常見的[regex 模式示例](#regex-pattern-examples) .
### Managing project cleanup policy through the API[](#managing-project-cleanup-policy-through-the-api "Permalink")
您可以使用 GitLab API 設置,更新和禁用清理策略.
Examples:
* 選擇所有標簽,每個圖像至少保留 1 個標簽,清理超過 14 天的所有標簽,每月運行一次,使用名稱`master`保留所有圖像并啟用該策略:
```
curl --request PUT --header 'Content-Type: application/json;charset=UTF-8' --header "PRIVATE-TOKEN: <your_access_token>" --data-binary '{"container_expiration_policy_attributes":{"cadence":"1month","enabled":true,"keep_n":1,"older_than":"14d","name_regex":"","name_regex_delete":".*","name_regex_keep":".*-master"}}' 'https://gitlab.example.com/api/v4/projects/2'
```
有關更多詳細信息,請參見 API 文檔: [編輯項目](../../../api/projects.html#edit-project) .
### Use with external container registries[](#use-with-external-container-registries "Permalink")
使用[外部容器注冊表時](./../../../administration/packages/container_registry.html#use-an-external-container-registry-with-gitlab-as-an-auth-endpoint) ,在項目上運行清除策略可能會帶來性能風險. 如果項目要運行的策略將刪除大量標簽(成千上萬個),則運行該策略的 GitLab 后臺作業可能會備份或完全失敗. 如果您確信要清除的標簽數量很少,建議僅對在 GitLab 12.8 之前創建的項目啟用容器清理策略.
### Regex pattern examples[](#regex-pattern-examples "Permalink")
清理策略使用正則表達式模式來確定應在 UI 和 API 中保留或刪除哪些標簽.
以下是您可能要使用的正則表達式模式的示例:
* 匹配所有標簽:
```
.*
```
* 匹配以`v`開頭的標簽:
```
v.+
```
* 匹配包含`master`標簽:
```
master
```
* 匹配以`v`開頭,包含`master`或包含`release`標簽:
```
(?:v.+|master|release)
```
## Use the Container Registry to store Helm Charts[](#use-the-container-registry-to-store-helm-charts "Permalink")
隨著[Helm v3](https://helm.sh/docs/topics/registries/)的發布,您可以使用 Container Registry 存儲 Helm Charts. 但是,由于 Docker 傳遞和存儲元數據的方式,GitLab 無法解析此數據并達到性能標準. [本史詩](https://gitlab.com/groups/gitlab-org/-/epics/2313)更新了 Container Registry 的體系結構以支持 Helm Charts.
您可以[在此處](https://gitlab.com/gitlab-org/gitlab/-/issues/38047#note_298842890)閱讀有關上述挑戰的更多信息.
## Limitations[](#limitations "Permalink")
* 推送圖像后,不支持移動或重命名現有 Container Registry 存儲庫,因為圖像已簽名,并且簽名包含存儲庫名稱. 要使用 Container Registry 移動或重命名存儲庫,必須刪除所有現有映像.
* 在 GitLab 12.10 之前,清理策略不會刪除使用與`latest`標簽相同的圖像 ID 的任何標簽.
## Troubleshooting the GitLab Container Registry[](#troubleshooting-the-gitlab-container-registry "Permalink")
### Docker connection error[](#docker-connection-error "Permalink")
當組,項目或分支名??稱中包含特殊字符時,可能會發生 Docker 連接錯誤. 特殊字符可以包括:
* 領先的下劃線
* 尾隨連字符/破折號
為了解決這個問題,您可以[更改組路徑](../../group/index.html#changing-a-groups-path) , [更改項目路徑](../../project/settings/index.html#renaming-a-repository)或更改分支名稱.
### Troubleshoot as a GitLab server admin[](#troubleshoot-as-a-gitlab-server-admin "Permalink")
大多數情況下,對 GitLab 容器注冊表進行故障排除需要管理員對 GitLab 服務器的訪問.
[Read how to troubleshoot the Container Registry](../../../administration/packages/container_registry.html#troubleshooting).
### Unable to change path or transfer a project[](#unable-to-change-path-or-transfer-a-project "Permalink")
如果您嘗試更改項目的路徑或將項目轉移到新的名稱空間,則可能會收到以下錯誤之一:
* "由于容器的注冊表中存在標簽,因此無法傳輸項目."
* "無法移動名稱空間,因為至少一個項目在容器注冊表中具有標簽."
當項目在容器注冊表中有圖像時,會發生此問題. 必須先刪除或移動這些圖像,然后才能更改路徑或傳輸項目.
以下過程使用這些樣本項目名稱:
* 對于當前項目: `example.gitlab.com/org/build/sample_project/cr:v2.9.1` : `example.gitlab.com/org/build/sample_project/cr:v2.9.1`
* 對于新項目: `example.gitlab.com/new_org/build/new_sample_project/cr:v2.9.1` : `example.gitlab.com/new_org/build/new_sample_project/cr:v2.9.1`
使用您自己的 URL 完成以下步驟:
1. 將 Docker 映像下載到您的計算機上:
```
docker login example.gitlab.com
docker pull example.gitlab.com/org/build/sample_project/cr:v2.9.1
```
2. 重命名圖像以匹配新的項目名稱:
```
docker tag example.gitlab.com/org/build/sample_project/cr:v2.9.1 example.gitlab.com/new_org/build/new_sample_project/cr:v2.9.1
```
3. 使用[UI](#delete-images)或[API](../../../api/packages.html#delete-a-project-package)刪除兩個項目中的圖像. 圖像排隊和刪除時可能會有延遲.
4. 通過轉到**"設置">"常規"**并展開" **高級"**來更改路徑或傳輸項目.
5. 恢復圖像:
```
docker push example.gitlab.com/new_org/build/new_sample_project/cr:v2.9.1
```
有關詳細信息,請關注[此問題](https://gitlab.com/gitlab-org/gitlab/-/issues/18383) .
- GitLab Docs
- Installation
- Requirements
- GitLab cloud native Helm Chart
- Install GitLab with Docker
- Installation from source
- Install GitLab on Microsoft Azure
- Installing GitLab on Google Cloud Platform
- Installing GitLab on Amazon Web Services (AWS)
- Analytics
- Code Review Analytics
- Productivity Analytics
- Value Stream Analytics
- Kubernetes clusters
- Adding and removing Kubernetes clusters
- Adding EKS clusters
- Adding GKE clusters
- Group-level Kubernetes clusters
- Instance-level Kubernetes clusters
- Canary Deployments
- Cluster Environments
- Deploy Boards
- GitLab Managed Apps
- Crossplane configuration
- Cluster management project (alpha)
- Kubernetes Logs
- Runbooks
- Serverless
- Deploying AWS Lambda function using GitLab CI/CD
- Securing your deployed applications
- Groups
- Contribution Analytics
- Custom group-level project templates
- Epics
- Manage epics
- Group Import/Export
- Insights
- Issues Analytics
- Iterations
- Public access
- SAML SSO for GitLab.com groups
- SCIM provisioning using SAML SSO for GitLab.com groups
- Subgroups
- Roadmap
- Projects
- GitLab Secure
- Security Configuration
- Container Scanning
- Dependency Scanning
- Dependency List
- Static Application Security Testing (SAST)
- Secret Detection
- Dynamic Application Security Testing (DAST)
- GitLab Security Dashboard
- Offline environments
- Standalone Vulnerability pages
- Security scanner integration
- Badges
- Bulk editing issues and merge requests at the project level
- Code Owners
- Compliance
- License Compliance
- Compliance Dashboard
- Create a project
- Description templates
- Deploy Keys
- Deploy Tokens
- File finder
- Project integrations
- Integrations
- Atlassian Bamboo CI Service
- Bugzilla Service
- Custom Issue Tracker service
- Discord Notifications service
- Enabling emails on push
- GitHub project integration
- Hangouts Chat service
- Atlassian HipChat
- Irker IRC Gateway
- GitLab Jira integration
- Mattermost Notifications Service
- Mattermost slash commands
- Microsoft Teams service
- Mock CI Service
- Prometheus integration
- Redmine Service
- Slack Notifications Service
- Slack slash commands
- GitLab Slack application
- Webhooks
- YouTrack Service
- Insights
- Issues
- Crosslinking Issues
- Design Management
- Confidential issues
- Due dates
- Issue Boards
- Issue Data and Actions
- Labels
- Managing issues
- Milestones
- Multiple Assignees for Issues
- Related issues
- Service Desk
- Sorting and ordering issue lists
- Issue weight
- Associate a Zoom meeting with an issue
- Merge requests
- Allow collaboration on merge requests across forks
- Merge Request Approvals
- Browser Performance Testing
- How to create a merge request
- Cherry-pick changes
- Code Quality
- Load Performance Testing
- Merge Request dependencies
- Fast-forward merge requests
- Merge when pipeline succeeds
- Merge request conflict resolution
- Reverting changes
- Reviewing and managing merge requests
- Squash and merge
- Merge requests versions
- Draft merge requests
- Members of a project
- Migrating projects to a GitLab instance
- Import your project from Bitbucket Cloud to GitLab
- Import your project from Bitbucket Server to GitLab
- Migrating from ClearCase
- Migrating from CVS
- Import your project from FogBugz to GitLab
- Gemnasium
- Import your project from GitHub to GitLab
- Project importing from GitLab.com to your private GitLab instance
- Import your project from Gitea to GitLab
- Import your Jira project issues to GitLab
- Migrating from Perforce Helix
- Import Phabricator tasks into a GitLab project
- Import multiple repositories by uploading a manifest file
- Import project from repo by URL
- Migrating from SVN to GitLab
- Migrating from TFVC to Git
- Push Options
- Releases
- Repository
- Branches
- Git Attributes
- File Locking
- Git file blame
- Git file history
- Repository mirroring
- Protected branches
- Protected tags
- Push Rules
- Reduce repository size
- Signing commits with GPG
- Syntax Highlighting
- GitLab Web Editor
- Web IDE
- Requirements Management
- Project settings
- Project import/export
- Project access tokens (Alpha)
- Share Projects with other Groups
- Snippets
- Static Site Editor
- Wiki
- Project operations
- Monitor metrics for your CI/CD environment
- Set up alerts for Prometheus metrics
- Embedding metric charts within GitLab-flavored Markdown
- Embedding Grafana charts
- Using the Metrics Dashboard
- Dashboard YAML properties
- Metrics dashboard settings
- Panel types for dashboards
- Using Variables
- Templating variables for metrics dashboards
- Prometheus Metrics library
- Monitoring AWS Resources
- Monitoring HAProxy
- Monitoring Kubernetes
- Monitoring NGINX
- Monitoring NGINX Ingress Controller
- Monitoring NGINX Ingress Controller with VTS metrics
- Alert Management
- Error Tracking
- Tracing
- Incident Management
- GitLab Status Page
- Feature Flags
- GitLab CI/CD
- GitLab CI/CD pipeline configuration reference
- GitLab CI/CD include examples
- Introduction to CI/CD with GitLab
- Getting started with GitLab CI/CD
- How to enable or disable GitLab CI/CD
- Using SSH keys with GitLab CI/CD
- Migrating from CircleCI
- Migrating from Jenkins
- Auto DevOps
- Getting started with Auto DevOps
- Requirements for Auto DevOps
- Customizing Auto DevOps
- Stages of Auto DevOps
- Upgrading PostgreSQL for Auto DevOps
- Cache dependencies in GitLab CI/CD
- GitLab ChatOps
- Cloud deployment
- Docker integration
- Building Docker images with GitLab CI/CD
- Using Docker images
- Building images with kaniko and GitLab CI/CD
- GitLab CI/CD environment variables
- Predefined environment variables reference
- Where variables can be used
- Deprecated GitLab CI/CD variables
- Environments and deployments
- Protected Environments
- GitLab CI/CD Examples
- Test a Clojure application with GitLab CI/CD
- Using Dpl as deployment tool
- Testing a Phoenix application with GitLab CI/CD
- End-to-end testing with GitLab CI/CD and WebdriverIO
- DevOps and Game Dev with GitLab CI/CD
- Deploy a Spring Boot application to Cloud Foundry with GitLab CI/CD
- How to deploy Maven projects to Artifactory with GitLab CI/CD
- Testing PHP projects
- Running Composer and NPM scripts with deployment via SCP in GitLab CI/CD
- Test and deploy Laravel applications with GitLab CI/CD and Envoy
- Test and deploy a Python application with GitLab CI/CD
- Test and deploy a Ruby application with GitLab CI/CD
- Test and deploy a Scala application to Heroku
- GitLab CI/CD for external repositories
- Using GitLab CI/CD with a Bitbucket Cloud repository
- Using GitLab CI/CD with a GitHub repository
- GitLab Pages
- GitLab Pages
- GitLab Pages domain names, URLs, and baseurls
- Create a GitLab Pages website from scratch
- Custom domains and SSL/TLS Certificates
- GitLab Pages integration with Let's Encrypt
- GitLab Pages Access Control
- Exploring GitLab Pages
- Incremental Rollouts with GitLab CI/CD
- Interactive Web Terminals
- Optimizing GitLab for large repositories
- Metrics Reports
- CI/CD pipelines
- Pipeline Architecture
- Directed Acyclic Graph
- Multi-project pipelines
- Parent-child pipelines
- Pipelines for Merge Requests
- Pipelines for Merged Results
- Merge Trains
- Job artifacts
- Pipeline schedules
- Pipeline settings
- Triggering pipelines through the API
- Review Apps
- Configuring GitLab Runners
- GitLab CI services examples
- Using MySQL
- Using PostgreSQL
- Using Redis
- Troubleshooting CI/CD
- GitLab Package Registry
- GitLab Container Registry
- Dependency Proxy
- GitLab Composer Repository
- GitLab Conan Repository
- GitLab Maven Repository
- GitLab NPM Registry
- GitLab NuGet Repository
- GitLab PyPi Repository
- API Docs
- API resources
- .gitignore API
- GitLab CI YMLs API
- Group and project access requests API
- Appearance API
- Applications API
- Audit Events API
- Avatar API
- Award Emoji API
- Project badges API
- Group badges API
- Branches API
- Broadcast Messages API
- Project clusters API
- Group clusters API
- Instance clusters API
- Commits API
- Container Registry API
- Custom Attributes API
- Dashboard annotations API
- Dependencies API
- Deploy Keys API
- Deployments API
- Discussions API
- Dockerfiles API
- Environments API
- Epics API
- Events
- Feature Flags API
- Feature flag user lists API
- Freeze Periods API
- Geo Nodes API
- Group Activity Analytics API
- Groups API
- Import API
- Issue Boards API
- Group Issue Boards API
- Issues API
- Epic Issues API
- Issues Statistics API
- Jobs API
- Keys API
- Labels API
- Group Labels API
- License
- Licenses API
- Issue links API
- Epic Links API
- Managed Licenses API
- Markdown API
- Group and project members API
- Merge request approvals API
- Merge requests API
- Project milestones API
- Group milestones API
- Namespaces API
- Notes API
- Notification settings API
- Packages API
- Pages domains API
- Pipeline schedules API
- Pipeline triggers API
- Pipelines API
- Project Aliases API
- Project import/export API
- Project repository storage moves API
- Project statistics API
- Project templates API
- Projects API
- Protected branches API
- Protected tags API
- Releases API
- Release links API
- Repositories API
- Repository files API
- Repository submodules API
- Resource label events API
- Resource milestone events API
- Resource weight events API
- Runners API
- SCIM API
- Search API
- Services API
- Application settings API
- Sidekiq Metrics API
- Snippets API
- Project snippets
- Application statistics API
- Suggest Changes API
- System hooks API
- Tags API
- Todos API
- Users API
- Project-level Variables API
- Group-level Variables API
- Version API
- Vulnerabilities API
- Vulnerability Findings API
- Wikis API
- GraphQL API
- Getting started with GitLab GraphQL API
- GraphQL API Resources
- API V3 to API V4
- Validate the .gitlab-ci.yml (API)
- User Docs
- Abuse reports
- User account
- Active sessions
- Deleting a User account
- Permissions
- Personal access tokens
- Profile preferences
- Threads
- GitLab and SSH keys
- GitLab integrations
- Git
- GitLab.com settings
- Infrastructure as code with Terraform and GitLab
- GitLab keyboard shortcuts
- GitLab Markdown
- AsciiDoc
- GitLab Notification Emails
- GitLab Quick Actions
- Autocomplete characters
- Reserved project and group names
- Search through GitLab
- Advanced Global Search
- Advanced Syntax Search
- Time Tracking
- GitLab To-Do List
- Administrator Docs
- Reference architectures
- Reference architecture: up to 1,000 users
- Reference architecture: up to 2,000 users
- Reference architecture: up to 3,000 users
- Reference architecture: up to 5,000 users
- Reference architecture: up to 10,000 users
- Reference architecture: up to 25,000 users
- Reference architecture: up to 50,000 users
- Troubleshooting a reference architecture set up
- Working with the bundled Consul service
- Configuring PostgreSQL for scaling
- Configuring GitLab application (Rails)
- Load Balancer for multi-node GitLab
- Configuring a Monitoring node for Scaling and High Availability
- NFS
- Working with the bundled PgBouncer service
- Configuring Redis for scaling
- Configuring Sidekiq
- Admin Area settings
- Continuous Integration and Deployment Admin settings
- Custom instance-level project templates
- Diff limits administration
- Enable and disable GitLab features deployed behind feature flags
- Geo nodes Admin Area
- GitLab Pages administration
- Health Check
- Job logs
- Labels administration
- Log system
- PlantUML & GitLab
- Repository checks
- Repository storage paths
- Repository storage types
- Account and limit settings
- Service templates
- System hooks
- Changing your time zone
- Uploads administration
- Abuse reports
- Activating and deactivating users
- Audit Events
- Blocking and unblocking users
- Broadcast Messages
- Elasticsearch integration
- Gitaly
- Gitaly Cluster
- Gitaly reference
- Monitoring GitLab
- Monitoring GitLab with Prometheus
- Performance Bar
- Usage statistics
- Object Storage
- Performing Operations in GitLab
- Cleaning up stale Redis sessions
- Fast lookup of authorized SSH keys in the database
- Filesystem Performance Benchmarking
- Moving repositories managed by GitLab
- Run multiple Sidekiq processes
- Sidekiq MemoryKiller
- Switching to Puma
- Understanding Unicorn and unicorn-worker-killer
- User lookup via OpenSSH's AuthorizedPrincipalsCommand
- GitLab Package Registry administration
- GitLab Container Registry administration
- Replication (Geo)
- Geo database replication
- Geo with external PostgreSQL instances
- Geo configuration
- Using a Geo Server
- Updating the Geo nodes
- Geo with Object storage
- Docker Registry for a secondary node
- Geo for multiple nodes
- Geo security review (Q&A)
- Location-aware Git remote URL with AWS Route53
- Tuning Geo
- Removing secondary Geo nodes
- Geo data types support
- Geo Frequently Asked Questions
- Geo Troubleshooting
- Geo validation tests
- Disaster Recovery (Geo)
- Disaster recovery for planned failover
- Bring a demoted primary node back online
- Automatic background verification
- Rake tasks
- Back up and restore GitLab
- Clean up
- Namespaces
- Maintenance Rake tasks
- Geo Rake Tasks
- GitHub import
- Import bare repositories
- Integrity check Rake task
- LDAP Rake tasks
- Listing repository directories
- Praefect Rake tasks
- Project import/export administration
- Repository storage Rake tasks
- Generate sample Prometheus data
- Uploads migrate Rake tasks
- Uploads sanitize Rake tasks
- User management
- Webhooks administration
- X.509 signatures
- Server hooks
- Static objects external storage
- Updating GitLab
- GitLab release and maintenance policy
- Security
- Password Storage
- Custom password length limits
- Restrict allowed SSH key technologies and minimum length
- Rate limits
- Webhooks and insecure internal web services
- Information exclusivity
- How to reset your root password
- How to unlock a locked user from the command line
- User File Uploads
- How we manage the TLS protocol CRIME vulnerability
- User email confirmation at sign-up
- Security of running jobs
- Proxying assets
- CI/CD Environment Variables
- Contributor and Development Docs
- Contribute to GitLab
- Community members & roles
- Implement design & UI elements
- Issues workflow
- Merge requests workflow
- Code Review Guidelines
- Style guides
- GitLab Architecture Overview
- CI/CD development documentation
- Database guides
- Database Review Guidelines
- Database Review Guidelines
- Migration Style Guide
- What requires downtime?
- Understanding EXPLAIN plans
- Rake tasks for developers
- Mass inserting Rails models
- GitLab Documentation guidelines
- Documentation Style Guide
- Documentation structure and template
- Documentation process
- Documentation site architecture
- Global navigation
- GitLab Docs monthly release process
- Telemetry Guide
- Usage Ping Guide
- Snowplow Guide
- Experiment Guide
- Feature flags in development of GitLab
- Feature flags process
- Developing with feature flags
- Feature flag controls
- Document features deployed behind feature flags
- Frontend Development Guidelines
- Accessibility & Readability
- Ajax
- Architecture
- Axios
- Design Patterns
- Frontend Development Process
- DropLab
- Emojis
- Filter
- Frontend FAQ
- GraphQL
- Icons and SVG Illustrations
- InputSetter
- Performance
- Principles
- Security
- Tooling
- Vuex
- Vue
- Geo (development)
- Geo self-service framework (alpha)
- Gitaly developers guide
- GitLab development style guides
- API style guide
- Go standards and style guidelines
- GraphQL API style guide
- Guidelines for shell commands in the GitLab codebase
- HTML style guide
- JavaScript style guide
- Migration Style Guide
- Newlines style guide
- Python Development Guidelines
- SCSS style guide
- Shell scripting standards and style guidelines
- Sidekiq debugging
- Sidekiq Style Guide
- SQL Query Guidelines
- Vue.js style guide
- Instrumenting Ruby code
- Testing standards and style guidelines
- Flaky tests
- Frontend testing standards and style guidelines
- GitLab tests in the Continuous Integration (CI) context
- Review Apps
- Smoke Tests
- Testing best practices
- Testing levels
- Testing Rails migrations at GitLab
- Testing Rake tasks
- End-to-end Testing
- Beginner's guide to writing end-to-end tests
- End-to-end testing Best Practices
- Dynamic Element Validation
- Flows in GitLab QA
- Page objects in GitLab QA
- Resource class in GitLab QA
- Style guide for writing end-to-end tests
- Testing with feature flags
- Translate GitLab to your language
- Internationalization for GitLab
- Translating GitLab
- Proofread Translations
- Merging translations from CrowdIn
- Value Stream Analytics development guide
- GitLab subscription
- Activate GitLab EE with a license