Azure Container Registryは、あらゆる種類のコンテナ展開のためにコンテナイメージやアーティファクトをプライベートレジストリに保存・管理することを可能にします。
Azure Container Registryのクライアントライブラリを使って:
- レジストリ内のイメージまたは成果物を一覧表示する
- 画像やアーティファクト、リポジトリ、タグのメタデータを取得する
- レジストリ項目の読み取り、書き込み、削除プロパティを設定する
- 画像やアーティファクト、リポジトリ、タグを削除してください
主要なリンク:
作業の開始
現在サポートされている環境
詳細はsupportポリシーをご覧ください。
注:このパッケージはサービスの制限によりブラウザ上で使用できません。この文書を参照してください。
[前提条件]
新しいコンテナレジストリを作成するには、Azure Portal、Azure PowerShell、またはAzure CLIを利用できます。 以下はAzure CLIを使った例です:
az acr create --name MyContainerRegistry --resource-group MyResourceGroup --location westus --sku Basic
@azure/container-registry パッケージをインストールする
npm を使用して JavaScript 用の Container Registry クライアント ライブラリをインストールします。
npm install @azure/container-registry
クライアントを認証する
Azure Identityライブラリは認証のための簡単なAzure Active Directoryサポートを提供します。
import { ContainerRegistryClient, KnownContainerRegistryAudience } from "@azure/container-registry";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://myregistryname.azurecr.io";
// Create a ContainerRegistryClient that will authenticate through Active Directory
const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential(), {
audience: KnownContainerRegistryAudience.AzureResourceManagerPublicCloud,
});
これらのサンプルは、ログインサーバー名とhttps://プレフィックスを含むURLのCONTAINER_REGISTRY_ENDPOINT環境変数セットがあることを前提としています。
国内クラウド
ナショナルクラウドのレジストリで認証するには、以下の設定追加が必要です:
- 認証情報オプションや
AZURE_AUTHORITY_HOST環境変数でauthorityHostを設定してください -
audienceをContainerRegistryClientOptions
import { ContainerRegistryClient, KnownContainerRegistryAudience } from "@azure/container-registry";
import { DefaultAzureCredential, AzureAuthorityHosts } from "@azure/identity";
const endpoint = "https://myregistryname.azurecr.cn";
// Create a ContainerRegistryClient that will authenticate through AAD in the China national cloud
const client = new ContainerRegistryClient(
endpoint,
new DefaultAzureCredential({ authorityHost: AzureAuthorityHosts.AzureChina }),
{
audience: KnownContainerRegistryAudience.AzureResourceManagerChina,
},
);
Azure Container RegistryでのAAD使用に関する詳細は、サービスの Authentication Overviewをご覧ください。
重要な概念
レジストリはDockerイメージとOCIアーティファクトを保存します。 イメージまたは成果物は、"マニフェスト"と "レイヤー" で構成されます。 画像のマニフェストは画像を構成する層を表し、 そのダイジェストによって一意に識別されます。 画像に「タグ付け」して人間が読み取れるエイリアスを付与することも可能です。 画像やアーティファクトには、タグが0つ または複数付けられ 、それぞれのタグが画像を一意に識別します。 同じ名前だけどタグが異なる画像のコレクションは リポジトリと呼ばれます。
詳細については「 コンテナレジストリの概念」をご覧ください。
例示
レジストリ操作
リポジトリ一覧
レジストリ内のリポジトリのコレクションを反復処理します。
import { ContainerRegistryClient, KnownContainerRegistryAudience } from "@azure/container-registry";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://myregistryname.azurecr.io";
const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential(), {
audience: KnownContainerRegistryAudience.AzureResourceManagerPublicCloud,
});
const iterator = client.listRepositoryNames();
for await (const repository of iterator) {
console.log(` repository: ${repository}`);
}
匿名アクセス可能なリストタグ
import { ContainerRegistryClient, KnownContainerRegistryAudience } from "@azure/container-registry";
const endpoint = "https://myregistryname.azurecr.io";
// Create a new ContainerRegistryClient for anonymous access
const client = new ContainerRegistryClient(endpoint, {
audience: KnownContainerRegistryAudience.AzureResourceManagerPublicCloud,
});
// Obtain a RegistryArtifact object to get access to image operations
const image = client.getArtifact("library/hello-world", "latest");
// List the set of tags on the hello_world image tagged as "latest"
const tagIterator = image.listTagProperties();
// Iterate through the image's tags, listing the tagged alias for the image
console.log(`${image.fullyQualifiedReference} has the following aliases:`);
for await (const tag of tagIterator) {
console.log(` ${tag.registryLoginServer}/${tag.repositoryName}:${tag.name}`);
}
成果物のプロパティを設定する
import { ContainerRegistryClient, KnownContainerRegistryAudience } from "@azure/container-registry";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://myregistryname.azurecr.io";
const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential(), {
audience: KnownContainerRegistryAudience.AzureResourceManagerPublicCloud,
});
const image = client.getArtifact("library/hello-world", "v1");
// Set permissions on the image's "latest" tag
await image.updateTagProperties("latest", { canWrite: false, canDelete: false });
イメージを削除する
import { ContainerRegistryClient, KnownContainerRegistryAudience } from "@azure/container-registry";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://myregistryname.azurecr.io";
const client = new ContainerRegistryClient(endpoint, new DefaultAzureCredential(), {
audience: KnownContainerRegistryAudience.AzureResourceManagerPublicCloud,
});
// Iterate through repositories
const repositoryNames = client.listRepositoryNames();
for await (const repositoryName of repositoryNames) {
const repository = client.getRepository(repositoryName);
// Obtain the images ordered from newest to oldest by passing the `order` option
const imageManifests = repository.listManifestProperties({
order: "LastUpdatedOnDescending",
});
const imagesToKeep = 3;
let imageCount = 0;
// Delete images older than the first three.
for await (const manifest of imageManifests) {
imageCount++;
if (imageCount > imagesToKeep) {
const image = repository.getArtifact(manifest.digest);
console.log(`Deleting image with digest ${manifest.digest}`);
console.log(` Deleting the following tags from the image:`);
for (const tagName of manifest.tags) {
console.log(` ${manifest.repositoryName}:${tagName}`);
image.deleteTag(tagName);
}
await image.delete();
}
}
}
ブロブおよびマニフェスト操作
画像のアップロード
import { ContainerRegistryContentClient } from "@azure/container-registry";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://myregistryname.azurecr.io";
const repository = "library/hello-world";
const client = new ContainerRegistryContentClient(
endpoint,
repository,
new DefaultAzureCredential(),
);
const config = Buffer.from("Sample config");
const { digest: configDigest, sizeInBytes: configSize } = await client.uploadBlob(config);
const layer = Buffer.from("Sample layer");
const { digest: layerDigest, sizeInBytes: layerSize } = await client.uploadBlob(layer);
const manifest = {
schemaVersion: 2,
config: {
digest: configDigest,
size: configSize,
mediaType: "application/vnd.oci.image.config.v1+json",
},
layers: [
{
digest: layerDigest,
size: layerSize,
mediaType: "application/vnd.oci.image.layer.v1.tar",
},
],
};
await client.setManifest(manifest, { tag: "demo" });
イメージのダウンロード
import {
ContainerRegistryContentClient,
KnownManifestMediaType,
OciImageManifest,
} from "@azure/container-registry";
import { DefaultAzureCredential } from "@azure/identity";
import { writeFileSync, createWriteStream } from "node:fs";
const endpoint = "https://myregistryname.azurecr.io";
const repository = "library/hello-world";
const client = new ContainerRegistryContentClient(
endpoint,
repository,
new DefaultAzureCredential(),
);
// Download the manifest to obtain the list of files in the image based on the tag
const result = await client.getManifest("demo");
if (result.mediaType !== KnownManifestMediaType.OciImageManifest) {
throw new Error("Expected an OCI image manifest");
}
const manifest = result.manifest as OciImageManifest;
// Manifests of all media types have a buffer containing their content; this can be written to a file.
writeFileSync("manifest.json", result.content);
const configResult = await client.downloadBlob(manifest.config.digest);
const configFile = createWriteStream("config.json");
configResult.content.pipe(configFile);
const trimSha = (digest: string): string => {
const index = digest.indexOf(":");
return index === -1 ? digest : digest.substring(index);
};
// Download and write out the layers
for (const layer of manifest.layers) {
const fileName = trimSha(layer.digest);
const layerStream = createWriteStream(fileName);
const downloadLayerResult = await client.downloadBlob(layer.digest);
downloadLayerResult.content.pipe(layerStream);
}
マニフェストを削除
import { ContainerRegistryContentClient } from "@azure/container-registry";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://myregistryname.azurecr.io";
const repository = "library/hello-world";
const client = new ContainerRegistryContentClient(
endpoint,
repository,
new DefaultAzureCredential(),
);
const downloadResult = await client.getManifest("latest");
await client.deleteManifest(downloadResult.digest);
BLOB を削除する
import {
ContainerRegistryContentClient,
KnownManifestMediaType,
OciImageManifest,
} from "@azure/container-registry";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://myregistryname.azurecr.io";
const repository = "library/hello-world";
const client = new ContainerRegistryContentClient(
endpoint,
repository,
new DefaultAzureCredential(),
);
const downloadResult = await client.getManifest("latest");
if (downloadResult.mediaType !== KnownManifestMediaType.OciImageManifest) {
throw new Error("Expected an OCI image manifest");
}
for (const layer of (downloadResult.manifest as OciImageManifest).layers) {
await client.deleteBlob(layer.digest);
}
Troubleshooting
トラブルシューティングについては、トラブルシューティングガイドを参照してください。
次のステップ
クライアントライブラリの使い方を示す詳細な例があるsamplesディレクトリをご覧ください。
Contributing
このライブラリに貢献したい場合は、コードのビルドやテスト方法について詳しく知るために、contributing guideをお読みください。
関連プロジェクト
Azure SDK for JavaScript