Cees
Cees

Reputation: 324

Visual Studio generated Dockerfile does not work with manual docker build

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"]

Upvotes: 6

Views: 5443

Answers (3)

mark_h
mark_h

Reputation: 5467

For those of you trying to run your Visual Studio generated Dockerfile in an Azure pipeline using the Docker@2 task you need to set the buildContext on the task. In many people's cases this is probably the solution folder which contains your project and the other projects it references;

- task: Docker@2
      inputs:
        containerRegistry: '<your container registry>'
        repository: '<your repository>'
        command: 'buildAndPush'
        Dockerfile: '*/<the start-up project>/Dockerfile'
        buildContext: '<Solution name>' <-- THIS
        tags: |
          $(tag)

Upvotes: 1

Rahim Lotfi
Rahim Lotfi

Reputation: 59

In visual studio, you can move the docker file to one back folder. For example, folder1/folder2/dockerfile move to folder1/dockerfile and build the image.

Docker build -t imageName .

Docker run -p 7778:80 imageName

Upvotes: 0

fbede
fbede

Reputation: 901

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.

Upvotes: 15

Related Questions