如何在Docker中构建dotnet核心?

问题描述 投票:0回答:1

[嗨,我正在尝试使用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.

我无法弄清楚。有人可以帮我解决这个问题吗?任何帮助,将不胜感激。谢谢

docker .net-core docker-build
1个回答
1
投票

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命令

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
© www.soinside.com 2019 - 2024. All rights reserved.