Overview:

Azure App Service on Linux provides pre-defined application stacks on Linux with support for languages such as .NET, PHP, Node.js and others. You can also use a custom Docker image to run your web app on an application stack that isn’t already defined in Azure.

This post shows you how to deploy an image from an Azure Container Registry (ACR) to App Service.

Below are the Azure services available for Containers.

Demo: Run a custom container in Azure App Service

Prerequisites

Steps (Summary):

1 – Create a container registry

2 – Sign in, VSC and verify Docker Images

3 – Check prerequisites (docker version)

4 – Create and build image; to simply starts that app

5 – Deploy to container registry

6 – Deploy to App Service

StepDesc/Manual Steps in VSC
1 – Create a container registry:  Azure Container Registry is a private registry service for building, storing, and managing container images and related artifacts. 
Other Options are: DockerHub: Docker Hub Container Image Library | App Containerization 
mycintainerregistryrk.azurecr.io
2 – Sign in, VSC and verify Docker ImagesCheck the images in VSC
3 – Check prerequisites; check the docker version etc.docker –version
4 – Create and build image;   this Docker file is used to simply starts that app;use (Docker Images: Build Image),  
<acr-name>.azurecr.io/<image-name>/<tag>    
5 – Deploy to container registry;Select Docker Images and Push, select   mycintainerregistryrk.azurecr.io/dockerfile/1.0:latest
6 – Deploy to App Service;In the REGISTRIES explorer, expand the image,
right-click the tag, and select Deploy image to Azure App Service.

The App Service app pulls from the container registry every time it starts. If you rebuild your image, you just need to push it to your container registry, and the app pulls in the updated image when it restarts.

To tell your app to pull in the updated image immediately, restart it.

Step-by-step

1 – Create a container registry

1 – Create a container registrymycintainerregistryrk.azurecr.io

Create either from Azure Portal.

And validate in VSC.

Command/CLI

Log in to registryaz acr login –name <registry-name>az acr login –name mycontainerregistry
Push Image docker pull mcr.microsoft.com/hello-world
Tag Imagedocker tag mcr.microsoft.com/hello-world <login-server>/hello-world:v1docker tag mcr.microsoft.com/hello-world mycontainerregistry.azurecr.io/hello-world:v1
Push Imagedocker push <login-server>/hello-world:v1 
Run Image from Registrydocker run <login-server>/hello-world:v1 

2 – Sign in, VSC and verify Docker Images 

2 – Sign in, VSC and verify Docker ImagesCheck the images in VSC

 3 – Check prerequisites (docker version)

3 – Check prerequisites;docker –version

 4 – Create and build image; to simply starts that app;

4 – Create and build image; to simply starts that app;use (Docker Images: Build Image),<acr-name>.azurecr.io/<image-name>/<tag>

 5 – Deploy to container registry;

5 – Deploy to container registry; Select Docker Images and Push, select mycintainerregistryrk.azurecr.io/dockerfile/1.0:latest

<acr-name>.azurecr.io/<image-name>/<tag>

mycintainerregistryrk.azurecr.io/dockerfile/1.0

Executing task:

docker build –pull –rm -f “dockerfile.dockerfile” -t

mycintainerregistryrk.azurecr.io/dockerfile/1.0 “.”

 

6 – Deploy to App Service;

6 – Deploy to App Service;In the REGISTRIES explorer, expand the image, right-click the tag, and select Deploy image to Azure App Service.

The App Service app pulls from the container registry every time it starts. If you rebuild your image, you just need to push it to your container registry, and the app pulls in the updated image when it restarts. To tell your app to pull in the updated image immediately, restart it.

Validate

Code behind:

Understanding Dockerfile

Containerize an app with Docker tutorial – .NET | Microsoft Learn

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env 
WORKDIR /App 
# Copy everything 
COPY . ./ 
# Restore as distinct layers 
RUN dotnet restore # Build and publish a release 
RUN dotnet publish -c Release -o out # Build runtime image 
FROM mcr.microsoft.com/dotnet/aspnet:8.0 
WORKDIR /App 
COPY --from=build-env /App/out .
 ENTRYPOINT ["dotnet", "DotNet.Docker.dll"]
# Build Stageuses the .NET SDK to install any required dependencies and build the project before publishing it to a folder named out   
FROMFROM mcr.microsoft.com/dotnet/sdk:6.0 AS build  
WORKDIR  
COPY  
RUN  
# Serve Stageuses the ASP.NET Core runtime image to run the application from the specified working directory, which is app in this case.    

Conclusion

Containerizing .NET applications using Docker provides numerous benefits, including portability, scalability, and an immutable infrastructure. By following the steps outlined in this post, you can easily deploy your .NET apps in custom containers on Azure App Service or locally using Docker.

Whether you are developing or deploying .NET applications, containerization offers a flexible and powerful solution for managing and scaling your applications.

Happy coding! 🚀🐳

References:

https://learn.microsoft.com/en-us/azure/app-service/quickstart-custom-container?tabs=dotnet&pivots=container-linux-vscode
https://learn.microsoft.com/en-us/dotnet/core/docker/build-container?tabs=windows&pivots=dotnet-8-0

Leave a Reply

Discover more from Rajeev Singh | Coder, Blogger, YouTuber

Subscribe now to keep reading and get access to the full archive.

Continue reading