. net 5 is deployed to run on docker


1. Create a site

Create an asp.net core web application and select enable docker support. Automatically create a dockerfile for us.



2. Write dockerfile file

Dockerfile is a configuration file in file format. Users can use dockerfile to quickly build a custom image. It consists of one line of command statements and supports comment lines beginning with #.

The topic content of dockerfile is generally divided into four parts

  1. Basic image information

  2. Maintainer information using label instruction

  3. Mirror operation instruction

  4. Execute command when container starts

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

#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/aspnet:5.0 AS base
#FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
#COPY ["AA.Mvc/AA.Mvc.csproj", "AA.Mvc/"]
#RUN dotnet restore "AA.Mvc/AA.Mvc.csproj"
#COPY . .
#WORKDIR "/src/AA.Mvc"
#RUN dotnet build "AA.Mvc.csproj" -c Release -o /app/build
#FROM build AS publish
#RUN dotnet publish "AA.Mvc.csproj" -c Release -o /app/publish
#FROM base AS final
#COPY --from=publish /app/publish .
#ENTRYPOINT ["dotnet", "AA.Mvc.dll"]

#Parent image
#FROM microsoft/aspnetcore:5.0
#FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS baseWORKDIR /app

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
##Set working directory
##Copy the publish file to / APP
#COPY . /app
##Set port
#EXPOSE 5005
COPY . .
#Use aa.mvc.dll to run asp.net core project, pay attention to case
#ENTRYPOINT ["dotnet", "AA.Mvc.dll", "--server.urls", "http://*:5005"]
ENTRYPOINT ["dotnet", "AA.Mvc.dll"]

Instruction description of dockerfile file:

  • From – specifies the base image of the created image

  • Workdir – configure working directory

  • Expose – declare the port on which the service in the image listens

  • Copy – copy content to image

  • Entrypoint – the default command to start the image

3. Compile and publish web project







4. Build the image, run the container and browse

To create an image, you can use the command docker build as follows:

docker build -t core-mvc .



This step may be slower, because you need to download the running environment. Please wait patiently



Parameter decomposition:

  • -T — specify the image name

  • At the end of the command  . — Indicates that the build context is the current directory. By default, docker will use the root directory of the context to find the dockerfile file

Look at the image list and enter the command

docker images




Create, start and view containers

docker run --name netcore-mvc -d -p 50879:80  core-mvcdocker ps -a



Parameter description

  • -D, which means to run the container in the form of daemonized in the background

  • -P external port and internal container port mapping.

  • –Name specifies the name of the container. Of course, you can not specify it. It will be created for us by default

Here, if docker run fails and runs again, it will prompt that the name already exists. You can use the following command to delete the container

docker rm -f netcore-mvc

The last parameter core MVC is the image name we just created. After the above four steps, enter the address in the browser http://localhost : 50879 / see the picture




So far, a complete project runs in the docker container manually.