1

I have two asp .net core projects a web app and an api. The web app has a reference going directly towards the api. The problem arises when I try and build a docker image using my Dockerfile. My idea was to build both the web app project and the api project in 1 dockerfile. I am very new to docker so I am not even sure if it's possible

This is my docker file:

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

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

COPY ./WebApp/WebApp.csproj ./
RUN dotnet restore /app/WebApp.csproj

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

COPY ./WebApp./
RUN dotnet publish /app/WebApp.csproj -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Blockchain Demonstrator Web App.dll"]

I get this error:

 => ERROR [build-env 6/6] RUN dotnet publish -c Release -o out                                                                                                                                                                                                                   3.1s
------
 > [build-env 6/6] RUN dotnet publish -c Release -o out:
#13 0.499 Microsoft (R) Build Engine version 16.7.2+b60ddb6f4 for .NET
#13 0.499 Copyright (C) Microsoft Corporation. All rights reserved.
#13 0.499
#13 0.917   Determining projects to restore...
#13 0.919   Skipping project "/BlockchainDemonstratorApi/BlockchainDemonstratorApi.csproj" because it was not found.
#13 0.920   Skipping project "/BlockchainDemonstratorApi/BlockchainDemonstratorApi.csproj" because it was not found.
#13 1.601   Restored /app/BlockchainDemonstratorWebApp.csproj (in 443 ms).
#13 1.846 /usr/share/dotnet/sdk/3.1.409/Microsoft.Common.CurrentVersion.targets(1850,5): warning : The referenced project '../BlockchainDemonstratorApi/BlockchainDemonstratorApi.csproj' does not exist. [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/BeerGameController.cs(6,7): error CS0246: The type or namespace name 'BlockchainDemonstratorApi' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/BeerGameController.cs(7,7): error CS0246: The type or namespace name 'BlockchainDemonstratorApi' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/FactorsController.cs(8,7): error CS0246: The type or namespace name 'BlockchainDemonstratorApi' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/FactorsController.cs(9,7): error CS0246: The type or namespace name 'BlockchainDemonstratorApi' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/BeerGameController.cs(108,54): error CS0246: The type or namespace name 'RoleType' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.996 Controllers/FactorsController.cs(71,4): error CS0246: The type or namespace name 'Factors' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.996 Controllers/BeerGameController.cs(160,21): error CS0246: The type or namespace name 'Order' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
------
failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c dotnet publish -c Release -o out]: runc did not terminate sucessfully

  • I would suggest you should have two separate dockerfiles - one each for the web application and the API application. These are both your microservices and so are separate images to be run as separate containers. Start from that point and see how you get on. – JohnXF May 31 '21 at 11:47
  • @JohnXF I tried doing that, but when I ran docker build using the web app dockerfile the error as shown above was thrown. This error is thrown because of the project reference between the two projects. 1 way of solving this is to seperate the two project, but I wanted to see if there was another way – RijstPlukker May 31 '21 at 11:57
  • I don't know much about dotnet so I cannot help you there, but it sounds like your problem is with the building part of the project, not necessarily the docker part of it. If the two applications are interdependent code-wise, perhaps try creating a docker image with all the code compiled/pacakged into it and then use that as the base for two new images with the different entry points? – JohnXF May 31 '21 at 12:13
  • Do not run 2 services in 1 dockerfile. If you do this then there is no point using docker at all and you'll not get any serious help. That's not what it's for and you're not learning anything. Build 2 separate dockerfiles, and learn how to make them talk to each other using Docker Compose. – Software Engineer May 31 '21 at 17:01

1 Answers1

-1

The solution was to have Visual Studio generate the Dockerfile for me. More info can be found on: https://stackoverflow.com/a/53000362/12559137