Home > Net >  Visual Studio generated Dockerfile does not work with manual docker build
Visual Studio generated Dockerfile does not work with manual docker build

Time:12-31

I want to dockerize an existing .NET core 5 app and used the container tools to generate a Dockerfile. When I debug with Visual Studio 2022 it works but when I manually run it with the command docker build -t some-name . it generates the following error:

Step 7/21 : COPY ["MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj", "MechEng.MovableBridges.Api/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj: file does not exist

Why doesn't it find the file?

This is my Dockerfile that is inside the MechEng.MovableBridges.Api folder:

#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/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj", "MechEng.MovableBridges.Api/"]
COPY ["MechEng.MovableBridges.Infrastructure/MechEng.MovableBridges.Infrastructure.csproj", "MechEng.MovableBridges.Infrastructure/"]
COPY ["MechEng.MovableBridges.MathCad/MechEng.MovableBridges.Infrastructure.MathCad.csproj", "MechEng.MovableBridges.MathCad/"]
COPY ["MechEng.MovableBridges.Application/MechEng.MovableBridges.Application.csproj", "MechEng.MovableBridges.Application/"]
COPY ["MechEng.MovableBridges.Domain/MechEng.MovableBridges.Domain.csproj", "MechEng.MovableBridges.Domain/"]
RUN dotnet restore "MechEng.MovableBridges.Api/MechEng.MovableBridges.Api.csproj"
COPY . .
WORKDIR "/src/MechEng.MovableBridges.Api"
RUN dotnet build "MechEng.MovableBridges.Api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MechEng.MovableBridges.Api.csproj" -c Release -o /app/publish

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

CodePudding user response:

Your command fails, because the build context is wrong. Visual studio is using the solution root folder as build context, you are (probably) using the project's dockerfile's location. You can read more about the docker build command here.

Your command should look similar to this:

docker build -f "<path-to-your-dockerfile>" -t some-name "<!!!path-to-your-solution-folder!!!>"

You can see the exact command executed by visual studio in the output window, with "Container Tools" selected from the dropdown box.

  • Related