ASP.NET Core项目Docker构建失败:dotnet publish执行出错
解决Docker Compose部署.NET服务时的NETSDK1152错误
我想把Kafka、Zookeeper和两个.NET服务部署到不同容器里学习消息代理机制,但执行docker-compose up -d时失败,报错如下:
> [reportservice publish 1/1] RUN dotnet publish "./ReportService.csproj" -c Release -o /app/publish /p:UseAppHost=false: 1.013 Determining projects to restore... 1.350 All projects are up-to-date for restore. 2.130 DirectoryService -> /src/DirectoryService/bin/Release/net9.0/DirectoryService.dll 4.383 /src/ReportService/Migrations/20250412141416_initial.cs(9,26): warning CS8981: The type name 'initial' only contains lower-cased ascii characters. Such names may become reserved for the language. [/src/ReportService/ReportService.csproj] 4.383 /src/ReportService/Migrations/20250412141416_initial.Designer.cs(16,19): warning CS8981: The type name 'initial' only contains lower-cased ascii characters. Such names may become reserved for the language. [/src/ReportService/ReportService.csproj] 4.383 /src/ReportService/Models/Report.cs(10,23): warning CS8618: Non-nullable property 'Location' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable. [/src/ReportService/ReportService.csproj] 4.383 /src/ReportService/Services/ReportService.cs(28,36): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. [/src/ReportService/ReportService.csproj] 4.590 ReportService -> /src/ReportService/bin/Release/net9.0/ReportService.dll 4.802 /usr/share/dotnet/sdk/9.0.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.ConflictResolution.targets(112,5): error NETSDK1152: Found multiple publish output files with the same relative path: /src/DirectoryService/appsettings.Development.json, /src/ReportService/appsettings.Development.json, /src/DirectoryService/appsettings.json, /src/ReportService/appsettings.json. [/src/ReportService/ReportService.csproj] ------ failed to solve: process "/bin/sh -c dotnet publish \"../ReportService.csproj\" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false" did not complete successfully: exit code: 1
我的docker-compose.yml文件
version: '3.8' services: zookeeper: image: confluentinc/cp-zookeeper:7.5.0 container_name: zookeeper ports: - "2181:2181" environment: ZOOKEEPER_CLIENT_PORT: 2181 ZOOKEEPER_TICK_TIME: 2000 networks: - app-network kafka: image: confluentinc/cp-kafka:7.5.0 container_name: kafka ports: - "9092:9092" environment: KAFKA_BROKER_ID: 1 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092 KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1 KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 depends_on: - zookeeper networks: - app-network directoryservice: build: context: . dockerfile: DirectoryService/Dockerfile container_name: directoryservice ports: - "5000:80" environment: - ASPNETCORE_ENVIRONMENT=Development - KAFKA_BOOTSTRAP_SERVERS=kafka:9092 depends_on: - kafka networks: - app-network restart: always reportservice: build: context: . dockerfile: ReportService/Dockerfile container_name: reportservice ports: - "5001:80" environment: - ASPNETCORE_ENVIRONMENT=Development - KAFKA_BOOTSTRAP_SERVERS=kafka:9092 depends_on: - kafka networks: - app-network restart: always networks: app-network: driver: bridge
两个项目的Dockerfile
ReportService的Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base USER $APP_UID WORKDIR /app EXPOSE 8080 EXPOSE 8081 FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["ReportService/ReportService.csproj", "ReportService/"] RUN dotnet restore "./ReportService/ReportService.csproj" COPY . . WORKDIR "/src/ReportService" RUN dotnet build "./ReportService.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./ReportService.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY ReportService/appsettings.json /app/ COPY ReportService/appsettings.Development.json /app/ COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "ReportService.dll"]
DirectoryService的Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base USER $APP_UID WORKDIR /app EXPOSE 8080 EXPOSE 8081 FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["DirectoryService/DirectoryService.csproj", "DirectoryService/"] RUN dotnet restore "./DirectoryService/DirectoryService.csproj" COPY . . WORKDIR "/src/DirectoryService" RUN dotnet build "./DirectoryService.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./DirectoryService.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY DirectoryService/appsettings.json /app/ COPY DirectoryService/appsettings.Development.json /app/ COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DirectoryService.dll"]
解决方案
错误根源是NETSDK1152:两个项目的appsettings.json和appsettings.Development.json文件名相同,Docker构建时因docker-compose的context设为根目录(.),且Dockerfile执行了COPY . .,导致两个项目的配置文件都被复制到/src目录下。dotnet publish时SDK扫描到同名文件,判定为冲突。
以下是三种可行修复方法:
方法1:调整Docker构建上下文(推荐)
修改docker-compose.yml中每个服务的context,指向各自项目目录,构建时仅包含当前项目文件:
# directoryservice部分修改 directoryservice: build: context: ./DirectoryService dockerfile: Dockerfile # 其他配置不变 # reportservice部分修改 reportservice: build: context: ./ReportService dockerfile: Dockerfile # 其他配置不变
同时修改两个项目的Dockerfile,移除项目目录前缀(因context已指向项目目录):
ReportService的Dockerfile修改后
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base USER $APP_UID WORKDIR /app EXPOSE 8080 EXPOSE 8081 FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["ReportService.csproj", "./"] RUN dotnet restore "./ReportService.csproj" COPY . . RUN dotnet build "./ReportService.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./ReportService.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY appsettings.json /app/ COPY appsettings.Development.json /app/ COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "ReportService.dll"]
DirectoryService的Dockerfile修改后
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base USER $APP_UID WORKDIR /app EXPOSE 8080 EXPOSE 8081 FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["DirectoryService.csproj", "./"] RUN dotnet restore "./DirectoryService.csproj" COPY . . RUN dotnet build "./DirectoryService.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./DirectoryService.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY appsettings.json /app/ COPY appsettings.Development.json /app/ COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DirectoryService.dll"]
方法2:在项目文件中配置冲突解决策略
在每个项目的.csproj文件中添加配置,让SDK忽略同名文件冲突:
<PropertyGroup> <PublishConflictBehavior>AcceptExternal</PublishConflictBehavior> </PropertyGroup>
或严格排除其他项目的配置文件:
<ItemGroup> <Content Remove="../**/appsettings*.json" /> <None Include="../**/appsettings*.json" /> </ItemGroup>
方法3:修改Dockerfile,避免复制整个根目录
在Dockerfile的build阶段,替换COPY . .为仅复制当前项目文件:
# ReportService的Dockerfile中替换COPY . . COPY ["ReportService/", "./ReportService/"]
这样只会复制ReportService目录下的文件,不会引入DirectoryService的配置文件。
内容的提问来源于stack exchange,提问作者Umut Konmus
相关产品推荐
相关产品推荐

