Rajat Sharma
Rajat Sharma

Reputation: 57

Create new container group in Azure Container Instances using .net

I want to create multiple container groups in an Azure container instance. Following code snippet creates only one group overwrites previous but I want to create new every time

var containerGroup = azure.ContainerGroups.Define(containerGroupName)
                .WithRegion(azureRegion)
                .WithExistingResourceGroup(resourceGroupName)
                .WithLinux()
                .WithPublicImageRegistryOnly()
                .WithoutVolume()
                .DefineContainerInstance(containerGroupName + inc.ToString())
                    .WithImage(containerImage)
                    .WithExternalTcpPort(80)
                    .WithCpuCoreCount(1.0)
                    .WithMemorySizeInGB(1)
                    .Attach()
                .WithDnsPrefix(containerGroupName)
                .Create();

In javascript aci apis I can see they have CreateOrUpdate method. How can I do it in .net.

Upvotes: 1

Views: 748

Answers (2)

granadaCoder
granadaCoder

Reputation: 27872

as per the documentation, the "name" must be distinct:

ContainerGroups.Define(containerGroupName)

You need to alter the value of "containerGroupName"

https://learn.microsoft.com/en-us/dotnet/api/overview/azure/containerinstance?view=azure-dotnet

you could check for existence of the name...and add artificial suffix for "next" one if you'd like.

as per the other answer, do you want a single containerGROUP with multiple containers

 // Create the container group
    var containerGroup = azure.ContainerGroups.Define(containerGroupName)
        .WithRegion(azureRegion)
        .WithExistingResourceGroup(resourceGroupName)
        .WithLinux()
        .WithPublicImageRegistryOnly()
        .WithoutVolume()
        .DefineContainerInstance(containerGroupName + "-1")
            .WithImage(containerImage1)
            .WithExternalTcpPort(80)
            .WithCpuCoreCount(0.5)
            .WithMemorySizeInGB(1)
            .Attach()
        .DefineContainerInstance(containerGroupName + "-2")
            .WithImage(containerImage2)
            .WithoutPorts()
            .WithCpuCoreCount(0.5)
            .WithMemorySizeInGB(1)
            .Attach()
        .WithDnsPrefix(containerGroupName)
        .Create();

?

Upvotes: 1

Charles Xu
Charles Xu

Reputation: 31424

I'm not sure what you really want. I assume that you know how to create a container group with multi-containers. So if you want to create multiple container groups with one container instance you should make the container group names unique, it means if you want to create two groups, there must be two different names.

For update the container groups:

Update the containers in a container group by redeploying an existing group with at least one modified property. When you update a container group, all running containers in the group are restarted in-place.

There also the limitation of the update. For more details, see Update containers in Azure Container Instances.

Upvotes: 0

Related Questions