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

时间:2018-03-26 18:55:12

标签: azure azure-container-registry azure-container-instances

我创建了一个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标记从图像名称中删除 - 仍然无法正常工作。
  5. 我不知道为什么私人注册表的凭据无法正常工作 - 我直接从Azure门户复制/粘贴以避免拼写错误,尝试手动输入等。

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

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

2 个答案:

答案 0 :(得分:3)

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

.WithImage("mytestimage/latest")

收件人:

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

至少这对我有用,我希望它对其他人有帮助。

答案 1 :(得分: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();

随着这种变化,现在一切正常。