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

标签 azure docker containers

我花了几天时间研究这个问题,但找不到如何执行此操作的示例,并且 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/

相关文章:

c++ - 用一个迭代器迭代 vector 的 vector

docker - 运行短期脚本后坚持Docker容器

python - 如何通过 Flask 应用程序在 Python 中使用 GDAL 打开远程文件

ruby-on-rails - docker-compose Rails spring 不起作用

docker - 在 Windows Ubuntu WSL 上安装 Docker 时遇到问题

docker - Windows 容器导致 Azure SQL 连接失败

c++ - 从列表容器中删除一个元素

reactjs - 静态托管 - 使用 Azure CDN 的 Azure Blob 存储上的 ReactJS 应用程序

Azure ARM - 将作为参数传递的标签与文字标签结合起来

Azure 函数流量管理器