3

I have seen a number of similar questions but none seem to represent exactly the issue I am facing. When I create a folder structure as follows:

./
./src
./src/test

Then navigate to ./src/test and run dotnet new webapi -lang c#, this will create a minimal API which works fine. I can also run dotnet publish -c RELEASE -o out /p:Version=1.0.0 without any issues.

When I then try create a docker file at the root level with the following contents:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

ARG BUILDCONFIG=RELEASE
ARG VERSION=1.0.0

COPY ./src/test/test.csproj ./src/test/
RUN dotnet restore ./src/test/test.csproj

COPY ./src/ ./
WORKDIR ./src/test/
RUN dotnet publish -c $BUILDCONFIG -o out /p:Version=$VERSION

FROM mcr.microsoft.com/dotnet/sdk:6.0
WORKDIR /app
COPY --from=build /src/test/out ./

EXPOSE 5000
ENTRYPOINT ["dotnet", "test.dll"]

I get the following output:

 => ERROR [build 6/6] RUN dotnet publish -c RELEASE -o out /p:Version=1.0.0                                        2.5s
------
 > [build 6/6] RUN dotnet publish -c RELEASE -o out /p:Version=1.0.0:
#11 0.511 Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET
#11 0.511 Copyright (C) Microsoft Corporation. All rights reserved.
#11 0.511
#11 0.931   Determining projects to restore...
#11 1.153   All projects are up-to-date for restore.
#11 2.400 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/test/test.csproj]

I tried a few things, including setting the OutputType in the csproj to Exe, DockerDefaultTargetOS to both win/linux and a few other suggestions I found in other threads but ultimately I always get the same error. Any ideas whay might be wrong?

WhiteWaterCoder
  • 119
  • 2
  • 7

2 Answers2

5

The error message is not explicative.

The real error is on the line of COPY ./src/ ./. With this cmd you copy the CONTENT of the src folder (test) into the ROOT of the container.

It should be COPY ./src/ /src/ or (better) ./src /src

Try to comment out ALL except the first 9 rows, build the container and run

docker run --rm -it <imagename> /bin/sh to see yourself.

Since you try to build an empty folder the compiler raise the error that not found the main method (in reality it not find anything...)

Max
  • 6,821
  • 3
  • 43
  • 59
1

I had the same issue using Visual Studio (Preview) on the Mac. Moved the Dockerfile to the solution root folder then everything worked.

Visual Studio for some reason put the Dockerfile at \Users\robk\git\Play-With-Containers\Play-With-Containers\Dockerfile after Right-click > Add > Add Docker Support. (this also where the .csproj file is)

Dragged the Dockerfile up one level to \Users\robk\git\Play-With-Containers then it worked.

Rob Koch
  • 1,523
  • 3
  • 18
  • 26