Table of Contents

Push an image to a remote repository

// This example demonstrates how to push a manifest to a remote repository.

// Create a HttpClient instance to be used for making HTTP requests.
var httpClient = new HttpClient();
var mockCredentialProvider = new Mock<ICredentialProvider>();
var repo = new Repository(new RepositoryOptions()
{
    Reference = Reference.Parse("localhost:5000/test"),
    Client = new Client(httpClient, mockCredentialProvider.Object),
});

var configBytes = new byte[] { 0x01, 0x02, 0x03 }; // Example config data
var config = Descriptor.Create(
    configBytes,
    MediaType.ImageConfig
);

var layersBytes = new List<byte[]>
{
    new byte[] { 0x04, 0x05, 0x06 }, // Example layer data
    new byte[] { 0x07, 0x08, 0x09 }  // Another layer data
};
var layers = new List<Descriptor>
{
    Descriptor.Create(
        layersBytes[0],
        MediaType.ImageLayer
    ),
    Descriptor.Create(
        layersBytes[1],
        MediaType.ImageLayer
    )
};

// Push config and layers to the repository
await repo.PushAsync(config, new MemoryStream(configBytes));
for (int i = 0; i < layers.Count; i++)
{
    await repo.PushAsync(layers[i], new MemoryStream(layersBytes[i]));

}

// Create a PackManifestOptions instance to specify the manifest configuration.
var options = new PackManifestOptions
{
    Config = config,
    Layers = layers
};

// Pack and push the manifest to the repository.
var pushedDescriptor = await Packer.PackManifestAsync(
    repo,
    Packer.ManifestVersion.Version1_1,
    "",
    options);

var tag = "tag";
// Tag the pushed image.
await repo.TagAsync(pushedDescriptor, tag);