Search code examples
docker.net-coredocker-build

How to build dotnet core in Docker?


Hi I am trying to build my dotnet core 2.1 app using docker. Whenever i create project template default docker file gets generated. This docker file runs fine but whenever I want to upload it to ecr this will not work. So I changed docker file as below.

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

Whenever I run using Docker I get the below error.

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 ==========

Below error I am getting after correcting the path.

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.

I am not able to figure it out. Can someone help me to fix this? Any help would be appreciated. Thanks


Solution

  • In your Dockerfile you are copying the code into the wrong folder:

    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
    

    The second WORKDIR command needs to be:

    WORKDIR /src/LocationServicesAPI
    

    This will make sure you copy the source code into the same folder as the .csproj

    EDIT

    You need to move the WORKDIR /src/LocationServicesAPI to after the COPY command so that the build command is executed in the same folder as the .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