DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

ACR Task: Don't build docker image in your local PC anymore!

Hello developers!!

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!

Reference: Automate container image builds and maintenance with ACR Tasks (official docs)

What is ACR

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.

What is ACR Task

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.

Prerequisites

Create ACR

Run following command to create ACR if you don't have any yet.

1. Login to azure.

az login
Enter fullscreen mode Exit fullscreen mode

2. Make sure to use correct subscription if you have some.

az account show
Enter fullscreen mode Exit fullscreen mode

3. Set subscription if you need to use different one.

az account set --subscription <azure subscription id>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To demonstrate the feature, I will build .NET5 console app.

Create .NET console app

Let's create simple console app.

1. Create console app from template.

dotnet new console -n helloacrtask
cd helloacrtask
Enter fullscreen mode Exit fullscreen mode

2. Build and run the app. You should see "Hello World!" in the console.

dotnet run
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

4. Build and run the docker image for test. You will see "Hello World!" again.

docker build -t acrtask .
docker run --rm acrtask
Enter fullscreen mode Exit fullscreen mode

Build image by using ACR Task

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 .
Enter fullscreen mode Exit fullscreen mode

2. You can see the build status and log as well.

az acr task logs --registry acrtaskacr
Enter fullscreen mode Exit fullscreen mode

3. Login to ACR

az acr login -n acrtaskacr
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

What's next?

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!

Top comments (1)

Collapse
 
aheisleycook profile image
privatecloudev

new