使用 Azure 容器注册表从 C# 创建新的 Azure 容器实例

Ben*_* Ø. 5 azure azure-container-registry azure-container-instances

我创建了一个 Azure 容器注册表,并将自定义 ACI 上传到注册表 - 没问题 - 一切都按预期工作。我曾尝试使用 Azure 门户从映像创建容器实例,也没有任何问题 - 但是 - 当我想使用 C# 和 Microsoft Azure 管理容器实例 Fluent API 自动化操作时,我遇到了问题,即使我感觉自己一直在上网和设置,寻找隐藏的障碍物,一直没能找到多少帮助。

我的代码如下:

var azureCredentials = new AzureCredentials(new
ServicePrincipalLoginInformation
{
    ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
AzureEnvironment.AzureGlobalCloud);

var azure = Azure
    .Configure()
    .Authenticate(azureCredentials)
    .WithSubscription("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

IContainerGroup containerGroup = azure.ContainerGroups.Define("mytestgroup")
    .WithRegion(Region.EuropeWest)
    .WithExistingResourceGroup("mytest-rg")
    .WithLinux()
    .WithPrivateImageRegistry("mytestreg.azurecr.io", "mytestreg", "xxxxxxxxxxxxxx")
    .WithoutVolume()
    .DefineContainerInstance("mytestgroup")
    .WithImage("mytestimage/latest")
    .WithExternalTcpPort(5555)
    .WithCpuCoreCount(.5)
    .WithMemorySizeInGB(.5)
    .Attach()
    .Create();
Run Code Online (Sandbox Code Playgroud)

上面的代码不断给我异常:

Microsoft.Rest.Azure.CloudException: 'The image 'mytestimage/latest' in container group 'mytestgroup' is not accessible. Please check the image and registry credential.'

我尝试了几件事;

  1. 测试凭据docker login- 没问题。
  2. 拉图像docker pull mytestreg.azurecr.io/mytestimage- 没问题。
  3. 交换WithPrivateImageRegistry使用WithPublicImageRegistryOnly,只是用debianWithImage-作为intented作品-没问题。
  4. latest标签从图像名称中删除 - 仍然不起作用。

我不知道为什么私有注册表的凭据不起作用 - 我直接从 Azure 门户复制/粘贴以避免输入错误,尝试手动输入等。

除了直接从 Azure 管理 API 返回上述异常消息之外,使用 Fiddler 检查流量并没有发现任何有趣的信息。

我缺少的明显的东西是什么?

小智 5

过去几周我遇到了同样的问题,我终于找到了解决方案。您应该在映像名称前添加您的 azure 注册服务器名称。因此,按照您的示例更改:

.WithImage("mytestimage/latest")
Run Code Online (Sandbox Code Playgroud)

到:

.WithImage("mytestreg.azurecr.io/mytestimage:latest")
Run Code Online (Sandbox Code Playgroud)

至少对我来说是这样,我希望它可以帮助其他人。


Mar*_*rty 5

上面的答案(即使用完整的 azure 注册表服务器名称):

.WithImage("mytestreg.azurecr.io/mytestimage:latest")
Run Code Online (Sandbox Code Playgroud)

似乎是解决方案的一部分,但即使进行了更改,我仍然看到此错误。查看网络上的其他示例(https://github.com/Azure-Samples/aci-dotnet-create-container-groups-using-private-registry/blob/master/Program.cs)包含我需要的内容,我更改了我的 azure 身份验证:

azure = Azure.Authenticate(authFilePath).WithDefaultSubscription();
Run Code Online (Sandbox Code Playgroud)

到:

AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(authFilePath);
azure = Azure
    .Configure()
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .WithDefaultSubscription();
Run Code Online (Sandbox Code Playgroud)

有了这种变化,事情现在可以正常工作了。