The first time someone explained Docker to me, they said: "It is like shipping your laptop to production, but only the parts that matter." I laughed, but it stuck with me.

Our Todo API currently runs on App Service with the .NET runtime that Azure provides. That works great. But sometimes you want full control over exactly what runs: the OS packages, the .NET version, everything. That is where containers come in.

In Part 6 we sorted out configuration and secrets. Today we:

  • Write a Dockerfile for the Todo API
  • Build the image in Azure Pipelines
  • Push it to Azure Container Registry (ACR)
  • Run the container on App Service

From code to container: Dockerfile, pipeline, ACR, App Service

What you need to know before the code

  1. A Dockerfile is a recipe. Docker follows it and produces an image: your app plus everything it needs to run.
  2. A registry (we use Azure Container Registry) is a private storage place for images, like a private Docker Hub.
  3. App Service can pull an image from the registry and run it. Our pipeline just keeps the registry up to date.

That is the whole idea. Everything below is just these three steps in practice.

Step 1: Write the Dockerfile

Create a file called Dockerfile in the root of the repo:

# Build stage: has the full .NET SDK FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build WORKDIR /src # Copy only the project file first, so restore is cached COPY src/TodoApi.Api/TodoApi.Api.csproj src/TodoApi.Api/ RUN dotnet restore src/TodoApi.Api/TodoApi.Api.csproj # Now copy everything else and publish COPY . . RUN dotnet publish src/TodoApi.Api/TodoApi.Api.csproj -c Release -o /app/publish --no-restore # Runtime stage: much smaller, no SDK FROM mcr.microsoft.com/dotnet/aspnet:10.0 WORKDIR /app COPY --from=build /app/publish . EXPOSE 8080 ENTRYPOINT ["dotnet", "TodoApi.Api.dll"] 

This is called a multi-stage build. The first stage has the big SDK to compile the app. The second stage only has the runtime and our published files. The final image is a lot smaller, and smaller images start faster and have fewer things that can go wrong.

Two details that confused me at first:

  • Since .NET 8, the official ASP.NET Core images listen on port 8080, not 80. That is why we expose 8080.
  • They also run as a non-root user by default, which is a nice security win you get for free.

Also add a .dockerignore file so Docker does not copy junk into the image:

**/bin/ **/obj/ .git/ .vs/ 

Step 2: Try it on your machine

If you have Docker Desktop installed:

docker build -t todoapi:local . docker run --rm -p 8080:8080 todoapi:local 

Open http://localhost:8080/todos and you should see [].

Want to see what is actually inside your image? Run docker inspect todoapi:local. It prints a long block of JSON with the environment variables, the exposed ports, the user and the entry point. I usually scroll straight to the Config section, where you can see the port, the user and the entry point at a glance. It is a great way to understand what Docker built for you.

Microsoft has a full guide on containers for .NET if you want to go deeper: Containerize a .NET app.

Step 3: Create an Azure Container Registry

ACR is a private Docker Hub that lives in your Azure subscription. In the Azure Cloud Shell:

az acr create --name acrtodoapiyourname --resource-group rg-todoapi --sku Basic 

The name must be unique worldwide, lowercase letters and numbers only.

Heads-up: unlike the F1 App Service plan, the Basic registry is not free. It costs a few dollars a month. We will delete everything at the end of the series in Part 8, so do not worry too much.

Step 4: Connect Azure DevOps to the registry

  1. Go to Project settings > Service connections > New service connection.
  2. Choose Docker Registry, then Azure Container Registry.
  3. Pick your subscription and the acrtodoapiyourname registry.
  4. Name it acr-todoapi and save.

Step 5: Build and push the image in the pipeline

Add a new stage after Build in your azure-pipelines.yml:

 - stage: Container dependsOn: Build jobs: - job: BuildAndPush pool: vmImage: ubuntu-latest steps: - task: Docker@2 displayName: Build and push image inputs: command: buildAndPush containerRegistry: acr-todoapi repository: todoapi dockerfile: Dockerfile tags: | $(Build.BuildId) latest 

Every run now pushes two tags: one with the build number, and latest. The build number tag is the important one. It means you can always deploy, or roll back to, an exact version. The task reference is here: Docker@2 task.

Run the pipeline once. Then in the Azure portal, open your registry and go to Repositories > todoapi. Your image is there.

Step 6: Create a web app that runs the container

This step has a few commands, so let us take them one at a time. Run them in the Azure Cloud Shell.

6a. Create the web app

az webapp create --name todoapi-yourname-container --resource-group rg-todoapi --plan plan-todoapi \ --container-image-name acrtodoapiyourname.azurecr.io/todoapi:latest \ --assign-identity [system] --acr-use-identity true --acr-identity [system] 

In plain words: "Create a web app that runs our todoapi image, give the app its own identity, and use that identity to log in to the registry." No passwords anywhere, Azure handles it for us.

6b. Allow the web app to pull from the registry

The web app has an identity now, but the registry does not trust it yet. These three lines fix that:

# 1. Get the web app's identity principalId=$(az webapp identity show --name todoapi-yourname-container --resource-group rg-todoapi --query principalId -o tsv) # 2. Get the registry's ID acrId=$(az acr show --name acrtodoapiyourname --query id -o tsv) # 3. Give the web app permission to pull images (the "AcrPull" role) az role assignment create --assignee $principalId --role AcrPull --scope $acrId 

Think of it like giving a new colleague a key card. Step 1 finds the colleague, step 2 finds the door, step 3 hands over the card.

6c. Tell App Service which port to use

az webapp config appsettings set --name todoapi-yourname-container --resource-group rg-todoapi --settings WEBSITES_PORT=8080 az webapp restart --name todoapi-yourname-container --resource-group rg-todoapi 

Remember, our container listens on port 8080. App Service needs to know that, then we restart the app so it picks up everything.

If your free F1 plan gives you trouble running containers, you can scale the plan up to B1 for this lesson and scale it back down afterwards. Just remember that B1 is not free.

Step 7: Deploy the new image from the pipeline

Add one more stage:

 - stage: DeployContainer dependsOn: Container jobs: - deployment: DeployContainer pool: vmImage: ubuntu-latest environment: todoapi-dev strategy: runOnce: deploy: steps: - task: AzureWebAppContainer@1 inputs: azureSubscription: azure-todoapi appName: todoapi-yourname-container containers: acrtodoapiyourname.azurecr.io/todoapi:$(Build.BuildId) 

Notice we deploy the build number tag, not latest. That way you always know exactly which version is running. Task details are here: AzureWebAppContainer@1.

Push, merge, and open https://todoapi-yourname-container.azurewebsites.net/todos. Same API, now running from your own container.

App Service or Container Apps?

You might hear about Azure Container Apps too. Simple version:

  • App Service for containers: great when you have one web app and want things simple. That is what we used.
  • Azure Container Apps: better when you have several small services, background workers, or want to scale to zero. Microsoft's overview is here: Azure Container Apps.

The pipeline part is almost the same for both, so what you learned today carries over.

Common problems (and fixes)

  • The site shows "Application Error": check Log stream in the portal. Most often it is the port. Make sure WEBSITES_PORT=8080 is set.
  • "Unauthorized" when pulling the image: the AcrPull role assignment can take a few minutes. Wait, then restart the app.
  • Build is slow every time: keep the COPY of the .csproj file before COPY . ., so Docker can cache the restore step.

Your homework

  • Add the Dockerfile and .dockerignore and run the container locally
  • Create the registry and the service connection
  • Build and push from the pipeline
  • Deploy the container to App Service

Coming up next

In Part 8, the last one, we clean things up like a pro: reusable YAML templates, a private NuGet feed in Azure Artifacts, running your own self-hosted agent on a cheap server, and deleting everything so you do not get a surprise bill.

Are you using containers at work yet, or still deploying the classic way? Both are fine, I am just curious.