13

I am trying to build a docker image from a visual studio solution that consists of two projects:

  • one project contains common code (http methods, logging etc)
  • the other is the business logic and application

I can build and run in Visual Studio, which produces a Docker image that I am able to deploy to our test environment. However, any POSTs sent to the API in the test environment return a 500 error. I can see references like: '/Users/Tim/Documents/Dotnet Code/dotnetcore-app/App/MyClass.cs' in the error response, which leads me to suspect there is something up with the image - that path is on my laptop.

Hoping someone else may have come across something similar, or could provide some pointers about building a multi project solution like this into a Docker container.

Disclaimer: this is my first time working with dotnet core and visual studio - I have some experience with docker.

Miq
  • 3,931
  • 2
  • 18
  • 32
Tim Keegan
  • 133
  • 1
  • 2
  • 5
  • can you share your Dockerfiles? – Miq Apr 09 '18 at 08:24
  • Possible duplicate of [asp.net core 2.0 - multiple projects solution docker file](https://stackoverflow.com/questions/47103570/asp-net-core-2-0-multiple-projects-solution-docker-file) – Rodrigo Werlang Mar 15 '19 at 01:07
  • I've had this situation. Check the way you are referencing the projects: 1) it's possible you are referencing them directly by DLL rather than via a standard `ProjectReference`. 2) Copy statements as in the main answer, may be incorrect. 3) Docker can think it's targeting Windows when it shouldn't... for me, this was due to a corrupt Docker for Windows install. – Kit Dec 02 '19 at 22:34

1 Answers1

28

If you use "standard" Dockerfile template from Visual Studio, you might have issue with having your "common" project being built on your local PC (probably windows) and the "main" project being built in docker container (probably Linux). the "main" dll is referencing windows-built dll, hence the issues.

Make sure you are copying not only main project, but also the common project, so dotnet can build it in docker also.

Try out this dockerfile:

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS http://*:5000
EXPOSE 5000

FROM microsoft/aspnetcore-build:2.0 AS builder
ARG Configuration=Release
WORKDIR /src
COPY *.sln ./
COPY Project.Main/Project.Main.csproj Project.Main/
COPY Project.Common/Project.Common.csproj Project.Common/
RUN dotnet restore
COPY . .
WORKDIR /src/ProjectMain
RUN dotnet build -c $Configuration -o /app

FROM builder AS publish
ARG Configuration=Release
RUN dotnet publish -c $Configuration -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Project.Main.dll"]

Although is to build the "Main" project, it's executed on the sln level.

Miq
  • 3,931
  • 2
  • 18
  • 32
  • 3
    It's working fine but, what about if my solution contains several several web applications. How can I get a deploymet for each web app? – Daní Apr 24 '20 at 19:31
  • @Daní could add separate Docker file to web projects, and then build them separately? – Leszek P Mar 08 '21 at 13:30
  • could you explain what each line of this COPY command does and why dotnet need all this .csproj on the same directory for build ? Im getting build errors on my dotnet command – Arthur Melo Aug 04 '22 at 15:59