英文:
.net core - build error with docker compose
问题
我尝试使用Visual Studio从我的后端项目创建一个发布版本,但是我收到以下错误消息:
Service 'backend_api' failed to build: The command '/bin/sh -c dotnet build ...
使用"docker-compose build"命令也会出现相同的错误。
在Visual Studio的调试模式下可以正常工作。
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["backendAPI/backendAPI.csproj", "backendAPI/"]
RUN dotnet restore "backendAPI/backendAPI.csproj"
COPY . .
WORKDIR "/src/backend_API"
RUN dotnet build "backendAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "backendAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "backendAPI.dll"]
docker-compose.yml
version: '3.7'
services:
backend_sql:
image: mcr.microsoft.com/mssql/server:2017-GA-ubuntu
container_name: testsql
networks:
- test_network
ports:
- "1440:1433"
volumes:
- testsqldata:/var/opt/mssql
backend_api:
image: ${DOCKER_REGISTRY-}backendapi
container_name: testapi
build:
context: .
dockerfile: backendAPI/Dockerfile
networks:
- test_network
depends_on:
- backend_sql
ports:
- "5000:80"
networks:
test_network:
external:
name: test_network
volumes:
testsqldata:
driver: local
name: testsqldata
错误消息:
...
2>Step 7/16 : RUN dotnet restore "backendAPI/backendAPI.csproj"
2> ---> Running in ee20ee70cedf
2> Restore completed in 41.8 sec for /src/backendAPI/backendAPI.csproj.
2>Removing intermediate container ee20ee70cedf
2> ---> 6cafbb7f9daf
2>Step 8/16 : COPY . .
2> ---> 74dc0dfe8145
2>Step 9/16 : WORKDIR "/src/backend_API"
2> ---> Running in e198c3ffb356
2>Removing intermediate container e198c3ffb356
2> ---> 114bfbba3151
2>Step 10/16 : RUN dotnet build "backendAPI.csproj" -c Release -o /app/build
2> ---> Running in 3e7bf02253ce
2>Microsoft (R) Build Engine version 16.3.2+e481bbf88 for .NET Core
2>Copyright (C) Microsoft Corporation. All rights reserved.
2>MSBUILD : error MSB1009: Project file does not exist.
2>Switch: backendAPI.csproj
2>Service 'backend_api' failed to build: The command '/bin/sh -c dotnet build "backendAPI.csproj" -c Release -o /app/build' returned a non-zero code: 1
2>C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets(492,5): error DT1001: Service 'backend_api' failed to build: The command '/bin/sh -c dotnet build "backendAPI.csproj" -c Release -o /app/build' returned a non-zero code: 1
2>Done building project "docker-compose.dcproj" -- FAILED.
英文:
I try to create a release build from my backend project with the Visual Studio, but I get the error message
Service 'backend_api' failed to build: The command '/bin/sh -c dotnet build ...
With "docker-compose build" I get the same error.
In Debug-Mode of the Visual Studio it works.
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["backendAPI/backendAPI.csproj", "backendAPI/"]
RUN dotnet restore "backendAPI/backendAPI.csproj"
COPY . .
WORKDIR "/src/backend_API"
RUN dotnet build "backendAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "backendAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "backendAPI.dll"]
docker-compose.yml
version: '3.7'
services:
backend_sql:
image: mcr.microsoft.com/mssql/server:2017-GA-ubuntu
container_name: testsql
networks:
- test_network
ports:
- "1440:1433"
volumes:
- testsqldata:/var/opt/mssql
backend_api:
image: ${DOCKER_REGISTRY-}backendapi
container_name: testapi
build:
context: .
dockerfile: backendAPI/Dockerfile
networks:
- test_network
depends_on:
- backend_sql
ports:
- "5000:80"
networks:
test_network:
external:
name: test_network
volumes:
testsqldata:
driver: local
name: testsqldata
Error-Message:
...
2>Step 7/16 : RUN dotnet restore "backendAPI/backendAPI.csproj"
2> ---> Running in ee20ee70cedf
2> Restore completed in 41.8 sec for /src/backendAPI/backendAPI.csproj.
2>Removing intermediate container ee20ee70cedf
2> ---> 6cafbb7f9daf
2>Step 8/16 : COPY . .
2> ---> 74dc0dfe8145
2>Step 9/16 : WORKDIR "/src/backend_API"
2> ---> Running in e198c3ffb356
2>Removing intermediate container e198c3ffb356
2> ---> 114bfbba3151
2>Step 10/16 : RUN dotnet build "backendAPI.csproj" -c Release -o /app/build
2> ---> Running in 3e7bf02253ce
2>Microsoft (R) Build Engine version 16.3.2+e481bbf88 for .NET Core
2>Copyright (C) Microsoft Corporation. All rights reserved.
2>MSBUILD : error MSB1009: Project file does not exist.
2>Switch: backendAPI.csproj
2>Service 'backend_api' failed to build: The command '/bin/sh -c dotnet build "backendAPI.csproj" -c Release -o /app/build' returned a non-zero code: 1
2>C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets(492,5): error DT1001: Service 'backend_api' failed to build: The command '/bin/sh -c dotnet build "backendAPI.csproj" -c Release -o /app/build' returned a non-zero code: 1
2>Done building project "docker-compose.dcproj" -- FAILED.
答案1
得分: 2
"error MSB1009: 项目文件不存在":
这个错误表明当前位置没有名为 *.proj
的文件。这是因为你拼写路径不正确:它应该是 backendAPI
而不是 backend_API
。要解决这个问题,请按照以下方式更改你的代码:
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build WORKDIR /src COPY ["backendAPI/backendAPI.csproj", "backendAPI/"] RUN dotnet restore "backendAPI/backendAPI.csproj" COPY . .WORKDIR "/src/backend_API"WORKDIR "/src/backendAPI" RUN dotnet build "backendAPI.csproj" -c Release -o /app/build
英文:
> "error MSB1009: Project file does not exist":
This error indicates that there's no such a *.proj
file in current location. That happens because you spelled the path incorrectly: it should be backendAPI
instead of backend_API
. To fix that issue, change your code as below:
<pre>
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["backendAPI/backendAPI.csproj", <b>"backendAPI/"</b>]
RUN dotnet restore "backendAPI/backendAPI.csproj"
COPY . .
<strike><b>WORKDIR "/src/backend_API"</b></strike>
<b>WORKDIR "/src/backendAPI"</b>
RUN dotnet build "backendAPI.csproj" -c Release -o /app/build
</pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论