Deploying .NET Core web application using docker container on Azure

Please check previous article about docker before proceeding further,

Photo by frank mckenna on Unsplash

Requirements before start:

1. Azure sign in.

2. Docker hub sign in.

3. Docker desktop.

4. Sign in to Docker hub using local machines.

5. Visual Studio.

Azure App Service is a fully managed “Platform as a Service” (PaaS) that integrates Microsoft Azure Websites, Mobile Services, and BizTalk Services into a single service, adding new capabilities that enable integration with on-premises or cloud systems.

The increase of containerization application deployments azure app service added the facility to deploy docker based web applications. Different kinds of options are provided in azure like single and multiple container deployments also developers can select public and private docker images.

Step 1) Create a web application using visual studio

Create new project → Asp.net core web application -> Select any template(MVC)

For adding docker file right click on the solution then click on add then click on add docker support.

After that change docker file according to your application, please find below code which added in dockerfile.

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS baseCOPY bin/Release/netcoreapp2.1/publish/ app/ENTRYPOINT ["dotnet", "app/WebApplication2.dll"]

Here, line 1 is getting .net core runtime from repository and line2 is copying published version into the app folder and line 3 is defining ENTRYPOINT instruction allows you to configure a container that will run as an executable of the application.

Step2) Build a docker image using dockerfile

Command : docker build –t dockerusername/imagename.

> docker build -t vaibhavrb/webapplication -f dockerfile .

Sending build context to Docker daemon  16.57MBStep 1/3 : FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base---> b3df7864b3e1Step 2/3 : COPY bin/Release/netcoreapp2.1/publish/ app/---> Using cache---> c97323bac0a4Step 3/3 : ENTRYPOINT ["dotnet", "app/WebApplication1.dll"]---> Using cache---> bbbdfdce8541Successfully built bbbdfdce8541Successfully tagged vaibhavrb/webapplication:latestSECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

Step 3) Start the Docker container on your local machine:

Command: docker container run --name <container name> -p <port used in local machine>:<port running in container> <image name>

Port used in the local machine: port number in which the app runs in our local machine. in my case 8090.

port running in container: docker starts the app in the port specified in the program. Type the port value here. In my case, I set the port value to 80.

image name: specify the image name after executing the previous step.

> docker container run — name azuredeptest -p 8090:80 vaibhavrb/webapplication

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[58]Creating key {d1bb5f67-71c4-4db0-8f34-8e5d00080f7b} with creation date 2019-12-09 12:25:59Z, activation date 2019-12-09 12:25:59Z, and expiration date 2020-03-08 12:25:59Z.warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]No XML encryptor configured. Key {d1bb5f67-71c4-4db0-8f34-8e5d00080f7b} may be persisted to storage in unencrypted form.info: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[39]Writing data to file '/root/.aspnet/DataProtection-Keys/key-d1bb5f67-71c4-4db0-8f34-8e5d00080f7b.xml'.Hosting environment: ProductionContent root path: /Now listening on: http://[::]:80Application started. Press Ctrl+C to shut down.

After that start and test your app locally using the below URL:

http://localhost:8090/

Step 4) Pushing created image on docker hub:

Command: docker push <docker username>/<image name>

> docker push dockerusername/webapplication

The push refers to repository [docker.io/ dockerusername /webapplication]c3b400f3a2be: Mounted from dockerusername/webapp3c5b129061374: Mounted from dockerusername/webapp3a2f9ed91e120: Mounted from dockerusername/webapp375bb365bb264: Mounted from dockerusername/webapp399b5261d397c: Mounted from dockerusername/webapp3latest: digest: sha256:70fd9e4004234ad32c2d73a009392a77996d754607416fbab7e9c421f2b2d1db size: 1374

Step 5) Deploying containerized application on azure.

Go to azure portal -> All resources -> App service -> Create new app service -> mention details as specified below.

Where you need to select a resource group and name of application type of publish where you need to select a docker container then operating system and region also you need to select a pricing plan for app service.

In the next window, you enter docker details which includes docker option which includes source, access type, and Image and tag.

Fill in the following details:

Option: ‘Single container’ as we are using only a single docker container and not many simultaneously.

Image source: ‘Docker hub’

Access type: ‘Public’ based on your image on the docker hub.

Image and tag: paste the name of the image which we pushed to the docker hub. The format is: <dockerhub username>/<image name>

Startup command: leave this field empty as we do not want to override any command once the container has spun up.

Once you entered these details just deploy this app service then browse particular app service through URL you will see the container application over there.

Step 6) STOP/DELETE of app service plan

STOP will stop all the running processes for the app but you have to pay for other resources such as storage, memory, etc.

DELETE will remove all processes related to the app and you are no longer billed for it.

Thank You, See you in the next article !!

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *