运行 dockerfile 时 dotnet restore 找不到文件夹

dotnet restore not finding folder while running dockerfile

这是一个非常简单的 API 我用 .NET 6 和 ASP.NET 核心创建的,我正在尝试 docker 化。

我在 运行ning docker 构建命令时总是遇到同样的错误。 这是我的 docker 文件:

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

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore "MyAPI/MyAPI.csproj" //this is where the build stops


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


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

这是我遇到的错误:

=> ERROR [build-env 4/6] RUN dotnet restore                                                                                                               0.7s 
------
 > [build-env 4/6] RUN dotnet restore:
#11 0.676 MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
------
executor failed running [/bin/sh -c dotnet restore]: exit code: 1

我想问题是路径问题,我已经尝试将路径传递到我的文件夹,但它一直给我同样的错误。

如果我尝试在终端中单独 运行 dotnet restore 命令,它会告诉我路径是正确的。

可能是什么问题?

请检查您的PC文件系统中路径yourSolutionRootFolder/MyAPI/MyAPI.csproj

中是否确实存在csproj文件

你的路径可能有错字? Upper/lower 大小写错误?

COPY *.csproj ./ 找不到要复制的任何内容,因为您的 .csproj 文件位于 MyAPI/ 目录中。

将语句更改为

COPY MyAPI/MyAPI.csproj MyAPI/

复制其余文件并发布项目的位置也应更改为

COPY . ./
RUN dotnet publish -c Release -o out MyAPI/MyAPI.csproj