How to dockerize multiple projects in ASP.NET Core?

Viewed 45

I'm trying to compose my two projects with one solution:

-Root
.sln
docker-compose.yml
 -Application.Api
    Dockerfile
 -Application.SMTP
    Dockerfile

My Dockerfile in Application.Api:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
EXPOSE 443
EXPOSE 80

# Copy csproj and restore as distinct layers
COPY *.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/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Application.Api.dll"]

Dockerfile in Application.SMTP:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
EXPOSE 443
EXPOSE 80

# Copy csproj and restore as distinct layers
COPY *.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/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Application.SMTP.dll"]

And my docker-compose is:

version: '3.4'

services:
  application.smtp:
    image: ${DOCKER_REGISTERY-}eshopsmtp
    build:
      context: .
      dockerfile: Application.SMTP/Dockerfile
  application.api:
    image: ${DOCKER_REGISTERY-}eshopapi
    build:
      context: .
      dockerfile: Application.Api/Dockerfile

When I use docker-compose build:

This happend

When I am trying to run Dockerfile from Api it crashes, because couldn't find reference from SMTP:

Output here

How to compose these two projects? I couldn't find any solution that works for me

1 Answers

When you create a Dockerfile you should be aware of the context in which you intend to build the image.

From your first paragraph, I deduct you have a solution with 2 projects, each one in its own folder.

Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

The above means you actually expect the context to be the project folder (not the one above that contains the solution). But your docker-compose.yml says otherwise:

context: .
dockerfile: Application.SMTP/Dockerfile

It actually expects the context to be the one with the solution and looks for the Dockerfile inside the project folder.

So all your paths in the Dockerfile must be relative to the context used by docker-compose.

Related