asp.net core 2.0 - multiple projects solution docker file asp.net core 2.0 - multiple projects solution docker file docker docker

asp.net core 2.0 - multiple projects solution docker file


I finally found a way how to built solutions with docker.

IMPORTANT: For this to work, you have to put the Dockerfile file into the same location where the solution file is

I've just created a docker file with the following content:

FROM microsoft/aspnetcore:2.0 AS baseWORKDIR /appEXPOSE 80FROM microsoft/aspnetcore-build:2.0 AS buildWORKDIR /srcCOPY Solution.sln ./COPY ClassLibraryProject/*.csproj ./ClassLibraryProject/COPY WebAPIProject/*.csproj ./WebAPIProject/RUN dotnet restoreCOPY . .WORKDIR /src/ClassLibraryProjectRUN dotnet build -c Release -o /appWORKDIR /src/WebAPIProjectRUN dotnet build -c Release -o /appFROM build AS publishRUN dotnet publish -c Release -o /appFROM base AS finalWORKDIR /appCOPY --from=publish /app .ENTRYPOINT ["dotnet", "WebAPIProject.dll"]

Note that I think you may have to respect project build dependencies but I don't know really.

Sample call to build:

sudo docker build --no-cache -t webapi:dev .

Sample call to run:

sudo docker run -d=false -p 8080:80 --name webapi webapi:dev

Hope that helps.


While I understand that dotnet core isn't specific to Windows and not everyone is going to be using Visual Studio, there's this neat feature that Microsoft has included inside Visual Studio (Tried it in 2017).

  1. Right click on the Web Project.
  2. Select Add.
  3. Then "Docker Support". This will automatically fetch multiple projects into the container.

If you have multiple Web Projects, repeat the step individually for each.

Here's the reference: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/visual-studio-tools-for-docker?view=aspnetcore-2.1#existing-app

If you happen to run with any issues when running the Docker container, try deselecting Hyper-V Services in "Windows Features" (Search for Windows Features in Start Menu), selecting again, and then restarting your computer.Check here.

enter image description here


I had the same problem and all other solutions didn't fit my so I worked out my own.I feel that bellow steps are legible, but if you're new to Docker, I explained it line by line in my blog post dedicated to new Docker users (so you could understand precisely what is going on in this Dockerfile).

  1. Keep Dockerfile in project directory (which is, in my point of view, better than keeping it next to solution file, because you can have more than one Docker image per solution).

  2. Configure Dockerfile as follows:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-envWORKDIR /appCOPY . ./RUN dotnet publish PROJECT_NAME -c Release -o outFROM mcr.microsoft.com/dotnet/core/aspnet:2.2WORKDIR /appCOPY --from=build-env /app/PROJECT_NAME/out .ENTRYPOINT ["dotnet", "PROJECT_NAME.dll"]
  1. Move .dockerignore file to solution directory - that's required, because Docker CLI takes .dockerignore file only from root directory of the build context (as documentation says), but it's also convenient, because you have one, common set of ignore rules for all projects (similar to .gitignore file), which is easier to maintain.

  2. Run build command from solution directory, pointing to Dockerfile in project directory, and setting current directory (.) as build context (to have access to all projects):

docker build -f PROJECT_DIRECTORY/Dockerfile -t IMAGE_NAME .
  1. Run container as usual, for example:
docker run -d -p 8080:80 --name my-app-name IMAGE_NAME