According to Microsoft, by default the debugger runs in Fast Mode to speed up the building process of your docker containers. In this mode, your Dockerfile is only partially built.
https://docs.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2019
In Fast mode, Visual Studio calls docker build with an argument that tells Docker to build only the base stage. Visual Studio handles the rest of the process without regard to the contents of the Dockerfile. So, when you modify your Dockerfile, such as to customize the container environment or install additional dependencies, you should put your modifications in the first stage. Any custom steps placed in the Dockerfile's build, publish, or final stages will not be executed.
This means that if you bring in any other image in your Dockerfile before the image you are using for your runtime, it will try to use this image for your container.
Solution
- If you want to keep all the steps in your Dockerfile, while still being able to debug, bring your runtime image in at the top of the file and still use it where you need it. For example:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 as base
...
FROM base
WORKDIR /src
...
- Alternatively you can add a PropertyGroup item to your project file, telling it to build the docker file in Regular Mode. This will slow down your builds.
<PropertyGroup>
<ContainerDevelopmentMode>Regular</ContainerDevelopmentMode>
</PropertyGroup>
Reproducing the error
I verified this by creating a new empty .NET core project with Docker Support and Container Orchestration.
The original dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["TestApplication/TestApplication.csproj", "TestApplication/"]
RUN dotnet restore "TestApplication/TestApplication.csproj"
COPY . .
WORKDIR "/src/TestApplication"
RUN dotnet build "TestApplication.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TestApplication.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestApplication.dll"]
Runs fine. When adding a different image, node in this case, at the top of the Dockerfile, the debugger breaks:
FROM node:14-alpine as node-base
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["TestApplication/TestApplication.csproj", "TestApplication/"]
RUN dotnet restore "TestApplication/TestApplication.csproj"
COPY . .
WORKDIR "/src/TestApplication"
RUN dotnet build "TestApplication.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TestApplication.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestApplication.dll"]