Unable to build docker image from docker file, for dotnet core web application

12/22/2019

I am trying to build a docker image for dotnet core app on windows which I am planning to host it on Kubernetes

with following details

#docker file

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

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

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

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

directory Structure is >>

enter image description here

After running docker build -t amazing-app-one . I am getting following error at step 10/16. Application builds & runs locally but docker is not able to build image out of it.

gt;
<Path>\amazing-app>docker build -t amazing-app-one . Sending build context to Docker daemon 5.741MB Step 1/16 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base ---> 08096137b740 Step 2/16 : WORKDIR /app ---> Running in 14f5f9b7b3e5 Removing intermediate container 14f5f9b7b3e5 ---> ae4846eda3f7 Step 3/16 : EXPOSE 8989 ---> Running in 0f464e383641 Removing intermediate container 0f464e383641 ---> 6b855b84749e Step 4/16 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build ---> 9817c25953a8 Step 5/16 : WORKDIR /src ---> Running in 5a8fc99a3ecf Removing intermediate container 5a8fc99a3ecf ---> 694d5063e8e6 Step 6/16 : COPY ["amazing-app.csproj", "amazing-app/"] ---> 450921f630c3 Step 7/16 : RUN dotnet restore "amazing-app/amazing-app.csproj" ---> Running in ddb564e875be Restore completed in 1.81 sec for /src/amazing-app/amazing-app.csproj. Removing intermediate container ddb564e875be ---> b59e0c1dfb4d Step 8/16 : COPY . . ---> 695977f3b543 Step 9/16 : WORKDIR "/src/amazing-app" ---> Running in aa5575c99ce3 Removing intermediate container aa5575c99ce3 ---> 781b4c552434 Step 10/16 : RUN dotnet build "amazing-app.csproj" -c Release -o /app/build ---> Running in 3a602c34b5a9 Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 36.78 ms for /src/amazing-app/amazing-app.csproj. CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/amazing-app/amazing-app.csproj] Build FAILED. CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/amazing-app/amazing-app.csproj] 0 Warning(s) 1 Error(s) Time Elapsed 00:00:02.37 The command '/bin/sh -c dotnet build "amazing-app.csproj" -c Release -o /app/build' returned a non-zero code: 1

find the source code on github link

am I missing something here? any help much appreciated.

-- CodeRunner
.net
docker
dockerfile
kubernetes

1 Answer

12/23/2019

The issue is in folder structure. You copied your source to /src folder (see line 8 and 11 of Dockerfile), but *.csproj file is copied to amazing-app subfolder.

You can check it by running "intermediate" image, eg: 781b4c552434 (it is a hash of image before crash), like following: docker run -it --rm --name xxx b5a968c1103c bash and then you can inspect file system by ls command to see that source code and csproj file are located in different places.

So, i suggest to move Dockerfile to root directory (close to .dockerignore) and update path to csproj file (also, you need to run docker build command from root directory. This Dockerfile should work:

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

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "amazing-app.dll"]
-- Exploding Kitten
Source: StackOverflow