

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 防止在 Amazon ECR 中覆寫映像標籤
<a name="image-tag-mutability"></a>

您可以在儲存庫中開啟標籤不可變性，以防止映像標籤遭到覆寫。開啟標籤不可變性後，如果您推送的映像具有已在儲存庫中的標籤，則會傳回` ImageTagAlreadyExistsException`錯誤。標籤不可變性會影響所有標籤。您無法讓某些標籤不可變，而其他標籤則不可變。

您可以使用 AWS 管理主控台 和 AWS CLI 工具來設定新儲存庫或現有儲存庫的影像標籤可變性。若要使用主控台步驟建立儲存庫，請參閱 [建立 Amazon ECR 私有儲存庫以存放映像](repository-create.md)。

## 設定影像標籤可變性 (AWS 管理主控台)
<a name="repository-edit"></a>

**設定影像標籤可變性**

1. 在 [https://console.aws.amazon.com/ecr/repositories](https://console.aws.amazon.com/ecr/repositories) 開啟 Amazon ECR 主控台。

1. 從導覽列上，選擇包含要編輯之儲存庫的區域。

1. 在導覽窗格中，選擇**私有登錄**檔下的**儲存庫**。

   如果您沒有看到**儲存庫**，請選擇**私有登錄**檔以展開選單，然後選擇**儲存庫**。

1. 在**私有儲存庫**頁面上，選擇您要設定影像標籤可變性設定的儲存庫名稱前面的選項按鈕。

1. 選擇**動作**，然後在**編輯**下選擇**儲存庫**。

1. 針對**影像標籤不可變性**，選擇下列其中一個儲存庫的標籤可變性設定。
   + **可互**斥 – 如果您想要覆寫影像標籤，請選擇此選項。這是使用透過快取提取動作的儲存庫的建議設定，確保 Amazon ECR 能夠更新快取的映像。此外，若要停用幾個可變標籤的標籤更新，請輸入標籤名稱或使用萬用字元 (\*) 來比對 **Mutable 標籤排除文字方塊中的多個類似標籤**。
   + **不可變**：如果您想要防止映像標籤遭到覆寫，而且在推送具有現有標籤的映像時，它適用於儲存庫中的所有標籤和排除項目，請選擇此選項。如果您嘗試推送具有現有標籤的映像，Amazon ECR 會傳回 `ImageTagAlreadyExistsException`。此外，若要啟用幾個不可變標籤的標籤更新，請輸入標籤名稱或使用萬用字元 (\*) 在**不可分割標籤排除文字方塊中比對多個類似的標籤**。

1. 對於 **Image scan settings** (映像掃描設定)，雖然您可以在儲存庫層級指定基本掃描的掃描設定，但最佳實務是在私有登錄檔層級指定掃描組態。在私有登錄檔層級指定掃描設定讓您能啟用增強型掃描或基本掃描，還能定義篩選條件來指定要掃描哪些儲存庫。如需詳細資訊，請參閱[掃描映像是否有 Amazon ECR 中的軟體漏洞](image-scanning.md)。

1. 對於 **Encryption settings** (加密設定)，這是一個僅能檢視的欄位，因為儲存庫建立後，就無法變更儲存庫的加密設定。

1. 選擇 **Save** (儲存) 更新儲存庫設定。

## 設定影像標籤可變性 (AWS CLI)
<a name="image-tag-mutability-cli"></a>

**建立儲存庫並設定不可變標籤**

使用以下其中一個命令來建立新的映像儲存庫，並設定不可變標籤。
+ [create-repository](https://docs.aws.amazon.com/cli/latest/reference/ecr/create-repository.html) (AWS CLI) 具有影像標籤可變性

  ```
  aws ecr create-repository --repository-name {{name}} --image-tag-mutability {{IMMUTABLE}} --region {{us-east-2}}
  ```
+ [create-repository](https://docs.aws.amazon.com/cli/latest/reference/ecr/create-repository.html) (AWS CLI) 搭配影像標籤可變性排除篩選條件

  ```
  aws ecr create-repository --repository-name {{name}} --image-tag-mutability {{IMMUTABLE_WITH_EXCLUSION}} --image-tag-mutability-exclusion-filters filterType={{WILDCARD}},filter={{filter-text}} --region {{us-east-2}}
  ```
+ 具有映像標籤可變性的 [New-ECRRepository](https://docs.aws.amazon.com/powershell/latest/reference/items/New-ECRRepository.html) (AWS Tools for Windows PowerShell)

  ```
  New-ECRRepository -RepositoryName {{name}} -ImageTagMutability {{IMMUTABLE}} -Region {{us-east-2}} -Force
  ```
+ 具有影像標籤可變性排除篩選條件[的 New-ECRRepository](https://docs.aws.amazon.com/powershell/latest/reference/items/New-ECRRepository.html) (AWS Tools for Windows PowerShell)

  ```
  New-ECRRepository -RepositoryName {{name}} -ImageTagMutability {{IMMUTABLE_WITH_EXCLUSION}} -ImageTagMutabilityExclusionFilter @{FilterType={{WILDCARD}} Filter={{filter-text}}} -Region {{us-east-2}} -Force
  ```

**更新儲存庫的映像標籤可變性設定**

使用以下其中一個命令來更新現有儲存庫的映像標籤可變性設定。
+ [ put-image-tag-mutability](https://docs.aws.amazon.com/cli/latest/reference/ecr/put-image-tag-mutability.html) (AWS CLI) 具有影像標籤可變性

  ```
  aws ecr put-image-tag-mutability --repository-name {{name}} --image-tag-mutability {{IMMUTABLE}} --region {{us-east-2}}
  ```
+ [ put-image-tag-mutability](https://docs.aws.amazon.com/cli/latest/reference/ecr/put-image-tag-mutability.html) (AWS CLI) 搭配影像標籤可變性排除篩選條件

  ```
  aws ecr put-image-tag-mutability --repository-name {{name}} --image-tag-mutability {{IMMUTABLE_WITH_EXCLUSION}} --image-tag-mutability-exclusion-filters filterType={{WILDCARD}},filter={{latest}} --region {{us-east-2}}
  ```
+ [ Write-ECRImageTagMutability](https://docs.aws.amazon.com/powershell/latest/reference/items/Write-ECRImageTagMutability.html) (AWS Tools for Windows PowerShell) 具有影像標籤可變性

  ```
  Write-ECRImageTagMutability -RepositoryName {{name}} -ImageTagMutability {{IMMUTABLE}} -Region {{us-east-2}} -Force
  ```
+ [ Write-ECRImageTagMutability](https://docs.aws.amazon.com/powershell/latest/reference/items/Write-ECRImageTagMutability.html) (AWS Tools for Windows PowerShell) 搭配影像標籤可變性排除篩選條件

  ```
  Write-ECRImageTagMutability -RepositoryName {{name}} -ImageTagMutability {{IMMUTABLE_WITH_EXCLUSION}} -ImageTagMutabilityExclusionFilter @{FilterType={{WILDCARD}} Filter={{latest}}}
  ```