Error in dotnet restore in executing a docker build in project .net 6 use layers

Viewed 789

I'm trying executing a "docker build" in my application .Net 6.0, but I receive an error in Dotnet restore in Dockerfile. The application executing normally local, not any error.

Docker command:

docker build -t aspnetcore-docker-image .

Error in terminal:

=> ERROR [build 7/9] RUN dotnet restore ./DevFreela.API/DevFreela.API.csproj                                              0.2s
------
 > [build 7/9] RUN dotnet restore ./DevFreela.API/DevFreela.API.csproj:
#11 0.185 Could not execute because the application was not found or a compatible .NET SDK is not installed.
#11 0.185 Possible reasons for this include:
#11 0.185   * You intended to execute a .NET program:
#11 0.185       The application 'restore' does not exist.
#11 0.185   * You intended to execute a .NET SDK command:
#11 0.185       It was not possible to find any installed .NET SDKs.
#11 0.185       Install a .NET SDK from:
#11 0.185         https://aka.ms/dotnet-download
------
executor failed running [/bin/sh -c dotnet restore ./DevFreela.API/DevFreela.API.csproj]: exit code: 145

My Dockerfile

# .NET Core SDK
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS build

# Sets the working directory
WORKDIR /app

# Copy Projects
#COPY *.sln .
COPY Src/DevFreela.API/DevFreela.API.csproj ./DevFreela.API/
COPY Src/DevFreela.Application/DevFreela.Application.csproj ./DevFreela.Application/
COPY Src/DevFreela.Core/DevFreela.Core.csproj ./DevFreela.Core/
COPY Src/DevFreela.Infrastructure/DevFreela.Infrastructure.csproj ./DevFreela.Infrastructure/

# .NET Core Restore
RUN dotnet restore ./DevFreela.API/DevFreela.API.csproj

# Copy All Files
COPY Src ./

# .NET Core Build and Publish
RUN dotnet publish ./DevFreela.Api/DevFreela.Api.csproj -c Release -o /publish

# ASP.NET Core Runtime
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY --from=build /publish ./

EXPOSE 80 5195 7066
ENV ASPNETCORE_URLS=http://+:5195;https://+:7066

ENTRYPOINT ["dotnet", "DevFreela.API.dll"]

Project structure:

enter image description here

Complete terminal log:

=> [internal] load build definition from Dockerfile                                                                       0.0s
 => => transferring dockerfile: 938B                                                                                       0.0s
 => [internal] load .dockerignore                                                                                          0.0s
 => => transferring context: 35B                                                                                           0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:6.0                                                       0.6s
 => [internal] load build context                                                                                          0.0s
 => => transferring context: 13.14kB                                                                                       0.0s
 => [runtime 1/3] FROM mcr.microsoft.com/dotnet/aspnet:6.0@sha256:26ef9dc4aa354cc4aa4ae533c97f92d0d72c5e848f6968660be51d9  0.0s
 => CACHED [runtime 2/3] WORKDIR /app                                                                                      0.0s
 => CACHED [build 3/9] COPY Src/DevFreela.API/DevFreela.API.csproj ./DevFreela.API/                                        0.0s
 => CACHED [build 4/9] COPY Src/DevFreela.Application/DevFreela.Application.csproj ./DevFreela.Application/                0.0s
 => CACHED [build 5/9] COPY Src/DevFreela.Core/DevFreela.Core.csproj ./DevFreela.Core/                                     0.0s
 => CACHED [build 6/9] COPY Src/DevFreela.Infrastructure/DevFreela.Infrastructure.csproj ./DevFreela.Infrastructure/       0.0s
 => ERROR [build 7/9] RUN dotnet restore ./DevFreela.API/DevFreela.API.csproj                                              0.2s
------
 > [build 7/9] RUN dotnet restore ./DevFreela.API/DevFreela.API.csproj:
#11 0.185 Could not execute because the application was not found or a compatible .NET SDK is not installed.
#11 0.185 Possible reasons for this include:
#11 0.185   * You intended to execute a .NET program:
#11 0.185       The application 'restore' does not exist.
#11 0.185   * You intended to execute a .NET SDK command:
#11 0.185       It was not possible to find any installed .NET SDKs.
#11 0.185       Install a .NET SDK from:
#11 0.185         https://aka.ms/dotnet-download
------
executor failed running [/bin/sh -c dotnet restore ./DevFreela.API/DevFreela.API.csproj]: exit code: 145
1 Answers

You're using the aspnet image which doesn't contain the SDK, so you can't build using that. You need the sdk image for the first part of your Dockerfile like this

# .NET Core SDK
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

# Sets the working directory
WORKDIR /app

# Copy Projects
#COPY *.sln .
COPY Src/DevFreela.API/DevFreela.API.csproj ./DevFreela.API/
COPY Src/DevFreela.Application/DevFreela.Application.csproj ./DevFreela.Application/
COPY Src/DevFreela.Core/DevFreela.Core.csproj ./DevFreela.Core/
COPY Src/DevFreela.Infrastructure/DevFreela.Infrastructure.csproj ./DevFreela.Infrastructure/

# .NET Core Restore
RUN dotnet restore ./DevFreela.API/DevFreela.API.csproj

# Copy All Files
COPY Src ./

# .NET Core Build and Publish
RUN dotnet publish ./DevFreela.Api/DevFreela.Api.csproj -c Release -o /publish

# ASP.NET Core Runtime
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY --from=build /publish ./

EXPOSE 80 5195 7066
ENV ASPNETCORE_URLS=http://+:5195;https://+:7066

ENTRYPOINT ["dotnet", "DevFreela.API.dll"]
Related