0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OCI コンテナレジストリのイメージ一覧とn世代より古いのイメージ削除

Last updated at Posted at 2025-10-02

概要

OCI コンテナレジストリのリポジトリ管理の補助するShellを作成しました

  • リポジトリのイメージ一覧
  • リポジトリの古いイメージ(n世代より古い)のイメージ削除

~/.oc/configにて資格情報を指定して、oci cliをインストールした端末より実行可能です

コードサンプル

リポジトリ毎のイメージ一覧

リポジトリ名をキーにイメージ一覧JSONを取得してイメージ:タグ、作成時間およびOCIDを作成時間を降順に一覧表示します
引数は以下のとおりです
./ContainterImageList.sh コンパートメントID リポジトリ名

  • コンパートメントID: コンパートメントOCID
  • リポジトリ名: コンテナリポジトリ名
ContainterImageList.sh
#!/bin/bash

# 引数 / Arguments:
# $1 - コンパートメントID / Compartment OCID
# $2 - リポジトリ名 / Repository name
COMPARTMENT_ID=$1
REPOSITORY_NAME=$2

# イメージ一覧を取得 / Get image list
IMAGE_JSON=$(oci artifacts container image list \
            --compartment-id $COMPARTMENT_ID \
            --repository-name $REPOSITORY_NAME \
            --region "ap-tokyo-1" \
            --all \
            --output json)

# イメージIDを抽出 / Extract image IDs
echo "[display-name] [time-created]"
echo "$IMAGE_JSON" | jq -r "
  .data.items
  | sort_by(.[\"time-created\"]) | reverse
  | map(select(.\"lifecycle-state\" == \"AVAILABLE\"))
  | .[] | [.\"display-name\", .\"time-created\", .id] | @tsv
"

リポジトリの古いイメージ(n世代より古い)のイメージ削除

リポジトリ名をキーにイメージ一覧JSONを取得して作成時間が新しい順から指定した世代以降の古いイメージを削除します
引数は以下のとおりです
./ContainterImageOldDel.sh コンパートメントID リポジトリ名 世代 --dryrun

  • コンパートメントID: コンパートメントOCID
  • リポジトリ名: コンテナリポジトリ名
  • 世代: 保持する世代数
  • --dryrun: 指定した場合は削除を実施しない
ContainterImageOldDel.sh
#!/bin/bash

# dryrunフラグの初期化 / Initialize dryrun flag
DRYRUN=false

# 引数 / Arguments:
# $1 - コンパートメントID / Compartment OCID
# $2 - リポジトリ名 / Repository name
# $3 - 保持する最新イメージ数 / Number of latest images to keep
# $4 - 引数に --dryrun が含まれているかチェック / Check if --dryrun is passed as an argument
COMPARTMENT_ID=$1
REPOSITORY_NAME=$2
IMAGE_VERSION=$3

for arg in "$@"; do
  if [ "$arg" = "--dryrun" ]; then
    DRYRUN=true
  fi
done

# IMAGE_VERSIONチェック / Validate IMAGE_VERSION
if ! [[ "$IMAGE_VERSION" =~ ^[0-9]+$ ]] || [ "$IMAGE_VERSION" -lt 1 ]; then
  echo "Error: Number must be a number greater than or equal to 1"
  exit 1
fi

# イメージ一覧を取得 / Get image list
IMAGE_JSON=$(oci artifacts container image list \
            --compartment-id $COMPARTMENT_ID \
            --repository-name $REPOSITORY_NAME \
            --region "ap-tokyo-1" \
            --all \
            --output json)

# 古いイメージIDを抽出 / Extract old image IDs
DELETE_IDS=$(echo "$IMAGE_JSON" | jq -r "
  .data.items
  | sort_by(.[\"time-created\"]) | reverse
  | map(select(.\"lifecycle-state\" == \"AVAILABLE\"))
  | .[$IMAGE_VERSION:][] | [.id] | @tsv
")

# DELETE_IDS が空かチェック / Check if DELETE_IDS is empty
if [ -z "$DELETE_IDS" ]; then
  echo "Error: No image IDs found to delete. DELETE_IDS is empty."
  exit 1
fi

# イメージを削除 / Delete images
echo "$DELETE_IDS" | while read OLD_IMAGEID; do
  if [ "$DRYRUN" = false ]; then
    # 削除実行 / Perform deletion
    echo "Deleting image id: $OLD_IMAGEID"
    oci artifacts container image delete \
      --image-id "$OLD_IMAGEID" \
      --force
  else
    # 削除スキップ / Skip deletion in dry run mode
    echo "(dry run) Skipped deletion of image id: $OLD_IMAGEID"
  fi
done

実行結果

実施結果は以下のとおりです

警告
動作確認は開発用のリポジトリ等影響のない環境にて確認してください

  • イメージ一覧を表示
$ ./ContainterImageList.sh <<ocid1.compartment.oc1..aaaaaaaa>> <<repositry_name>>
[display-name] [time-created]
<<repositry_name>>:0.0.6        2025-09-30T17:12:11.331000+00:00        ocid1.containerimage.oc1....aaaaaaaa6
<<repositry_name>>:0.0.5        2025-09-30T17:10:32.678000+00:00        ocid1.containerimage.oc1....aaaaaaaa5
<<repositry_name>>:0.0.4        2025-09-30T17:08:57.720000+00:00        ocid1.containerimage.oc1....aaaaaaaa4
<<repositry_name>>:0.0.3        2025-09-30T17:06:44.014000+00:00        ocid1.containerimage.oc1....aaaaaaaa3
<<repositry_name>>:0.0.2        2025-09-30T17:03:19.077000+00:00        ocid1.containerimage.oc1....aaaaaaaa2
  • 3世代を保持して古いイメージは削除するコマンドをdryrunで検証
$ ./ContainterImageOldDel.sh <<ocid1.compartment.oc1..aaaaaaaa>> <<repositry_name>> 3 --dryrun
(dry run) Skipped deletion of image id: ocid1.containerimage.oc1....aaaaaaaa3
(dry run) Skipped deletion of image id: ocid1.containerimage.oc1....aaaaaaaa2
  • 3世代を保持して古いイメージは削除
$ ./ContainterImageOldDel.sh <<ocid1.compartment.oc1..aaaaaaaa>> <<repositry_name>> 3
Deleting image id: ocid1.containerimage.oc1....aaaaaaaa3
Deleting image id: ocid1.containerimage.oc1....aaaaaaaa2
  • イメージ一覧を表示して古いバージョンが削除されたことを確認
$ ./ContainterImageList.sh <<ocid1.compartment.oc1..aaaaaaaa>> <<repositry_name>>
[display-name] [time-created]
<<repositry_name>>:0.0.6        2025-09-30T17:12:11.331000+00:00        ocid1.containerimage.oc1....aaaaaaaa6
<<repositry_name>>:0.0.5        2025-09-30T17:10:32.678000+00:00        ocid1.containerimage.oc1....aaaaaaaa5
<<repositry_name>>:0.0.4        2025-09-30T17:08:57.720000+00:00        ocid1.containerimage.oc1....aaaaaaaa4

情報
誤って必要なイメージを削除した場合は以下のコマンドで復活できます
oci artifacts container image restore <<イメージOCID>>
ただしタグはunknown@ダイジェストになりますので注意が必要です

  • 世代を0に指定した場合はエラーになります
$ ./ContainterImageOldDel.sh <<ocid1.compartment.oc1..aaaaaaaa>> <<repositry_name>> 0 --dryrun
Error: Number must be a number greater than or equal to 1
  • イメージ世代以上の世代数を指定した場合はエラーになります
$ ./ContainterImageOldDel.sh <<ocid1.compartment.oc1..aaaaaaaa>> <<repositry_name>> 10 --dryrun
Error: No image IDs found to delete. DELETE_IDS is empty.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?