Containers are similar to virtual machines.But they have the following advantages when compared to virtual machines:-
- They are lightweight since all the containers share the same OS.
- They have faster start up time.
Docker is one of the most popular containerization technologies.Docker can run on different OS such as Windows,Linux and Mac.
We can run ASP.NET Core application in a container by following the below steps:
1.Create ASP.NET Core application.You can use the following commands to create ASP.NET Core application:-
dotnet new webapp
2.once the application is created,create a file named Dockerfile in the root folder of the application.Dockerfile defines the image.This image can be used to run
containers.There are different instructions in the docker file such as:
- FROM
- RUN
- COPY
- CMD
- ENTRYPOINT
You can use the following instructions in your Dockerfile to create image of your ASP.NET Core application.We are using two base layers here one for dotnet core sdk and another for asp.net core runtime.
# https://hub.docker.com/_/microsoft-dotnet-core
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /source
RUN echo 'building app'
# copy csproj and restore as distinct layers
#COPY *.sln .
COPY mvc/*.csproj ./mvc/
#RUN dotnet restore
RUN echo 'building app'
# copy everything else and build app
COPY mvc/. ./mvc/
WORKDIR /source/mvc
RUN dotnet publish -c release -o /app #--no-restore
# final stage/image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "NAME OF YOUR APPLICATION..dll"]