【问题标题】:How to build dotnet core in Docker?如何在 Docker 中构建 dotnet 核心?
【发布时间】:2020-02-18 04:01:00
【问题描述】:

您好,我正在尝试使用 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】:

    在您的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
    

    【讨论】:

    • 谢谢我错过了。我改变了它。但现在我开始遇到一些不同的错误。我会把它贴在上面。
    • @Niranjan 你有什么解决办法吗??
    猜你喜欢
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    相关资源
    最近更新 更多