刪除 Azure Container Registry 中的容器映像
若要維護 Azure Container Registry 的大小,您應該定期刪除過時的映像資料。 雖然有些部署至生產環境的容器映像可能需要較長時間的儲存,但其他容器映像通常可以更快速地刪除。 例如,在自動化建置和測試案例中,您的登錄可以快速地填入可能永遠不會部署的映像,並可在完成建置和測試行程後立刻清除。
您可以用數種不同的方式刪除映像資料,因此務必了解每項刪除作業對於儲存體使用量有何影響。 本文說明幾種刪除映像資料的方法:
- 刪除存放庫:刪除存放庫內的所有映像和所有唯一層次。
- 依標記刪除:刪除映像、標記、映像參考的所有唯一層次,以及與映像相關聯的所有其他標記。
- 刪除資訊清單摘要:刪除映像、映像參考的所有唯一層次,以及與映像相關聯的所有標記。
如需這些概念的簡介,請參閱關於登錄、存放庫和映像。
注意
刪除映像資料之後,Azure Container Registry 會立即停止對相關聯的儲存體計費。 不過,登錄會使用非同步流程復原相關聯的儲存空間。 登錄清理各層並顯示更新的儲存體使用量之前需要一些時間。
刪除存放庫
刪除存放庫即可刪除存放庫中的所有映像,包括所有標籤、唯一層次及資訊清單。 刪除存放庫時,會復原參考該存放庫中唯一層的映像所使用的儲存空間。
下列 Azure CLI 命令可刪除 "acr-helloworld" 存放庫以及該存放庫內的所有標記和資訊清單。 如果已刪除資訊清單所參考的層次並未由登錄中任何其他映像所參考,則也會刪除其層次資料,並復原儲存空間。
az acr repository delete --name myregistry --repository acr-helloworld
依標記刪除
您可以在刪除作業中指定存放庫名稱和標記,以從存放庫中刪除個別映像。 當您依標記刪除時,會復原映像中任何唯一層次 (並未由登錄中任何其他映像共用的層次) 所使用的儲存空間。
若要依標記刪除,請使用 az acr repository delete 並在 --image
參數中指定映像名稱。 映像獨有的所有層次,與該映像相關聯的任何其他標記都會遭到刪除。
例如,從 "myregistry" 登錄中刪除 "acr-helloworld:latest" 映像:
az acr repository delete --name myregistry --image acr-helloworld:latest
This operation will delete the manifest 'sha256:0a2e01852872580b2c2fea9380ff8d7b637d3928783c55beb3f21a6e58d5d108' and all the following images: 'acr-helloworld:latest', 'acr-helloworld:v3'.
Are you sure you want to continue? (y/n):
提示
「依標記」刪除不應該與刪除標記 (取消標記) 混淆。 您可以使用 Azure CLI 命令 az acr repository untag 來刪除標記。 取消標記映像時,系統不會釋出任何空間,因為其資訊清單和層資料仍保留在登錄中。 只有標記參考本身會被刪除。
依資訊清單摘要刪除
資訊清單摘要可以與一個、零個或多個標記產生關聯。 當您依摘要刪除時,由資訊清單參考的所有標記都會遭到刪除,因為任何層次的層次資料對映像而言都是唯一的。 共用的層次資料不會遭到刪除。
若要依摘要刪除,首先針對包含所要刪除映像的存放庫列出資訊清單摘要。 例如:
az acr manifest list-metadata --name acr-helloworld --registry myregistry
[
{
"digest": "sha256:0a2e01852872580b2c2fea9380ff8d7b637d3928783c55beb3f21a6e58d5d108",
"tags": [
"latest",
"v3"
],
"timestamp": "2018-07-12T15:52:00.2075864Z"
},
{
"digest": "sha256:3168a21b98836dda7eb7a846b3d735286e09a32b0aa2401773da518e7eba3b57",
"tags": [
"v2"
],
"timestamp": "2018-07-12T15:50:53.5372468Z"
}
]
接下來,在 az acr repository delete 命令中指定您要刪除的摘要。 此命令的格式為:
az acr repository delete --name <acrName> --image <repositoryName>@<digest>
例如,若要刪除前面輸出中所列的最後一個資訊清單 (含標記 "v2"):
az acr repository delete --name myregistry --image acr-helloworld@sha256:3168a21b98836dda7eb7a846b3d735286e09a32b0aa2401773da518e7eba3b57
This operation will delete the manifest 'sha256:3168a21b98836dda7eb7a846b3d735286e09a32b0aa2401773da518e7eba3b57' and all the following images: 'acr-helloworld:v2'.
Are you sure you want to continue? (y/n):
acr-helloworld:v2
映像會從登錄中刪除,因為任何層資料對該映像而言都是唯一的。 如果資訊清單與多個標記相關聯,也會一併刪除所有相關聯的標記。
依時間戳記刪除摘要
若要保持存放庫或登錄的大小,您可能需要定期刪除早於特定日期的資訊清單摘要。
下列 Azure CLI 命令會以遞增順序列出存放庫中早於指定時間戳記的所有資訊清單摘要。 以適合您環境的值取代 <acrName>
和 <repositoryName>
。 時間戳記可以是完整的日期時間運算式或日期,如本範例所示。
az acr manifest list-metadata --name <repositoryName> --registry <acrName> \
--orderby time_asc -o tsv --query "[?lastUpdateTime < '2019-04-05'].[digest, lastUpdateTime]"
找出過時的資訊清單摘要之後,您可以執行下列 Bash 指令碼來刪除早於指定時間戳記的資訊清單摘要。 它需要 Azure CLI 和 xargs。 根據預設,此指令碼不會執行任何刪除。 將 ENABLE_DELETE
值變更為 true
,以啟用映像刪除。
警告
請謹慎使用下列範例指令碼;刪除的映像資料無法復原。 如果您有系統是依照資訊清單摘要 (而不是映像名稱) 提取映像,則不應該執行這些指令碼。 刪除資訊清單摘要會導致這些系統無法從登錄中提取映像。 請考慮採用唯一標記配置 (這是建議的最佳做法),而不是依照資訊清單提取。
#!/bin/bash
# WARNING! This script deletes data!
# Run only if you do not have systems
# that pull images via manifest digest.
# Change to 'true' to enable image delete
ENABLE_DELETE=false
# Modify for your environment
# TIMESTAMP can be a date-time string such as 2019-03-15T17:55:00.
REGISTRY=myregistry
REPOSITORY=myrepository
TIMESTAMP=2019-04-05
# Delete all images older than specified timestamp.
if [ "$ENABLE_DELETE" = true ]
then
az acr manifest list-metadata --name $REPOSITORY --registry $REGISTRY \
--orderby time_asc --query "[?lastUpdateTime < '$TIMESTAMP'].digest" -o tsv \
| xargs -I% az acr repository delete --name $REGISTRY --image $REPOSITORY@% --yes
else
echo "No data deleted."
echo "Set ENABLE_DELETE=true to enable deletion of these images in $REPOSITORY:"
az acr manifest list-metadata --name $REPOSITORY --registry $REGISTRY \
--orderby time_asc --query "[?lastUpdateTime < '$TIMESTAMP'].[digest, lastUpdateTime]" -o tsv
fi
刪除已取消標記的映像
如資訊清單摘要一節所述,使用現有標記推送已修改的映像會取消標記先前推送的映像,因而產生孤立 (或「懸盪」) 的映像。 先前推送映像的資訊清單以及其層次資料都會保留在登錄中。 考量以下事件順序:
推送具有 latest 標記的映像 acr-helloworld:
docker push myregistry.azurecr.io/acr-helloworld:latest
檢查 acr-helloworld 存放庫的資訊清單:
az acr manifest list-metadata --name acr-helloworld --registry myregistry
[ { "digest": "sha256:d2bdc0c22d78cde155f53b4092111d7e13fe28ebf87a945f94b19c248000ceec", "tags": [ "latest" ], "timestamp": "2018-07-11T21:32:21.1400513Z" } ]
修改 acr-helloworld Dockerfile
推送具有 latest 標記的映像 acr-helloworld:
docker push myregistry.azurecr.io/acr-helloworld:latest
檢查 acr-helloworld 存放庫的資訊清單:
az acr manifest list-metadata --name acr-helloworld --registry myregistry
[ { "architecture": "amd64", "changeableAttributes": { "deleteEnabled": true, "listEnabled": true, "quarantineDetails": "{\"state\":\"Scan Passed\",\"link\":\"https://aka.ms/test\",\"scanner\":\"Azure Security Monitoring-Qualys Scanner\",\"result\":{\"version\":\"2020-05-13T00:23:31.954Z\",\"summary\":[{\"severity\":\"High\",\"count\":2},{\"severity\":\"Medium\",\"count\":0},{\"severity\":\"Low\",\"count\":0}]}}", "quarantineState": "Passed", "readEnabled": true, "writeEnabled": true }, "configMediaType": "application/vnd.docker.container.image.v1+json", "createdTime": "2020-05-16T04:25:14.3112885Z", "digest": "sha256:eef2ef471f9f9d01fd2ed81bd2492ddcbc0f281b0a6e4edb700fbf9025448388", "imageSize": 22906605, "lastUpdateTime": "2020-05-16T04:25:14.3112885Z", "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "os": "linux", "timestamp": "2020-05-16T04:25:14.3112885Z" } ]
當影像未標記時,標記陣列會從元資料中移除。 此資訊清單以及它所參考的任何唯一層次資料,仍存在於登錄中。 若要刪除這類孤立映像及其層次資料,您必須依資訊清單摘要刪除。
自動清除標記和資訊清單
Azure Container Registry 提供下列自動化方法來移除標記和資訊清單,以及其相關聯的唯一層資料:
建立執行
acr purge
容器命令的 ACR 工作,以刪除早於特定持續時間或符合指定名稱篩選條件的所有標記。 選擇性設定acr purge
以刪除未標記的資訊清單。acr purge
容器命令目前處於預覽狀態。 如需詳細資訊,請參閱自動清除 Azure Container Registry 中的映像。選擇性為每個登錄設定保留原則,以管理未標記的資訊清單。 啟用保留原則時,登錄中沒有任何相關標記和基礎層資料的映像資訊清單,會在設定期間後自動刪除。
保留原則目前是進階容器登錄的預覽功能。 保留原則僅適用於原則生效之後所建立的未標記資訊清單。
下一步
如需 Azure Container Registry 中映像儲存體的詳細資訊,請參閱 Azure Container Registry 中的容器映像儲存體。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應