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

问题描述 投票:4回答:2

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

我的代码如下:

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();

上面的代码一直给我一个例外:

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并在debian中使用WithImage - 按意图工作 - 没问题。
  4. latest标签留在图像名称之外 - 仍然无法正常工作。

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

使用Fiddler检查流量并未发现任何有趣的内容,除了上述异常消息直接从Azure Management API返回。

我遗失的显而易见的事情是什么?

azure azure-container-registry azure-container-instances
2个回答
2
投票

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

.WithImage("mytestreg.azurecr.io/mytestimage:latest")

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

azure = Azure.Authenticate(authFilePath).WithDefaultSubscription();

至:

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

随着这种变化,事情现在正在发挥作用。


3
投票

在过去几周我遇到了同样的问题,我终于找到了解决方案。您应该在图像名称前添加azure注册服务器名称。所以按照你的例子改变:

.WithImage("mytestimage/latest")

至:

.WithImage("mytestreg.azurecr.io/mytestimage:latest")

至少那个为我做了,我希望它可以帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.