Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Azure Container Registry creating new Azure Container Instance from C#

I have created a Azure Container Registry, and uploaded a custom ACI to the registry - no problem - everything works as intended. I have tried creating an container instance from the image using the Azure Portal, and no problems there either - however - when I want to automate things using C# with the Microsoft Azure Management Container Instance Fluent API, I run into problems, and even though I feel like I have been all over the Internet and the settings, looking for hidden obstructions, I haven't been able to find much help.

My code is as follows:

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

The above code keeps giving me the exception:

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

I have tried a couple of things;

  1. Testing the credentials with docker login - no problem.
  2. Pulling the image with docker pull mytestreg.azurecr.io/mytestimage - no problem.
  3. Swapping WithPrivateImageRegistry with WithPublicImageRegistryOnly and just using debian in WithImage - works as intented - no problem.
  4. Leaving the latest tag out of the image name - still doesn't work.

I have no idea why the credentials for the private Registry won't work - I have been copy/pasting directly from the Azure Portal to avoid typos, tried typing in manually etc.

Using Fiddler to inspect the traffic doesn't reveal anything interesting, other than the above exception message is returned directly from the Azure Management API.

What is the obvious thing that I am missing?

like image 687
Benjamin Ø. Avatar asked Mar 26 '18 18:03

Benjamin Ø.


People also ask

How do I create an instance of a container in Azure?

Create a container instance. On the Azure portal homepage, select Create a resource. Select Containers > Container Instances. On the Basics page, choose a subscription and enter the following values for Resource group, Container name, Image source, and Container image.

How do I run a container on an Azure container registry?

Deploy container with Azure CLI First get the registry's login server name by using the az acr show command. The login server name is all lowercase and similar to myregistry.azurecr.io . Execute the following az container create command to deploy a container instance.

Which command will create a new Azure container registry?

Create a container registry using the az acr create command. The registry name must be unique within Azure, and contain 5-50 alphanumeric characters.

Can you authenticate with Azure container registry from Azure container instances?

Authenticate using the service principal To launch a container in Azure Container Instances using a service principal, specify its ID for --registry-username , and its password for --registry-password . We recommend running the commands in the most recent version of the Azure Cloud Shell.


1 Answers

The answer above (ie using full azure registry server name):

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

seems to be part of the solution, but even with that change I was still seeing this error. Looking through other examples on the web (https://github.com/Azure-Samples/aci-dotnet-create-container-groups-using-private-registry/blob/master/Program.cs) containing what I needed, I changed my azure authentication from:

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

to:

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

and with THAT change, things are now working correctly.

like image 169
Marty Avatar answered Sep 25 '22 21:09

Marty