windows 从Dockerfile构建docker镜像时出错

dffbzjpn  于 2023-04-07  发布在  Windows
关注(0)|答案(1)|浏览(170)

我有一个包含两个Web服务的解决方案。这个解决方案中的所有项目都设置了net6.0目标框架。本地构建通过,没有任何错误。我试图从dockerfile构建一个docker镜像,并在dotnet build stage上获得了很多CS0246错误。
我的dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS env
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY . .
RUN dotnet restore

COPY . .
RUN dotnet build -c Debug -o /app/build --no-restore

FROM build AS publish
RUN dotnet publish "My.Api/My.Api.csproj" -c Debug -o /app/publish --no-build --no-restore

FROM env AS final
WORKDIR /app
COPY --from=build /app/publish ./
EXPOSE 5000
ENV ASPNETCORE_URLS=https://+:5000
ENV ASPNETCORE_ENVIRONMENT=Development
ENTRYPOINT ["dotnet", "My.Api.dll"]

我从解决方案目录调用的命令:docker build . -t my.api:1.0.0 -t my.api:lastest -f ./My.Api/Dockerfile --no-cache
下面是该命令的部分输出:

[+] Building 45.9s (14/15)
[internal] load build definition from Dockerfile
transferring dockerfile: 618B
[internal] load .dockerignore
transferring context: 35B
[internal] load metadata for mcr.microsoft.com/dotnet/aspnet:6.0
[internal] load metadata for mcr.microsoft.com/dotnet/sdk:6.0
[build 1/6] FROM mcr.microsoft.com/dotnet/sdk:6.0
[env 1/2] FROM mcr.microsoft.com/dotnet/aspnet:6.0
[internal] load build context
transferring context: 11.35kB
CACHED [build 2/6] WORKDIR /src
CACHED [env 2/2] WORKDIR /app
CACHED [final 1/2] WORKDIR /app
[build 3/6] COPY . .
[build 4/6] RUN dotnet restore
[build 5/6] COPY . .
ERROR [build 6/6] RUN dotnet build -c Debug -o /app/build --no-restore
------
 > [build 6/6] RUN dotnet build -c Debug -o /app/build --no-restore:
#14 0.700 MSBuild version 17.3.2+561848881 for .NET
#14 4.307 /src/Localization/LocalizationAttribute.cs(6,47): error CS0246: The type or namespace name 'Attribute' could not be found (are you missing a using directive or an assembly reference?) [/src/Localization/Localization.csproj]
#14 4.307 /src/Localization/LocalizationAttribute.cs(5,2): error CS0246: The type or namespace name 'AttributeUsageAttribute' could not be found (are you missing a using directive or an assembly reference?) [/src/Localization/Localization.csproj]
#14 4.307 /src/Localization/LocalizationAttribute.cs(5,2): error CS0246: The type or namespace name 'AttributeUsage' could not be found (are you missing a using directive or an assembly reference?) [/src/Localization/Localization.csproj]
#14 4.307 /src/Localization/LocalizationAttribute.cs(5,17): error CS0103: The name 'AttributeTargets' does not exist in the current context [/src/Localization/Localization.csproj]
#14 4.307 /src/Localization/LocalizationAttribute.cs(8,22): error CS0246: The type or namespace name 'Type' could not be found (are you missing a using directive or an assembly reference?) [/src/Localization/Localization.csproj]
#14 4.307 /src/Localization/LocalizationAttribute.cs(10,57): error CS0246: The type or namespace name 'Type' could not be found (are you missing a using directive or an assembly reference?) [/src/Localization/Localization.csproj]
#14 4.307 /src/Localization/LocalizationAttribute.cs(18,13): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. [/src/Localization/Localization.csproj]
#14 4.315 /src/Common/Extensions/CollectionExtensions.cs(5,40): error CS0246: The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?) [/src/Common/Common.csproj]
#14 4.315 /src/Common/Extensions/CollectionExtensions.cs(12,40): error CS0246: The type or namespace name 'ICollection<>' could not be found (are you missing a using directive or an assembly reference?) [/src/Common/Common.csproj]
#14 4.315 /src/Common/Extensions/CollectionExtensions.cs(14,43): error CS0246: The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?) [/src/Common/Common.csproj]
#14 4.315 /src/Common/Extensions/StringExtensions.cs(7,70): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. [/src/Common/Common.csproj]
#14 5.176
#14 5.176 Build FAILED.
#14 5.17

我试着:

  • 将dockerfile放到solution文件夹中
  • 使用和不使用--no-cache参数运行
  • 只运行COPY命令并搜索容器中的文件。它们都在那里
3j86kqsm

3j86kqsm1#

我找到了解决方法,docker dotnet环境中有一个bug或者smth,它不能在ImplicitUsings上工作。
删除该标签并手动添加用法使其工作。全局用法也工作。

相关问题