johnstaveley
johnstaveley

Reputation: 1499

How do I start a previously stopped Azure Container instance using C#?

I have the following code to stop an Azure container instance and would like to start it using similar.

using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;

 var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal("XXXX",                "XXXX", "XXXX", AzureEnvironment.AzureGlobalCloud);

        var azure = Azure
            .Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .Authenticate(credentials)
            .WithSubscription("XXXXX");

        var containerName = "mycontainer";
        var containerGroup = azure.ContainerGroups.GetByResourceGroup("myResourceGroup", containerName);
        if (containerGroup.State == "Running")
        {
            containerGroup.Stop();
        }

I would like to do the same and start my azure container instance. So where is containerGroup.Start(); ? This does not appear to exist in the interface. I have tried using containerGroup.Restart(); but this does not work from a stopped state. I need to be able to do this from within C# code and would like to avoid powershell if possible.

Upvotes: 4

Views: 1867

Answers (2)

Marnix van Valen
Marnix van Valen

Reputation: 13673

There is a way to do this but it is not exposed in the fluent API:

using Microsoft.Azure.Management.ContainerInstance.Fluent;

// azure is an instance of IAzure; the fluent Azure API
var resources = await azure.ContainerGroups.ListAsync();

foreach(var containerGroup in resources.Where(aci => aci.State != "Running"))
{
  await ContainerGroupsOperationsExtensions.StartAsync(
           containerGroup.Manager.Inner.ContainerGroups,
           containerGroup.ResourceGroupName, 
           containerGroup.Name);
}

As mentioned by other people, you do need to realize that this is effectively starting a fresh container. No state will be maintained from the previous run unless you persisted that somewhere else like in a mounted volume.

You'll also need to grant the appropriate rights to whom ever is executing this code. I'm using a function so I had to setup a service account and a role, this blog post has all the details.

Update The code I'm using is in on GitHub: https://github.com/alanta/azure_scheduler/blob/master/src/StartACIs.cs

Upvotes: 2

Charles Xu
Charles Xu

Reputation: 31454

Unfortunately, when you stop the container instances, they would be in the Terminated state and you cannot start them again.

Terminated or deleted container groups can't be updated. Once a container group has stopped (is in the Terminated state) or has been deleted, the group is deployed as new.

Even if you update the ACI, it also means the ACI would be redeployed. You can take a look at Update containers in Azure Container Instances. In addition, the Restart action also works when the container instances are in the running state.

So there is no start function in the C# SDK for you, at least now. Hope this will help you.

Update

Take a look at the event:

enter image description here

Each time when you start the container group after stop, the container group always these steps: pull the image -> create the container group -> start the container instances. So it’s clear, the container group was recreated when you start it after stop.

Upvotes: 1

Related Questions