The most previous resource we have as a developer is our dev environment, which usually local computer (or maybe dev container in the cloud, such as visual studio code space). In any case, whenever you build docker image, you don't want to waste your dev environment resource, so let's offload the job to ACR (Azure Container Registry) Task!
ACR (Azure Container Registry) is Microsoft Azure hosted private docker hub which we can store and manage our container images. Of course it does not just store image, but also provides many great features to make DevOps lifecycle easier.
It builds the docker image from our source code. We can trigger the task manually on demand, as well as auto-trigger based on source code update, base image update, schedule, etc.
By using ACR Task, we don't need to build image locally, or we even don't need to have docker installed in local PC, which we all have anyway though.
- Azure Subscription
- Install Azure CLI
- Create ACR
- Install .NET 5 SDK
- Docker Engine (I use Docker Desktop)
Run following command to create ACR if you don't have any yet.
1. Login to azure.
2. Make sure to use correct subscription if you have some.
az account show
3. Set subscription if you need to use different one.
az account set --subscription <azure subscription id>
4. Create ACR. Change name and location as you need.
az group create -n acrtaskrg -l eastus az acr create -n acrtaskacr -g acrtaskrg --sku Standard
To demonstrate the feature, I will build .NET5 console app.
Let's create simple console app.
1. Create console app from template.
dotnet new console -n helloacrtask cd helloacrtask
2. Build and run the app. You should see "Hello World!" in the console.
3. Add Dockerfile in the root directory.
# https://hub.docker.com/_/microsoft-dotnet FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build WORKDIR /source # copy csproj and restore as distinct layers COPY *.csproj . RUN dotnet restore # copy and publish app and libraries COPY . . RUN dotnet publish -c release -o /app # final stage/image FROM mcr.microsoft.com/dotnet/runtime:5.0 WORKDIR /app COPY --from=build /app . ENTRYPOINT ["dotnet", "helloacrtask.dll"]
4. Build and run the docker image for test. You will see "Hello World!" again.
docker build -t acrtask . docker run --rm acrtask
Now it's time to build and publish image by using ACR task.
1. Run following command.
az acr build -t acrtaskacr.azurecr.io/acrtask:v1 -r acrtaskacr .
2. You can see the build status and log as well.
az acr task logs --registry acrtaskacr
3. Login to ACR
az acr login -n acrtaskacr
4. Run the image. You will see the image is downloaded from the ACR and "Hello World!" message.
docker run --rm acrtaskacr.azurecr.io/acrtask:v1
I only explain very first step to use ACR Task. As mentioned above, we can configure to auto-task as well with many other useful options.
See tutorial for more information!