问题
I have deployed a container group with just one container in it, with the restart policy as Never. I want to run this container again, but with different environment variables. I see ways to do this in powershell and azure cmdlets, but the Azure .net SDK is not very clear on how this can be achieved.
Does anyone have any pointers on how this can be achieved with the Fluent API? This will be useful for me, since the image need not be pulled again, just the container instance restarted.
回答1:
I'm not sure you will be able to prevent the image from being pulled again on container start. I often see ACIs pull in the image again on restart, regardless of whether the same exact image was pulled before. I guess that's what serverless means in the real ;)
From your question it's not entirely clear what you are trying to achieve and whether restarting the container with environment vars is the best way to do this.
So without knowing the specifics, here's a couple of ideas:
If you're running the container occasionally and just want to give it some instructions, you could consider redeploying the container from a parameterized ARM template. You can definitely pass in environment variables through the ARM template.
You could mount a file share from an Azure Storage account into the container and run a script that loads the vars from there. You'd upload the data to process to the file share and start the container.
I've used this to run arbitrary (.NET) executables in a generic container, for example for testing purposes.
For more frequent runs, you could stick the configuration data in a database, a queue or a blob and process that from the container. Blobs and queues can easily be tied to Azure Functions so you could have a function that starts your container automatically when there's work to be processed.
From you're question though I'm sort of guessing you're looking for some .NET code to pull this off. There seems to be some support for that in the management SDK but I've been unable to get that to work so far:
using System.Threading.Tasks;
using Microsoft.Azure.Management.ContainerInstance.Fluent;
using Microsoft.Azure.Management.ContainerInstance.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
// ...
public static async Task SetEnvAndStart(string varName, string customValue)
{
// connect to Azure
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
Configuration.ClientId(),
Configuration.ClientSecret(),
Configuration.TenantId(),
AzureEnvironment.AzureGlobalCloud);
var azure = await Azure.Authenticate(credentials).WithDefaultSubscriptionAsync();
// Find your container group
var containerGroup = await azure.ContainerGroups.GetByIdAsync("<resource id>");
// Add the environment variable
containerGroup.Containers["mycontainer"].EnvironmentVariables.Add(new EnvironmentVariable(varName, customValue));
// Apply the changes
await containerGroup.Update().ApplyAsync();
// Restart the container
await ContainerGroupsOperationsExtensions.StartAsync(containerGroup.Manager.Inner.ContainerGroups, containerGroup.ResourceGroupName, containerGroup.Name);
}
If you really want to go with environment vars, I think your best bet is to re-create the container. You may find some of the example code for the Azure Management SDK helpful.
回答2:
When you want to run this container again, it means you need to update the configuration of this container group, even if you only want to change the environment variables. Get more details about Update containers. For Azure Container Instance, the update means to create again. For .NET, you can use the IWithEnvironmentVariables in the Microsoft.Azure.Management.ContainerInstance.Fluent.ContainerGroup.Definition to set the environment variables and then create the container group again.
来源:https://stackoverflow.com/questions/65406374/azure-container-instances-net-sdk-change-environment-variable-and-restart-t