dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

Visual studio ignoring docker file #58064

Open vadimt2 opened 3 weeks ago

vadimt2 commented 3 weeks ago

Is there an existing issue for this?

Describe the bug

visual studio is ignoring the Docker file

# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
#COPY ["WebApplication13/WebApplication13.csproj", "WebApplication13/"]
##RUN dotnet restore "./WebApplication13/WebApplication13.csproj"
#COPY . .
#WORKDIR "/src/WebApplication13"
#RUN dotnet build "./WebApplication13.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
#FROM build AS publish
#ARG BUILD_CONFIGURATION=Release
#RUN dotnet publish "./WebApplication13.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
#
## This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
#FROM base AS final
#WORKDIR /app
#COPY --from=publish /app/publish .
#ENTRYPOINT ["dotnet", "WebApplication13.dll"]

Expected Behavior

when you click on the play button, it should run copy and etc. but all the code is comment and it's still running, it doesn't event restoring the nugets -> when you build docker with cmd it's working!

Steps To Reproduce

Use the latest vs 2022 create a docker file simillar to:

# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Docker file to build your images for faster debugging.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
#COPY ["WebApplication13/WebApplication13.csproj", "WebApplication13/"]
##RUN dotnet restore "./WebApplication13/WebApplication13.csproj"
#COPY . .
#WORKDIR "/src/WebApplication13"
#RUN dotnet build "./WebApplication13.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
#FROM build AS publish
#ARG BUILD_CONFIGURATION=Release
#RUN dotnet publish "./WebApplication13.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
#
## This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
#FROM base AS final
#WORKDIR /app
#COPY --from=publish /app/publish .
#ENTRYPOINT ["dotnet", "WebApplication13.dll"]

and run

Exceptions (if any)

No response

.NET Version

8

Anything else?

No response

amcasey commented 3 weeks ago

Are you using the Container launch profile? image

amcasey commented 3 weeks ago

Did you enable container support when you created the project or are you adding a docker file later? image

vadimt2 commented 2 weeks ago

I have created a project with a docker support and Clicked on the "Container (Dockerfile)" - like in the first image

amcasey commented 1 week ago

I'm not actually on the team that works on these tools, but my best guess would be that it's working by accident. In the Container Tools output window, you can see the command line used to start docker. For me, I see this mount:

-v "C:\Users\amcasey\source\repos\WebApplication50\WebApplication50:/app:rw"

Since I've built locally (I believe F5 will do this, even if you're launching in a container), the mounted /app directory contains a bin directory. I suspect that dotnet is invoked in such a way that it discovers the pre-built binaries in bin and runs those.

Edit: maybe it's not an accident. The docs say that the base target is used for Debug builds and the final target is used for other (i.e. Release) builds.

NCarlsonMSFT commented 1 week ago

@vadimt2 FYI the best place to get support for the container tools is https://github.com/microsoft/DockerTools/issues

But while I'm here, as @amcasey said the default behavior in the container tools is to not build the entire image, instead using volume mounts to make for a faster F5 experience. If you prefer having the entire Dockerfile built for each F5, you can set the MSBuild property <ContainerDevelopmentMode>Regular</ContainerDevelopmentMode> in your project