4

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.

johnstaveley
  • 1,400
  • 1
  • 22
  • 46

2 Answers2

2

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

Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74
  • Hi, is there any way to change the env vars before starting the container group? The documentation seems a bit cryptic – sargeMonkey Dec 22 '20 at 06:58
  • I'm not aware of any way to do that from the management API but I can think of some alternative ways of passing data to the container group. You should probably post a new question if you want a more detailed answer. – Marnix van Valen Dec 22 '20 at 08:06
  • thanks, posted the question https://stackoverflow.com/questions/65406374/azure-container-instances-net-sdk-change-environment-variable-and-restart-t – sargeMonkey Dec 22 '20 at 19:11
1

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.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • 1
    If this were true, why can I start them from the portal? If you see the comment above @Anders indicates it will be added to the fluent SDK and already exists in the non-fluent SDK. It also exists in powershell – johnstaveley Dec 11 '18 at 07:45
  • @johnstaveley I show the test screenshot in the answer. Hope it will help you. – Charles Xu Dec 11 '18 at 14:03