ACR Task: Don't build docker image in your local PC anymore!
Tags: azure, acr, docker
Published: 2021-02-19 06:18:08

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)](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-tasks-overview) 

# 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

- Azure Subscription
- [Install Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
- Create ACR
- [Install .NET 5 SDK](https://dotnet.microsoft.com/download/dotnet/5.0)
- Docker Engine (I use Docker Desktop)

## Create ACR

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

1\. Login to azure.

```shell
az login
```

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

```shell
az account show
```

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

```shell
az account set --subscription <azure subscription id>
```

4\. Create ACR. Change name and location as you need.

```shell
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.

# Create .NET console app

Let's create simple console app.

1\. Create console app from template.

```shell
dotnet new console -n helloacrtask
cd helloacrtask
```

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

```shell
dotnet run
```

3\. Add Dockerfile in the root directory.

```Dockerfile
# 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.

```shell
docker build -t acrtask .
docker run --rm acrtask
```

# Build image by using ACR Task

Now it's time to build and publish image by using ACR task. 

1\. Run following command.

```shell
az acr build -t acrtaskacr.azurecr.io/acrtask:v1 -r acrtaskacr .
```

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

```shell
az acr task logs --registry acrtaskacr
```

3\. Login to ACR

```shell
az acr login -n acrtaskacr
```

4\. Run the image. You will see the image is downloaded from the ACR and "Hello World!" message.

```shell
docker run --rm acrtaskacr.azurecr.io/acrtask:v1
```
# 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](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-tutorial-quick-task) for more information!