Azure Container Registry client library for JavaScript - verze 1.1.2

Azure Container Registry umožňuje ukládat a spravovat obrazy a artefakty kontejnerů v privátním registru pro všechny typy nasazení kontejnerů.

Použijte klientskou knihovnu pro Azure Container Registry kdyby:

  • Výpis imagí nebo artefaktů v registru
  • Získejte metadata pro obrázky a artefakty, repozitáře a tagy
  • Nastavení vlastností čtení, zápisu a odstranění u položek registru
  • Smažte obrázky a artefakty, úložiště a štítky

Klíčové odkazy:

Začínáme

Aktuálně podporovaná prostředí

Pro více informací viz naše support policy.

Poznámka: Tento balíček nelze v prohlížeči používat kvůli omezením služeb, pro doporučení se prosím podívejte na tento dokument pro pokyny.

Předpoklady

Pro vytvoření nového Container Registry můžete použít Azure Portal, Azure PowerShell nebo Azure CLI. Tady je příklad s použitím Azure CLI:

az acr create --name MyContainerRegistry --resource-group MyResourceGroup --location westus --sku Basic

Nainstalujte balíček @azure/container-registry.

Nainstalujte klientskou knihovnu Container Registry pro JavaScript pomocí npm:

npm install @azure/container-registry

Ověření klienta

Knihovna Azure Identity poskytuje snadnou Azure Active Directory podporu autentizace.

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,
});

Všimněte si, že tyto ukázky předpokládají, že máte nastavenou proměnnou CONTAINER_REGISTRY_ENDPOINT prostředí, což je URL obsahující název přihlašovacího serveru a prefix.https://

Národní oblaky

Pro autentizaci s registrem v National Cloud budete muset do své konfigurace provést následující úpravy:

  • Nastavte v authorityHost možnostech přihlašovacích údajů nebo pomocí AZURE_AUTHORITY_HOST proměnné prostředí
  • audience Nastavit vContainerRegistryClientOptions
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,
  },
);

Pro více informací o používání AAD s Azure Container Registry navštivte prosím přehled Authentication služby.

Klíčové koncepty

Registr uchovává obrázky Dockeru a OCI artefakty. Obrázek nebo artefakt se skládá z manifestu a vrstev. Manifest obrazu popisuje vrstvy, které obraz tvoří, a je jedinečně identifikován podle svého digestu. Obrázek lze také "označit" tak, aby získal lidsky čitelný alias. Obrázek nebo artefakt může mít nula nebo více značek přiřazených a každý štítek jej jednoznačně identifikuje. Kolekce obrázků, které mají stejný název, ale odlišné štítky, se nazývá repozitář.

Pro více informací viz Koncepty kontejnerového registru.

Příklady

Operace registru

Seznam repozitářů

Procházejte kolekci úložišť v registru.

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}`);
}

Seznamové štítky s anonymním přístupem

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}`);
}

Nastavení vlastností artefaktů

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 });

Smazat obrázky

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();
    }
  }
}

Operace s blobem a manifestem

Odeslání obrázků

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" });

Stáhnout obrázky

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);
}

Smazat manifest

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);

Odstranění objektu 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

Pro informace o řešení problémů se podívejte do trouble guide.

Další kroky

Podívejte se prosím do adresáře samples pro podrobné příklady, které ukazují, jak používat klientské knihovny.

Contributing

Pokud byste chtěli přispět do této knihovny, přečtěte si prosím průvodce přispívání kde se dozvíte více o tom, jak kód sestavit a testovat.