从我的项目文件创建新的Docker镜像时出现错误

5
在我的 Windows 10 机器上,当我尝试创建一个 Docker 镜像来打包我的解决方案/项目时,它会给我以下错误(这是完整日志,错误出现在最后)。
Sending build context to Docker daemon  1.363MB
Step 1/20 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
---> bbfbcd874370
Step 2/20 : WORKDIR /app
---> Using cache
---> 6aa13d5c0c78
Step 3/20 : EXPOSE 80
 ---> Using cache
 ---> 80df41e66b51
Step 4/20 : EXPOSE 443
 ---> Using cache
 ---> e6b48dd0ebe8
Step 5/20 : FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
 ---> 83e4b70d6dc1
Step 6/20 : ARG BuildConfiguration=Release
 ---> Using cache
 ---> 1f78c91d39f1
Step 7/20 : WORKDIR /src
 ---> Using cache
 ---> 213aa5f648e4
Step 8/20 : COPY ["NuGet.config", "."]
 ---> Using cache
 ---> d8843fccc7af
Step 9/20 : COPY ["MyProj.csproj", "ServiceProject/"]
 ---> Using cache
 ---> af245be6312e
Step 10/20 : RUN dotnet restore  --configfile NuGet.config "MyProj.csproj"
 ---> Running in 0582b332eab7
MSBUILD : error MSB1009: Project file does not exist.
Switch: MyProj.csproj
The command '/bin/sh -c dotnet restore  --configfile NuGet.config "MyProj.csproj"' returned a non-zero code: 1

Dockerfile 如下所示

FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
ARG BuildConfiguration=Release
WORKDIR /src
COPY ["NuGet.config", "."]
COPY ["MyProj.csproj", "NewProj/"]
RUN dotnet restore  --configfile NuGet.config "MyProj.csproj"
COPY . .
WORKDIR "/src/MyService"
RUN dotnet build "MyProj.csproj" -c ${BuildConfiguration} -o /app --no-restore

FROM build AS publish
ARG BuildConfiguration=Release
RUN dotnet publish "MyProj.csproj" -c ${BuildConfiguration} -o /app --no-restore

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

文件 MyProj.csproj 存在于该文件夹中。 是的,我正在以管理员模式运行powershell。 我还尝试了提供完整路径,但也没有用。

我已经查看了Docker文档和Stackoverflow,但没有提到这个错误。如有可能,请提供逐步解决方案。谢谢。


我认为你正在将“MyProj.csproj”复制到“NewProj/”文件夹中,但是然后尝试将其用作在“.”中一样,就像你的nuget.config一样。 - JleruOHeP
谢谢,我尝试将csproj文件复制到“。”现在它正在给出以下错误: /usr/share/dotnet/sdk/2.1.801/NuGet.targets(525,5): error : 值不能为空或为空字符串。[/src/MyProj.csproj] /usr/share/dotnet/sdk/2.1.801/NuGet.targets(525,5): error : 参数名称:用户名[/src/MyProj.csproj] - Karan
这意味着您的NuGet Feed需要用户名/密码身份验证。 您可以将它们添加到您的nuget.config文件中(https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#package-source-sections)。 - JleruOHeP
1个回答

3

作为回答,你最初的问题在于 csproj 文件的位置。你运行:

COPY ["NuGet.config", "."]
COPY ["MyProj.csproj", "NewProj/"]

因此,MyProj.csproj位于“NewProj/”目录下。因此,当您运行时:
RUN dotnet restore  --configfile NuGet.config "MyProj.csproj"

无法找到MyProj项目。您可以选择提供正确的项目文件路径或将其复制到与 nuget.config 相同的位置。

至于以下错误 - 似乎

/usr/share/dotnet/sdk/2.1.801/NuGet.targets(525,5): error : 值不能为空或为空字符串。[/src/MyProj.csproj]

/usr/share/dotnet/sdk/2.1.801/NuGet.targets(525,5): error : 参数名称:username [/src/MyProj.csproj]

"username"是访问您正在使用的nuget源之一所需的。尝试在您的nuget.config文件中添加用户名/密码 - https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#package-source-sections


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接