Need assistance with Dockerfile and Kubernetes for .AspNetCore service

3/21/2019

My docker build is failing due to the following error:

COPY failed: CreateFile \?\C:\ProgramData\Docker\tmp\docker-builder117584470\Aeros.Services.Kubernetes\Aeros.Services.Kubernetes.csproj: The system cannot find the path specified.

I am fairly new to docker and have went with the basic project template that is set up when you create a Kubernetes container project template, so I'd figure it would work out of the box, but I'm mistaken.

I'm having problems trying to figure out what it's attempting to due in the temp directory structure and the reason it is failing. Can anyone offer some assistance? I've done some searching and others have said the default docker template was incorrect in Visual Studio, but I'm not seeing any of the files being copied over to the temp directory to begin with, so figuring out what is going on is being rather problematic at the time.

Here is the docker file, the only thing I've added is a publishingProfile arg so I can tell it which profile to use in the Build and Publish steps :

ARG publishingProfile

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["Aeros.Services.Kubernetes/Aeros.Services.Kubernetes.csproj", "Aeros.Services.Kubernetes/"]

RUN dotnet restore "Aeros.Services.Kubernetes/Aeros.Services.Kubernetes.csproj"
COPY . ./
WORKDIR "/src/Aeros.Services.Kubernetes"
RUN dotnet build "Aeros.Services.Kubernetes.csproj" -c $publishingProfile -o /app

FROM build AS publish
RUN dotnet publish "Aeros.Services.Kubernetes.csproj" -c $publishingProfile -o /app

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

I haven't touched the yaml file, but if you need that I can provide it as well. Again, all I've done with this is add a few NuGet packages to the project reference. Build in VisualStudio runs fine, but the docker command:

docker build . --build-arg publishingProfile=Release

is failing with the error mentioned above.

Can someone be so kind as to offer some enlightenment? Thanks!

Edit 1: I am executing this from the project's folder via a PowerShell command line.

-- Nathan Raley
asp.net-core
docker
kubernetes
visual-studio

2 Answers

3/21/2019

In which line the problem happens? I do not remember if docker build shows it.

Where are you executing this build? The problem is that it is not finding the file you are trying to copy. It should be local to where the command is executed.

I saw now, the problem is on the first COPY.

-- Leandro Donizetti Soares
Source: StackOverflow

3/22/2019

Leandro's comments helped come across the solution.

So first a rundown of that COPY command, it takes two parameters, source and destination. Within the template for the Dockerfile for Visual Studio, it includes the folder location of the .csproj file it is attempting to copy. In my case, the command read as follows:

COPY ["Aeros.Services.Kubernetes/Aeros.Services.Kubernetes.csproj", "Aeros.Services.Kubernetes/"]

So it is looking for my Aeros.Services.Kubernetes.csproj file in the Aeros.Services.Kubernetes project folder and copying it to the Aeros.Services.Kubernetes folder in the src folder of Docker.

The problem with this is that if you use the default setup, your dockerfile is included inside the project folder. If you are executing the docker build from within the project folder, the syntax for the COPY command is actually looking in the wrong file location. For instance, if your project is TestApp.csproj located in the TestApp project folder, and you are executing the Docker build command for the dockerfile within the same folder, the syntax for that COPY command:

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

is actually looking for: TestApp/TestApp/TestApp.csproj.

The correct syntax for the COPY command in this situation should be:

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

since you are already within the TestApp project folder.

Another problem with the default template that may trouble some is that it doesn't copy the web files for the project either, so once you get past the COPY and dotnet restore steps, you will fail during the BUILD with a:

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point

This is resolved by adding:

COPY . ./

following your RUN dotnet restore command to copy your files.

Once these pieces have been addressed in the default template provided, everything should be functioning as expected.

Thanks for the help!

-- Nathan Raley
Source: StackOverflow