简体   繁体   中英

Cannot build docker image (ASP.NET Core )

I have asp.net core solution, that contains 3 projects.

I want to deploy it to the docker container.

Here is how the solution looks like

在此处输入图像描述

Here is my Dockerfile

 FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "tooseeweb.dll"]

But when I run docker build I got this in console

Skipping project "/TooSeeWeb.Core/TooSeeWeb.Core.csproj" because it was not found.

Skipping project "/TooSeeWeb.Infrastructure/TooSeeWeb.Infrastructure.csproj" because it was not found.

MSBUILD: error MSB1009: Project file does not exist. Switch: TooSeeWeb.csproj

Error: ResponseItem.ErrorDetail[code=1,message=The command '/bin/sh -c dotnet build "TooSeeWeb.csproj" -c Release -o /app' returned a non-zero code: 1] Failed to deploy ' Dockerfile: TooSeeWeb/Dockerfile': The command '/bin/sh -c dotnet build "TooSeeWeb.csproj" -c Release -o /app' returned a non-zero code: 1

Where can be a problem?

UPDATE

Structure of folders of my solution

TooSeeWeb
 |- aspnet-core(folder)
    |-TooSeeWeb (folder)
      |- TooSeeWeb.sln
      |- TooSeeWeb
         |-Dockerfile
         |- TooSeeWeb.csproj
      |- TooSeeWeb.Core(folder)
         |- TooSeeWeb.Core.csproj
      |- TooSeeWeb.Infrastructure(folder)
         |- TooSeeWeb.Infrastructure.csproj

UPDATE2

I move dockerfile in the same folder with sln (it's one folder up)

Now my docker file looks like this

    ROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.sln ./
COPY TooSeeWeb/*.csproj ./TooSeeWeb/
COPY TooSeeWeb.Core/*.csproj ./TooSeeWeb.Core/
COPY TooSeeWeb.Infrastructure/.*csproj ./TooSeeWeb.Infrastructure/
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app/TooSeeWeb
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "tooseeweb.dll"]

But I got error

Step 4/14: COPY *.sln./ COPY failed: no source files were specified

The COPY command doesn't reproduce the same folder structure in the target folder, so your.csproj files are all in the same folder and the references are broken. Try adding

COPY TooSeeWeb.Core/*.csproj ./TooSeeWebCore/
COPY TooSeeWeb.Infrastructure/*.csproj ./TooSeeWeb.Infrastructure/

Update:
An example folder structure for a solution, with projects in subfolders

src
 |-my.sln
 |-Dockerfile
 |-Web (folder)
   |-web.csproj
 |-Core (folder)
   |-core.csproj
 |-Infrastructure (folder)
   |-infrastructure.csproj

The Dockerfile would be

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.sln ./
COPY Web/*.csproj ./Web/
COPY Core/*.csproj ./Core/
COPY Infrastructure/.*csproj ./Infrastructure/
RUN dotnet restore

# Copy everything else and build
COPY . ./
WORKDIR /app/Web
RUN dotnet build

Update 2
If there is no solution file then I think you need to dotnet restore each project individually

COPY Web/*.csproj ./Web/
COPY Core/*.csproj ./Core/
COPY Infrastructure/.*csproj ./Infrastructure/
RUN dotnet restore ./Web/Web.csproj
RUN dotnet restore ./Core/Core.csproj
RUN dotnet restore ./Infrastructure/Infrastructure.csproj
etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM