Deploying API as a container

Practice Lab

This practice lab is divided into 3 steps like this:

1- Develop dot net core API/App in visual studio 2019.

2- Deploy code as image in to docker hub (optionally you can deploy to Azure Container Registry also)

3- Creating Azure App Service using Azure CLI based on Image available on docker hub as per step 2.

Open your Visual Studio 2019 and create a new ASP Net core Web Application like this


On configure new project screen give proper project name


 In next screen make sure you select MVC project template with Enable Docker support for application.


Please note my docker was configured for linux so I am choosing it in the dropdown. You can select as per your local docker desktop configuration.

Once you click on Create button you will get New project in your solution explorer if visual studio like this.


Please notice you have MVC project template with Folders like Models Views and Controllers with an extra Dockerfile which has docker compose code to create image from this.

Dockerfile code will look like this

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base

WORKDIR /app

EXPOSE 80

EXPOSE 443


FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build

WORKDIR /src

COPY["WebApp4Container123/WebApp4Container123.csproj","WebApp4Container123

RUN dotnet restore "WebApp4Container123/WebApp4Container123.csproj" 

 COPY . .

WORKDIR "/src/WebApp4Container123"

RUN dotnet build "WebApp4Container123.csproj" -c Release -o /app/build


FROM build AS publish

RUN dotnet publish "WebApp4Container123.csproj" -c Release -o /app/publish


FROM base AS final

WORKDIR /app

COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "WebApp4Container123.dll"]


Right click on controllers folder and add new API controller in this project


Give name of the controller as TrainingController


Logically this controller should connect with Model and get the data from database using queries and stored procedures but, in our case, we have simple API controller with some Array data


To call this API from Home page we are adding following HTML code in _layout.cshtml page of this application

This will Add New Tab on the top navigation bar so that we can click on that and get the API data.


IF you click on Call API Tab you will get JSON response like this


Now once we have tested this application in local now it’s a time to deploy this to Docker Hub as an Image.

Right click on the project name in solution explorer and click on publish

On publish target page choose Container Registry and Docker hub and then click on create profile.


If it is asking for credentials then provide your account username and password which you have used in Docker Hub.

Note: if you do not have docker hub account please register using following link.

https://hub.docker.com/


Click on Publish button and observe the output window for the progress of this step.

As written in Dockerfile of this project this will take base image from Microsoft Container Registry and create and deploy our custom image into our own public repository.

Once this process is done it will push this image into Docker hub and from that you can anytime access image and create containers from that.

IF you make your repository public then your team can also pull it just by providing Image name and Tag name.


You can click on this repository in Docker hub and get the Image and Tag details from it.

My repository which I am using in this practice lab is having following details


If you want you can also use this same image.in next step.

Navigate to https://shell.azure.com and in bash command prompt execute following command to create new Resource group


Once resource group is created fire another command to create app service which is using our Docker hub image and deploying it with public IP Access like this.


For your ref both the commands are given below

az group create --name trainingRGcntr1 --location eastus

az container create --name marutihelloworld123 --image

marutimakwana/webapp4container123 --resource-group trainingRGcntr1 --ip-address public

once both the commands are executed successfully it will provide one public ip address on which this app service is hosted.


Try to access this IP in separate browser tab and you will get that same application with the Call API tab on that


Try to call API and I am sure it will work like this.


For all the members of Mentorstag this image is available on my Github 

repository so give it a try and do not forget to try other Azure Practise Labs of the site.

Keep Learning and Make Yourself Future Ready.



Maruti Makwana
An Indian, Coach, Learner, Father, Husband, and friend for a few Chosen ones

.

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
Mentor's TAG 2024 Privacy policy Terms of use Contact us Refund policy