Creating a private container registry using Azure PowerShell
In this tutorial, we will learn about Azure container Registry and how to create a private container registry using Azure Powershell.
Azure Container Registry refers to a managed, private Docker container registry service for building, storing, and serving Docker container images.
Prerequisites
- Firstly, for this, you must have an Azure PowerShell module. Then, run Get-Module -ListAvailable Az for determining your installed version.
- Secondly, you must also have Docker installed locally. That is to say, the docker provides packages for OS like macOS, Windows, and Linux systems.
Sign in to Azure
For signing in to your Azure subscription with the Connect-AzAccount command:
PowerShell
Connect-AzAccount
Creating resource group
After authenticating with Azure, create a resource group with New-AzResourceGroup. However, a resource group is a logical container in which you deploy and manage your Azure resources.
PowerShell
New-AzResourceGroup -Name myResourceGroup -Location EastUS
Creating container registry
Now, we will create a container registry in a new resource group with the New-AzContainerRegistry command.
However, the registry name must be unique within Azure and should contain 5-50 alphanumeric characters. For example, we will create a registry named “myContainerRegistry007.” Replace myContainerRegistry007 in the following command, then run it to create the registry:
PowerShell
$registry = New-AzContainerRegistry -ResourceGroupName “myResourceGroup” -Name “myContainerRegistry007” -EnableAdminUser -Sku Basic
Now, in this, you create a Basic registry, that is a cost-optimized option for developers learning about Azure Container Registry.
Log in to registry
Before pushing and pulling container images, firstly, you must log in to your registry. That is to say, in production scenarios, you must use an individual identity or service principal for container registry access. However, for keeping this quickstart briefing, just enables the admin user on your registry with the Get-AzContainerRegistryCredential command:
PowerShell
$creds = Get-AzContainerRegistryCredential -Registry $registry
Next, run docker login to log in:
PowerShell
$creds.Password | docker login $registry.LoginServer -u $creds.Username –password-stdin
The command returns Login Succeeded once completed.
Pushing image to registry
For pushing an image to an Azure Container Registry, you must first have an image. However, if you don’t yet have any local container images, run the following docker pull command to pull an existing image from Docker Hub. For example, given, pull the hello-world image.
docker pull hello-world
Before pushing an image to your registry, first, tag it with the fully qualified name of your registry login server. However, the login server name is in the format <registry-name>.azurecr.io (all lowercase), for example, mycontainerregistry.azurecr.io.
Tag the image using the docker tag command. Replace <login-server> with the login server name of your ACR instance.
docker tag hello-world <login-server>/hello-world:v1
Example:
docker tag hello-world mycontainerregistry.azurecr.io/hello-world:v1
Lastly, use the docker push to push the image to the registry instance. After that, replace <login-server> with the login server name of your registry instance. For example, let’s create the hello-world repository, containing the hello-world:v1 image.
docker push <login-server>/hello-world:v1
However, after pushing the image to the container registry, remove the hello-world:v1 image from your local Docker environment.
docker rmi <login-server>/hello-world:v1
Run image from registry
Now, you can pull and run the hello-world:v1 container image from your container registry by using docker run:
docker run <login-server>/hello-world:v1
Example output:
Unable to find image ‘mycontainerregistry.azurecr.io/hello-world:v1’ locally
v1: Pulling from hello-world
Digest: sha256:662dd8e65ef7ccf13f417962c2f77567d3b132f12c95909de6c85ac3c326a345
Status: Downloaded newer image for mycontainerregistry.azurecr.io/hello-world:v1
Hello from Docker!
This message shows that your installation is working correctly.
[…]
Cleaning up resources
After finishing working with the resources you created in this quickstart, firstly, use the Remove-AzResourceGroup command for removing the resource group. Further, it also removes the container registry, and the container images stored there using the command:
PowerShell
Remove-AzResourceGroup -Name myResourceGroup
Reference: Microsoft Documentation