我有两个web服务的解决方案。该解决方案中的所有项目都设置了net6.0
目标框架。本地构建通过,没有任何错误。我正试图从dockerfile建立一个docker映像,并在dotnet build
阶段得到很多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
I tried to:
- 将dockerfile放入解决方案文件夹
- 带和不带
--no-cache
参数运行 - 只执行
COPY
命令,搜索容器内的文件。它们都在那里
我找到了解决方案。docker。net环境中有一个bug。它只是不工作与ImplicitUsings
。
删除该标签并手动添加使用可以使其工作。全局使用也可以。