1

I'm new to docker, so I would like to ask for help creating a Dockerfile for a .net core application. The application has the following architecture:

   FarmHouse.Services.AuthenticationService
    │   ├───FarmHouse.Services.AuthenticationService.Api
    │   ├───FarmHouse.Services.AuthenticationService.Logic
    │   └───Dockerfile
    │ 
    ├───FarmHouse.Services.Common
    │   └───FarmHouse.Services.Common.Models
    │   
    └───FarmHouse.Services.EmailService
        ├───FarmHouse.Services.EmailService.Api
        ├───FarmHouse.Services.EmailService.Logic
        └───Dockerfile

These three projects (FarmHouse.Services.EmailService, FarmHouse.Services.Common and FarmHouse.Services.AuthenticationService) are in the same solution and FarmHouse.Services.AuthenticationService uses objects from the Common project, i.e. FarmHouse.Services.Common is a dependency for it.

I want other services to link to Common as well. My Dockerfile for AuthenticationService looks like this:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ./*.sln .
COPY ./FarmHouse.Services.AuthenticationService/FarmHouse.Services.AuthenticationService.Logic/FarmHouse.Services.AuthenticationService.Logic.csproj ./FarmHouse.Services.AuthenticationService.Logic/FarmHouse.Services.AuthenticationService.Logic.csproj
COPY ./FarmHouse.Services.AuthenticationService/FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj ./FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj
COPY ./FarmHouse.Services.Common/FarmHouse.Services.Common.Models/FarmHouse.Services.Common.Models.csproj ./FarmHouse.Services.Common.Models/FarmHouse.Services.Common.Models.csproj

RUN dotnet restore

# Copy everything else and build
COPY ./ ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "FarmHouse.Services.AuthenticationService.Api.dll"]

And when I run docker build from the parent folder, I see the following errors:

>docker build -t coreapp -f ./FarmHouse.Services.AuthenticationService/Dockerfile .

#14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.AuthenticationService/FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj" was not found. [/app/FarmHouseServices.sln]417050dbf1abf19fd7c8a7a0eb5d56150a                                                         0.0s
    #14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.AuthenticationService/FarmHouse.Services.AuthenticationService.Logic/FarmHouse.Services.AuthenticationService.Logic.csproj" was not found. [/app/FarmHouseServices.sln]                                                                                       0.0s
    #14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.Common/FarmHouse.Services.Common.Models/FarmHouse.Services.Common.Models.csproj" was not found. [/app/FarmHouseServices.sln]uthenticationService/FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj ./FarmHous  0.0s
    #14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.EmailService/FarmHouse.Services.EmailService.Api/FarmHouse.Services.EmailService.Api.csproj" was not found. [/app/FarmHouseServices.sln]                                                                                                                      1.0s
    #14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.EmailService/FarmHouse.Services.EmailService.Logic/FarmHouse.Services.EmailService.Logic.csproj" was not found. [/app/FarmHouseServices.sln]

Please, advice how to modify a Dockerfile in this case or the architecture. Thanks.

0

1 Answer 1

1

You're getting those errors because you're running dotnet commands at .sln file level, and therefore dotnet tries to restore all the .csproj files in the solution, which haven't been copied.

Try replacing your dotnet restore and publish commands with:

RUN dotnet restore ./FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj

RUN dotnet publish ./FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj -c Release -o out

You can alternatively copy all .csproj files and let them be restored with simply dotnet restore, but I would use the project path for the dotnet publish command anyway.

Sign up to request clarification or add additional context in comments.

4 Comments

When I try to do this, I get the following warning: #17 4.032 /usr/share/dotnet/sdk/3.1.406/Microsoft.Common.CurrentVersion.targets(2084,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "FarmHouse.Services.Common.Models". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [/app/FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj].And then an error stating that the type from the Common project was not found.
You need to replicate whatever folder structure appears in the projects that are referenced in FarmHouse.Services.AuthenticationService.Api.csproj when copying them, or those relative paths won't work. You seem to be missing the FarmHouse.Services.Common part when copying the common project.
Back to the question, since I decided to create pipelines in gitlab. Locally, docker build works well, but gitlab does not: before_script: - apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community nodejs-npm - npm i -g heroku - docker login -u _ -p token registry.heroku.com script: - cd FarmHouseServices - docker build -f ./FarmHouse.Services.AuthenticationService/Dockerfile .
Feel free to create a separated question, it will very difficult to understand the new problem just using comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.