As we all know, ASP.NET Core is now becoming the most popular web application development tool for developing web applications on .NET platform in a smart way.
Being open-source, ASP.NET Core has another great feature called cross-platform and container support, this means it can run on Windows, Linux and macOS, which makes this platform more unique and robust.
ASP.NET Core was initially released in 2016 which was Windows OS only and later on it was re-designed to have cross-platform support.
Features
- It can run on Windows, macOS and different/multiple distributions of Linux.
- Another feature is it can support different CPU architecture.
- We can now develop and publish asp.net application on Windows, macOS and Linux too.
Container Support
Another feature of ASP.NET Core is Container support so that you can place your published code directly in a container.
There are many containers available in the market like Docker, LXC, rkt, Kubernetes, Cloud Foundry and Vagrant.
Let’s understand Container in a simple way.
Container is nothing but a lightweight, standalone and executable package set of software that is required by application to run. In other words, Container is a software which includes an application and all of its runtime dependencies.
- Runtime
- Code
- System Tools
- System Library
- Other Settings required by application
So, in simple words, Container helps us to build an application once and run it anywhere. Also we can isolate each application on a host operating system.
Before we understand how it works with ASP.NET Core, we need to install Docker first if are working on Windows Platform.
Use this
link to install Docker for Windows.
Once installation is done, now let's understand in brief how Doker Container works with ASP.NET Core.
- Develop and publish a required ASP.NET Core application
- Once published, then create and configure a Dockerfile for ASP.NET Core application
- Once the above step is done build a Docker image
- Once build is done then create and run a Docker container at the end
Once we are done with this, we will see a new Dockerfile is created at the same root of where our project files exist as below.
So now this Docker file will run your created application inside container.
Let's have a look at how it looks.
- #Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
- #For more information, please see https://aka.ms/containercompat
-
- FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-nanoserver-1903 AS base
- WORKDIR /app
- EXPOSE 80
- EXPOSE 443
-
- FROM mcr.microsoft.com/dotnet/core/sdk:2.1-nanoserver-1903 AS build
- WORKDIR /src
- COPY ["TestDocker.csproj", ""]
- RUN dotnet restore "./TestDocker.csproj"
- COPY . .
- WORKDIR "/src/."
- RUN dotnet build "TestDocker.csproj" -c Release -o /app/build
-
- FROM build AS publish
- RUN dotnet publish "TestDocker.csproj" -c Release -o /app/publish
-
- FROM base AS final
- WORKDIR /app
- COPY --from=publish /app/publish .
- ENTRYPOINT ["dotnet", "TestDocker.dll"]
You will see the Docker option as in the below screen to run your application under Docker Container.
Above is a sample Docker file which defines a set of instructions or rules that creates an image for the specific application.
Benifits of using Docker with ASP.NET Core.
- One of the main benefit of using ASP.Net Core for Docker is it allows to run .Net versions of applications side by side within the same machine on which it is hosted.
- .NET Core application can easily run in a Docker container with minimum efforts, thanks to Microsoft.
- Containers provide a smooth lightweight way to isolate your application from the rest of the host system and which sharing only just the kernel and using resources given to your application on which it is depend.
In short a container is a runnable instance of an image, as you build your image, you deploy your application and dependencies. And then, multiple containers can be instantiated and each one is isolated from one another. Each and every container instance has its own memory, network interface and filesystem.
I hope you like this article and it's helpful.