如何在 Docker 中构建 dotnet 核心?
How to build dotnet core in Docker?
您好,我正在尝试使用 docker 构建我的 dotnet core 2.1 应用程序。每当我创建项目模板默认 docker 文件生成。这个 docker 文件 运行 很好,但是每当我想将它上传到 ecr 时,这将不起作用。所以我更改了 docker 文件如下。
FROM microsoft/dotnet:2.1-sdk AS build
ENV ASPNETCORE_URLS http://*:44319
EXPOSE 44319
WORKDIR /src
COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
WORKDIR /app/LocationServicesAPI
COPY . .
RUN dotnet build "LocationServicesAPI.csproj" -c Release -o /app
每当我 运行 使用 Docker 时,我都会收到以下错误。
1>Step 9/19 : EXPOSE 44319
1> ---> Using cache
1> ---> 069a0777f156
1>Step 10/19 : WORKDIR /src
1> ---> Using cache
1> ---> 6e9768b88723
1>Step 11/19 : COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
1> ---> Using cache
1> ---> 37b9e63b9b97
1>Step 12/19 : RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
1> ---> Using cache
1>Step 13/19 : WORKDIR /app/LocationServicesAPI
1> ---> f505d07f4d8c
1> ---> Using cache
1> ---> e03aaf3a0d7d
1>Step 14/19 : COPY . .
1> ---> 20b8bb0d74bd
1>Step 15/19 : RUN dotnet build "LocationServicesAPI.csproj" -c Release -o /app
1> ---> Running in f04182972995
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core
1>MSBUILD : error MSB1009: Project file does not exist.
1>Switch: LocationServicesAPI.csproj
1>Removing intermediate container f04182972995
1>The command '/bin/sh -c dotnet build "LocationServicesAPI.csproj" -c Release -o /app' returned a non-zero code: 1
1>C:\Users\ngodbole\Documents\MerchWebServices\LocationServicesAPI\LocationServicesAPI\Dockerfile : error CTC1014: Docker command failed with exit code 1.
1>C:\Users\ngodbole\Documents\MerchWebServices\LocationServicesAPI\LocationServicesAPI\Dockerfile : error CTC1014: The command '/bin/sh -c dotnet build "LocationServicesAPI.csproj" -c Release -o /app' returned a non-zero code: 1
1>Done building project "LocationServicesAPI.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
修正路径后出现以下错误。
1> ---> cc58805dac5d
1>Step 14/19 : COPY . .
1> ---> ced094b3788d
1>Step 15/19 : RUN dotnet build LocationServicesAPI.csproj -c Release -o /app
1> ---> Running in 290429a9f4d1
1>Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>Switch: LocationServicesAPI.csproj
1>MSBUILD : error MSB1009: Project file does not exist.
我想不通。有人可以帮我解决这个问题吗?任何帮助,将不胜感激。谢谢
在您的 Dockerfile
中,您将代码复制到错误的文件夹中:
WORKDIR /src
COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
WORKDIR /app/LocationServicesAPI
COPY . .
RUN dotnet build "LocationServicesAPI.csproj" -c Release -o /app
第二个WORKDIR
命令需要是:
WORKDIR /src/LocationServicesAPI
这将确保您将源代码复制到与 .csproj 相同的文件夹中
编辑
您需要将WORKDIR /src/LocationServicesAPI
移动到COPY
命令之后,以便build
命令与.csproj
[=19=在同一文件夹中执行]
WORKDIR /src
COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
COPY . .
WORKDIR /src/LocationServicesAPI
RUN dotnet build "LocationServicesAPI.csproj" -c Release -o /app
您好,我正在尝试使用 docker 构建我的 dotnet core 2.1 应用程序。每当我创建项目模板默认 docker 文件生成。这个 docker 文件 运行 很好,但是每当我想将它上传到 ecr 时,这将不起作用。所以我更改了 docker 文件如下。
FROM microsoft/dotnet:2.1-sdk AS build
ENV ASPNETCORE_URLS http://*:44319
EXPOSE 44319
WORKDIR /src
COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
WORKDIR /app/LocationServicesAPI
COPY . .
RUN dotnet build "LocationServicesAPI.csproj" -c Release -o /app
每当我 运行 使用 Docker 时,我都会收到以下错误。
1>Step 9/19 : EXPOSE 44319
1> ---> Using cache
1> ---> 069a0777f156
1>Step 10/19 : WORKDIR /src
1> ---> Using cache
1> ---> 6e9768b88723
1>Step 11/19 : COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
1> ---> Using cache
1> ---> 37b9e63b9b97
1>Step 12/19 : RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
1> ---> Using cache
1>Step 13/19 : WORKDIR /app/LocationServicesAPI
1> ---> f505d07f4d8c
1> ---> Using cache
1> ---> e03aaf3a0d7d
1>Step 14/19 : COPY . .
1> ---> 20b8bb0d74bd
1>Step 15/19 : RUN dotnet build "LocationServicesAPI.csproj" -c Release -o /app
1> ---> Running in f04182972995
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core
1>MSBUILD : error MSB1009: Project file does not exist.
1>Switch: LocationServicesAPI.csproj
1>Removing intermediate container f04182972995
1>The command '/bin/sh -c dotnet build "LocationServicesAPI.csproj" -c Release -o /app' returned a non-zero code: 1
1>C:\Users\ngodbole\Documents\MerchWebServices\LocationServicesAPI\LocationServicesAPI\Dockerfile : error CTC1014: Docker command failed with exit code 1.
1>C:\Users\ngodbole\Documents\MerchWebServices\LocationServicesAPI\LocationServicesAPI\Dockerfile : error CTC1014: The command '/bin/sh -c dotnet build "LocationServicesAPI.csproj" -c Release -o /app' returned a non-zero code: 1
1>Done building project "LocationServicesAPI.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
修正路径后出现以下错误。
1> ---> cc58805dac5d
1>Step 14/19 : COPY . .
1> ---> ced094b3788d
1>Step 15/19 : RUN dotnet build LocationServicesAPI.csproj -c Release -o /app
1> ---> Running in 290429a9f4d1
1>Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>Switch: LocationServicesAPI.csproj
1>MSBUILD : error MSB1009: Project file does not exist.
我想不通。有人可以帮我解决这个问题吗?任何帮助,将不胜感激。谢谢
在您的 Dockerfile
中,您将代码复制到错误的文件夹中:
WORKDIR /src
COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
WORKDIR /app/LocationServicesAPI
COPY . .
RUN dotnet build "LocationServicesAPI.csproj" -c Release -o /app
第二个WORKDIR
命令需要是:
WORKDIR /src/LocationServicesAPI
这将确保您将源代码复制到与 .csproj 相同的文件夹中
编辑
您需要将WORKDIR /src/LocationServicesAPI
移动到COPY
命令之后,以便build
命令与.csproj
[=19=在同一文件夹中执行]
WORKDIR /src
COPY ["LocationServicesAPI/LocationServicesAPI.csproj", "LocationServicesAPI/"]
RUN dotnet restore "LocationServicesAPI/LocationServicesAPI.csproj"
COPY . .
WORKDIR /src/LocationServicesAPI
RUN dotnet build "LocationServicesAPI.csproj" -c Release -o /app