Rui Jarimba
Rui Jarimba

Reputation: 18024

Create Azure container instance with VNet using C#

I have the following powershell command that creates an Azure container group with 1 container instance:

az container create \
-g $(ResourceGroup) \
--name $(ContainerName) \
--image $(DockerImage) \
--cpu 2 --memory 8 \
--restart-policy OnFailure \
--vnet $(VNet) \
--subnet $(VNetSubnet) \
--registry-username $(RepositoryUserName) \
--registry-password $(RepositoryPassword)

I'm trying to do the same using the .NET Client Libraries, based on this sample code:

var containerGroup = _azure.ContainerGroups
          .Define(agentName) 
          .WithRegion(resourceGroup.Region)
          .WithExistingResourceGroup(resourceGroup.Name)
          .WithLinux()
          .WithPrivateImageRegistry("xxx.azurecr.io", "xxx", "xxx")
          .WithoutVolume()
          .DefineContainerInstance(agentName)
              .WithImage(args.DockerImageName)
              .WithExternalTcpPort(80)
              .WithCpuCoreCount(args.CpuCoreCount)
              .WithMemorySizeInGB(args.MemorySizeInGB)
          .Attach()
          .WithTags(tags)
          .WithRestartPolicy(ContainerGroupRestartPolicy.Always)
          .CreateAsync()

But I can't find a way to set the vnet and subnet. How to do it using C#?

Upvotes: 2

Views: 716

Answers (2)

kkleeberger
kkleeberger

Reputation: 65

VNET is meanwhile supported

Add WithNetworkProfile

var containerGroup = _azure.ContainerGroups
          .Define(agentName) 
          .WithRegion(resourceGroup.Region)
          .WithExistingResourceGroup(resourceGroup.Name)
          .WithLinux()
          .WithPrivateImageRegistry("xxx.azurecr.io", "xxx", "xxx")
          .WithoutVolume()
          .DefineContainerInstance(agentName)
              .WithImage(args.DockerImageName)
              .WithExternalTcpPort(80)
              .WithCpuCoreCount(args.CpuCoreCount)
              .WithMemorySizeInGB(args.MemorySizeInGB)
          .Attach()
          .WithTags(tags)
          .WithRestartPolicy(ContainerGroupRestartPolicy.Always)
          .WithNetworkProfile(subscriptionId,resourceGroup,"aci-network-profile-vnettest-containersubnet")
          .CreateAsync()

The profile can be obtained with the e.g. Azure CLI

az network profile list

Upvotes: 0

Charles Xu
Charles Xu

Reputation: 31424

Not sure, but it seems there you cannot do that through C#. Maybe because it is just a preview version of Azure Container Instance. You can see all that you can define in The entirety of the Azure Container Instance service container group definition.

Or you can create it through Azure REST API. See Container Groups - Create Or Update and you can config the property networkProfile to make the container group in a vnet.

Upvotes: 2

Related Questions