gpt4 book ai didi

azure - 如何在已部署的 Azure Docker Windows 容器实例中执行命令

转载 作者:行者123 更新时间:2023-12-03 06:41:37 24 4
gpt4 key购买 nike

我花了几天时间研究这个问题,但找不到如何执行此操作的示例,并且 Azure.ResoureManager 的文档提供了很少的见解。给定一个正在运行的容器实例(部署的 Docker Windows 容器)和最新的 Azure SDK for .NET (Azure.ResourceManager...),如何在正在运行的容器中执行命令?该命令的格式为“c:\folder\dothis.exe blobURL”。

虽然已弃用,但我尝试使用 Microsoft.Azure.Management Fluent 库未成功,如下面的代码所示,顺便说一下,它不起作用!也许它可以在 Linux 容器上运行,但不能在 Windows 容器上运行。请参阅下面的代码:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.ContainerInstance.Fluent;
using Microsoft.Azure.Management.ContainerInstance.Fluent.Models;

class Program
{
static void Main(string[] args)
{
string resourceGroupName = "HContainerGroup";
string containerGroupName = "hcontainer";
//string containerImage = "hcontaineregistry.azurecr.io/horoscope-container-img:v5";
// Set the AZURE_AUTH_LOCATION environment variable with the full
// path to an auth file. Create an auth file with the Azure CLI:
// az ad sp create-for-rbac --sdk-auth > my.azureauth
string authFilePath = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION", EnvironmentVariableTarget.User);
// Authenticate(RestClient restClient, string tenantId)
// Authenticate with Azure
ISubscription sub;
IAzure azure = Azure.Authenticate(authFilePath).WithDefaultSubscription(); //.WithSubscription("e94537de-4c00-4abc-8df3-0ec30cbdf3a8"); //.WithDefaultSubscription();
sub = azure.GetCurrentSubscription();

IResourceGroup resGroup = azure.ResourceGroups.GetByName(resourceGroupName);
Region azureRegion = resGroup.Region;
Microsoft.Azure.Management.ContainerInstance.Fluent.IContainerGroup containerGroup = azure.ContainerGroups.GetByResourceGroup(resourceGroupName, "hcontainer");
// This all works fine up to here. containerGroup has all the right stuff, so the ExecuteCommand function below should work great !!!!!!!!!!!
string command = @"c:\GetHCmdLine\GetHCmdLine.exe testblob1";
IContainerExecResponse containerExecResponse = containerGroup.ExecuteCommand("hcontainer", command, 120, 120); // !!! Does not work !!!
string log = containerGroup.GetLogContent("hcontainer", 2);
}
}

请注意,我可以使用 Azure CLI 和以下命令在容器中成功执行命令:

az container exec -g HContainerGroup --name hcontainer --exec-command "c:\GetHCmdLine\GetHCmdLine.exe testblob1"

我尝试使用新的 Azure.ResourceManager 包来替换 Fluent 的东西来尝试向正在运行的容器发送命令,但我不知道如何使用它们。

最佳答案

通过 C#,您可以对 Azure API 进行 REST 调用。

https://management.azure.com/subscriptions/subid/resourceGroups/demo/providers/Microsoft.ContainerInstance/containerGroups/demo1/containers/container1/exec?api-version=2022-09-01

{
"command": "/bin/bash",
"terminalSize": {
"rows": 12,
"cols": 12
}
}

文档:https://learn.microsoft.com/en-us/rest/api/container-instances/containers/execute-command?tabs=HTTP

关于azure - 如何在已部署的 Azure Docker Windows 容器实例中执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74466464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com