我需要使用 Docker 將 ASP.NET Core 6.0 Web API 容器化。該專案可以在我的這個GitHub Repo中找到。
該專案具有以下結構:
───PlaygroundApiDockerized
│
├───src
│ ├───Playground.API
| | ├───Dockerfile
| | └───Playground.API.csproj
| |
│ └───Playground.Core
| └───Playground.Core.csproj
│
├───test
│ ├───Playground.Tests
│ └───Playground.Tests.csproj
│
├───Playground.sln
└───docker-compose.yml
API 專案依賴于一個名為的簡單 C# 類別庫Playground.Core,并且具有Dockerfile.
Dockerfile 的內容:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Playground.API/Playground.API.csproj", "Playground.API/"]
COPY ["src/Playground.Core/Playground.Core.csproj", "Playground.Core/"]
RUN dotnet restore "src/Playground.API/Playground.API.csproj"
COPY . .
WORKDIR "/src/Playground.API"
RUN dotnet build "Playground.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Playground.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Playground.API.dll"]
該docker-compose檔案如下所示:
version: '3.7'
services:
playground-api:
build:
context: ./
dockerfile: src/Playground.API/Dockerfile
restart: always
ports:
- "7987:80"
我的期望是,當docker-compose up在解決方案根目錄中執行命令時,API 會啟動。
但是該命令會導致以下錯誤。
我知道執行命令的當前作業目錄是docker-compose.yml. 在我的示例中,背景關系是解決方案根。我根本不知道如何在Dockerfile. 有人知道嗎?
Creating network "playgroundapidockerized_default" with the default driver
Building playground-api
[ ] Building 2.2s (11/18)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 720B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:6.0 0.8s
=> [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:6.0 0.0s
=> [internal] load build context 0.3s
=> => transferring context: 13.35MB 0.2s
=> [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:6.0 0.0s
=> [build 1/8] FROM mcr.microsoft.com/dotnet/sdk:6.0@sha256:fde93347d1cc74a03f1804f113ce85add00c6f0af15881181165 0.0s
=> CACHED [build 2/8] WORKDIR /src 0.0s
=> [build 3/8] COPY [src/Playground.API/Playground.API.csproj, Playground.API/] 0.1s
=> [build 4/8] COPY [src/Playground.Core/Playground.Core.csproj, Playground.Core/] 0.0s
=> ERROR [build 5/8] RUN dotnet restore "src/Playground.API/Playground.API.csproj" 0.9s
------
> [build 5/8] RUN dotnet restore "src/Playground.API/Playground.API.csproj":
#13 0.814 MSBUILD : error MSB1009: Project file does not exist.
#13 0.814 Switch: src/Playground.API/Playground.API.csproj
------
executor failed running [/bin/sh -c dotnet restore "src/Playground.API/Playground.API.csproj"]: exit code: 1
ERROR: Service 'playground-api' failed to build : Build failed
uj5u.com熱心網友回復:
當您復制專案檔案時,影像中的當前目錄是/src并且您將其復制到,Playground.API/因此專案檔案最終具有絕對路徑/src/Playground.API/Playground.API.csproj。
然后在運行dotnet restore時使用相對路徑src/Playground.API/Playground.API.csproj。所以它會尋找/src/src/Playground.API/Playground.API.csproj不存在的。
稍后當您復制所有檔案時,COPY . .您會從主機獲得目錄結構,所以現在您的檔案變為/src/src/Playground.API/Playground.API.csprojetc。此時您有 2 個專案檔案的副本。一進/src/Playground.API一進/src/src/Playground.API。
我認為最好的辦法是將主機目錄結構保留在作業目錄下,即/src/src/.... /src為了避免混淆,我會將作業目錄重命名為其他名稱。像這樣的東西
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app
COPY ["src/Playground.API/Playground.API.csproj", "src/Playground.API/"]
COPY ["src/Playground.Core/Playground.Core.csproj", "src/Playground.Core/"]
RUN dotnet restore "src/Playground.API/Playground.API.csproj"
COPY . .
WORKDIR "/app/src/Playground.API"
RUN dotnet build "Playground.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Playground.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Playground.API.dll"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/465592.html
