How to Use Azure Container Registry with Docker
To use
Azure Container Registry, first create a registry in Azure, then log in using az acr login. Build your Docker image locally, tag it with your registry's login server, and push it using docker push.Syntax
Here is the basic syntax to work with Azure Container Registry (ACR):
az acr login --name <registry-name>: Log in to your ACR.docker build -t <registry-login-server>/<image-name>:<tag> .: Build a Docker image and tag it for your registry.docker push <registry-login-server>/<image-name>:<tag>: Push the image to ACR.docker pull <registry-login-server>/<image-name>:<tag>: Pull the image from ACR.
bash
az acr login --name myRegistry
docker build -t myRegistry.azurecr.io/myapp:v1 .
docker push myRegistry.azurecr.io/myapp:v1
docker pull myRegistry.azurecr.io/myapp:v1Example
This example shows how to create a registry, log in, build a Docker image, and push it to Azure Container Registry.
bash
az group create --name myResourceGroup --location eastus
az acr create --resource-group myResourceGroup --name myRegistry --sku Basic
az acr login --name myRegistry
docker build -t myRegistry.azurecr.io/helloapp:v1 .
docker push myRegistry.azurecr.io/helloapp:v1
docker pull myRegistry.azurecr.io/helloapp:v1Output
Resource group 'myResourceGroup' created.
Registry 'myRegistry' created.
Login Succeeded
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine
---> a24bb4013296
Step 2/2 : CMD ["echo", "Hello from ACR"]
---> Running in 123abc456def
Successfully built abcdef123456
Successfully tagged myRegistry.azurecr.io/helloapp:v1
The push refers to repository [myRegistry.azurecr.io/helloapp]
...
v1: digest: sha256:abcdef123456 size: 1234
v1: Pulling from helloapp
Digest: sha256:abcdef123456
Status: Downloaded newer image for myRegistry.azurecr.io/helloapp:v1
Common Pitfalls
Common mistakes when using Azure Container Registry include:
- Not logging in to ACR before pushing or pulling images.
- Using incorrect registry login server in image tags.
- Forgetting to create the resource group or registry before use.
- Not having Docker installed or running locally.
Always verify your registry name and login server URL.
bash
docker push myapp:v1 # Error: denied: requested access to the resource is denied # Correct way: docker tag myapp:v1 myRegistry.azurecr.io/myapp:v1 docker push myRegistry.azurecr.io/myapp:v1
Quick Reference
| Command | Purpose |
|---|---|
| az acr login --name | Log in to Azure Container Registry |
| docker build -t | Build and tag Docker image for ACR |
| docker push | Push image to ACR |
| docker pull | Pull image from ACR |
| az acr create --resource-group | Create a new ACR instance |
Key Takeaways
Log in to Azure Container Registry using 'az acr login' before pushing or pulling images.
Tag Docker images with your registry's login server before pushing.
Create the Azure resource group and registry before using them.
Use correct registry URLs to avoid access denied errors.
Docker must be installed and running locally to build and push images.