.Net core API deployment on Docker

.Net core API deployment on Docker


Docker can be called a platform as a service, which uses the OS virtualization concept to deliver application building which is called containers.

How to install Kafka using Docker - ITNEXT


In this article I will explain how you can create .net core project docker enable, also how one container communicates and call another container API with the help of docker-compose command, also will list some useful commands which help you manage docker.

To create your project with docker support follow simple steps given below.

Create .net core project with following docker setting enable, in my case I selected ASP.net Core Web application.


  Note that I am selecting here "Enable Docker Support" and according to your need you can select a container platform, you have two options "Windows" and "Linux", in my case I selected  Windows.

here you have to make sure that the docker is running under windows, switch to windows containers if already on Linux.


Once you create a web API project, there DockerFile get created which is there due to you enabling docker  windows in your project,   file contact is as following.


FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["WebApplication2.csproj", ""]
RUN dotnet restore "./WebApplication2.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication2.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication2.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication2.dll"]


Each instruction creates one layer in container:
  • FROM creates a layer from the  mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 Docker image.
  • COPY adds files from your Docker client’s current directory.
  • RUN builds your application with make.

For more information  please refer to  DockerFile Documentation

  Now your application is ready you can debug your application in locally running docker application by clicking on the play button on top, this will host your web API application in docker as container and output will appear in the web browser.

above is some background on docker and web API hosting on docker windows(locally).

now create one more project as we build above with different names.

Create binaries of both projects with publish option in VS.net. in my case, I create one root folder called publish in which I put both  API  build folder.

Now here comes the tricky part now  here we want to have a connection btw these two project, where A project will call API in B project to do so we have to  have another file called docker-compose, the content of this file is as  following 

version"3.0"
services:
    sebapplication2:      
      build./WebApplication2
      ports:
        - "2182:80"
    webapplication1:      
      build./WebApplication1
      ports:
        - "2181:80"
      links:
        - WebApplication2
     

this file needs to be placed on the root as depicted in the following image.


now open common prompt and go to  this folder and run  following common

1- docker-compose  build (this will build the  containers) 

2- docker-compose  up -d( this will run the container in local docker)  Note:  -d means it will run the container in the background.

now try  http://localhost:2181/api/values  this will open  the values route of  the web service, this will confirm you that your web services are hosted  
similarly you can verify http://localhost:2182/api/values for other applications.

now you can also call route which call other web API route if you notice we have used line "links" which made a connection between both the services in compose file.

Note that WebApplication1  and WebApplication2 DockerFile should look like as following and both applications binaries should be placed inside their folders as well.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-nanoserver-1809
EXPOSE 80
WORKDIR /app
    COPY ./build .
ENTRYPOINT ["dotnet""WebApplication1.dll"]

I hope you will find this article helpful if you need more help in any of the docker related issues please feel free to comment below.




Comments

Popular posts from this blog

Setup source code of Visual studio on Bitbucket

.Net Core WebClient and proxy setup

Tables with ledger in sql server 2022