christophechichmanian
christophechichmanian

Reputation: 129

Wrong Dockerfile with Visual Studio and Asp.Net Core

Recently, I've created an ASP.Net Core project on Visual Studio 2019 with Docker Support enabled. It's then created me a full Dockerfile so I don't have to worry about this.

If I launch my application from Visual Studio, using the Docker launch settings, it builds perfectly and then runs, which is perfect !

But when I try to build it by hand using the docker build command, I get an error telling me:

COPY failed: stat /var/lib/docker/tmp/docker-builder563776422/Back/Back.csproj: no such file or directory

with Back being the name of my solution and the name of my only project in this solution.

If I then change the following line:

COPY ["Back/Back.csproj", "Back/"]

to

COPY ["Back.csproj", "Back/"]

it now builds with the command. This modification seems logical to me since the Dockerfile is already located inside of my project's folder, and not at the root of the solution.

I'm a bit lost here, why did it work on Visual Studio before the modification? Am I right to change this line?

Here is the full Dockerfile (before modification):

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

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

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

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

Upvotes: 5

Views: 2391

Answers (2)

Bill Lam
Bill Lam

Reputation: 504

You can explicitly specify Dockerfile location and context path during docker build:

docker build -f $dockerfilelocation $contextpath

VS does the same when it is building a docker image for your project:

docker build -f "C:\Temp\Code\WebApp\WebApp\Dockerfile" --force-rm -t webapp:dev --target base  --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=WebApp" "C:\Temp\Code\WebApp"

Screen shot of Build output

Upvotes: 2

Matt Thalman
Matt Thalman

Reputation: 3995

Because the default Dockerfile that gets generated assumes that you'll set your solution folder as your build context. The build context is the directory which will be copied to the Docker daemon, providing files that your Dockerfile can then copy into the image. It makes sense for this to be at the solution level. It would also make more sense for the Dockerfile to also exist at the solution level.

One option is to leave the Dockerfile where it is and set the solution directory as the build context: docker build ..\ (where ..\ indicates to navigate up one directory to the solution directory).

The other option is move the Dockerfile to be located in the solution directory.

Upvotes: 2

Related Questions