Vs2019 enable docker support

Time:2021-1-17
  • Create with vs2019 ASP.Net Core web application:
  • One of the advanced options on the right enables docker support. After checking, vs will automatically help us create dockerfile:
  • Take a look at the contents of dockerfile
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

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

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

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

At first glance, it seems perfect…

  • Next, use docker build. The usual method is as follows:
    docker build -t mywebapp .

    Then go to step 6 and report an error: copy failed: stat / var / lib / docker / TMP / docker builder 893785636 / webapplication1 / webapplication1.csproj: no such file or directory
    There is no such file or directory. If you look at copy [“webapplication1 / webapplication1. Csproj”, “webapplication1 /”] carefully, it’s really wrong. Webapplication1 directory is redundant.

    Then I tried to modify the dockerfile file, but it didn’t work…

Solution 1:

  • Move the generated dockerfile file to the upper level directory and execute in the upper level directorydocker build -t mywebapp .That’s it.


Solution 2:

  • Execute docker build in the upper directory and use the – f parameter to specify the location of dockerfile filedocker build -t mywebapp1 -f ./WebApplication1/Dockerfile .

Solution 3:

  • Use vs2019 to start the project directly. Set the project as docker to start

    CTRL + F5 to start:

    But due to the network and other reasons, we may encounter other problems…