drg
drg

Reputation: 83

How To Set Azure Container Instance Restart Policy With .Net Fluent SDK

How do I configure the restart policy when creating an Azure Container Instance using the .Net Fluent Management SDK? Am I missing something obvious or has is the SDK lagging behind the feature?

                _azure.ContainerGroups.Define(containerGroupName)
                    .WithRegion(Region.USEast)
                    .WithExistingResourceGroup("my-resource-group")
                    .WithLinux()
                    .WithPrivateImageRegistry("myreg.azurecr.io", "registry", "XXXXXXXXXXXXX")
                    .WithoutVolume()
                    .DefineContainerInstance(containerGroupName)
                        .WithImage("my-image/tag")
                        .WithoutPorts()
                        .WithCpuCoreCount(4)
                        .WithMemorySizeInGB(0.5)
                        .WithEnvironmentVariable("containerGroup", containerGroupName)
                        .Attach()
                    .Create();

Thank You

Upvotes: 2

Views: 1573

Answers (2)

Irwin
Irwin

Reputation: 12829

As per the documentation, set restart policy when you are starting up the container. In my example below, I set the policy to "Never", .WithRestartPolicy(ContainerGroupRestartPolicy.Never)

_azure.ContainerGroups.Define(containerGroupName)
                    .WithRegion(Region.USEast)
                    .WithExistingResourceGroup("my-resource-group")
                    .WithLinux()
                    .WithPrivateImageRegistry("myreg.azurecr.io", "registry", "XXXXXXXXXXXXX")
                    .WithoutVolume()
                    .DefineContainerInstance(containerGroupName)
                        .WithImage("my-image/tag")
                        .WithoutPorts()
                        .WithCpuCoreCount(4)
                        .WithMemorySizeInGB(0.5)
                        .WithEnvironmentVariable("containerGroup", containerGroupName)
                .WithRestartPolicy(ContainerGroupRestartPolicy.Never)

                        .Attach()
                    .Create();

From the docs:

How you specify a restart policy depends on how you create your container instances, such as with the Azure CLI, Azure PowerShell cmdlets, or in the Azure portal. In the Azure CLI, specify the --restart-policy parameter when you call az container create.

Upvotes: 1

Tom Sun
Tom Sun

Reputation: 24549

How do I configure the restart policy when creating an Azure Container Instance using the .Net Fluent Management SDK?

According to the Fluent SDK I find that we could set restart policy with following code

containerGroup.Inner.RestartPolicy = ContainerRestartPolicy.Always;

Demo code:

var containerGroup = _azure.ContainerGroups.Define(containerGroupName)
                .WithRegion(Region.USEast)
                .WithExistingResourceGroup("my-resource-group")
                .WithLinux()
                .WithPrivateImageRegistry("myreg.azurecr.io", "registry", "XXXXXXXXXXXXX")
                .WithoutVolume()
                .DefineContainerInstance(containerGroupName)
                .WithImage("my-image/tag")
                .WithoutPorts()
                .WithCpuCoreCount(4)
                .WithMemorySizeInGB(0.5)
                .WithEnvironmentVariable("containerGroup", containerGroupName)
                .Attach()
                .Create();

 containerGroup.Inner.RestartPolicy = ContainerRestartPolicy.Always;

Note: I find that the ContainerRestartPolicy just has one default value always, I don't test it on my side. I am not sure whether other value Never and OnFailure work

If ContainerInstanceManagementClient is possible, we could create Azure Container Instance with ContainerInstanceManagementClient

ContainerInstanceManagementClient client = new ContainerInstanceManagementClient(new TokenCredentials(token));

  var containerGroup = new ContainerGroupInner
            {
                Location = "xx",
                Containers = new List<Container>(),
                ImageRegistryCredentials = new List<ImageRegistryCredential>(),
                RestartPolicy = "xxx"

            };

var groupInner = client.ContainerGroups.CreateOrUpdateAsync("resourceGroup", "containerGroupName", containerGroupInner).Result;

Upvotes: 1

Related Questions