Search code examples
.netdockerbuilddockerfile

Docker compose up error while using --build


Hey everyone I don't know if this is the right topic for this but I have a problem with Docker exactly it is with docker compose.... I have 3 applications API, CLIENT, IdentityServer + SQL. I have created 3 Dockerfile and Docker-compose. When I do, for example, "docker build -f ./ShopProject.Api/Dockerfile -t vazaras/shopproject-api ." it all works nicely, the image builds up I can fire it up etc.... But when I docker compose up --build then during the build I get errors that the paths to the APIs were not found exactly that to the Shared application etc.... Dockerfile looks as follows and compose:

API:

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["ShopProject.Api/ShopProject.Api.csproj", "ShopProject.Api/"]
COPY ["ShopProject.Shared/ShopProject.Shared.csproj", "ShopProject.Shared/"]
COPY ["ShopProject.Application/ShopProject.Application.csproj", "ShopProject.Application/"]
COPY ["ShopProject.Infrastructure/ShopProject.Infrastructure.csproj","ShopProject.Infrastructure/"]
COPY ["ShopProject.Domain/ShopProject.Domain.csproj", "ShopProject.Domain/"]
COPY ["ShopProject.Persistence/ShopProject.Persistence.csproj", "ShopProject.Persistence/"]
RUN dotnet restore "ShopProject.Api/ShopProject.Api.csproj"
COPY . .
WORKDIR "/src/ShopProject.Api"
RUN dotnet build "ShopProject.Api.csproj" -c Release -o /app/build
RUN dotnet publish "ShopProject.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM mcr.microsoft.com/dotnet/aspnet:7.0
EXPOSE 80
EXPOSE 443
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "ShopProject.Api.dll"]

CLIENT:

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /app
COPY ["ShopProject.Client/ShopProject.Client.csproj", "ShopProject.Client/"]
COPY ["ShopProject.Shared/ShopProject.Shared.csproj", "ShopProject.Shared/"]
RUN dotnet restore "ShopProject.Client/ShopProject.Client.csproj"
COPY . .
RUN dotnet publish "ShopProject.Client/ShopProject.Client.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM nginx:alpine
EXPOSE 80
EXPOSE 443
COPY --from=build /app/publish/wwwroot /usr/share/nginx/html

IDS:

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["DuendeIdentityServer/DuendeIdentityServer.csproj", "DuendeIdentityServer/"]
RUN dotnet restore "DuendeIdentityServer/DuendeIdentityServer.csproj"
COPY . .
WORKDIR "/src/DuendeIdentityServer"
RUN dotnet build "DuendeIdentityServer.csproj" -c Release -o /app/build
RUN dotnet publish "DuendeIdentityServer.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
EXPOSE 80
EXPOSE 443
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "DuendeIdentityServer.dll"]

Docker-compose:

version: '3.8'

services:
  sqlserver:
    image: mcr.microsoft.com/azure-sql-edge
    container_name: sqlserver-shopproject
    environment:
      - ACCEPT_EULA=1
      - MSSQL_SA_PASSWORD=Pass1234$
    ports:
      - 4000:1433
  
  identity-server:
    build:
      context: ./DuendeIdentityServer
      dockerfile: Dockerfile
    image: vazaras/shopproject-identity-server
    container_name: shopproject-identity-server
    ports:
      - 9000:80
      - 9001:443
  
  api:
    build:
      context: ./ShopProject.Api
      dockerfile: Dockerfile
    image: vazaras/shopproject-api
    container_name: shopproject-api
    ports:
      - 9002:80
      - 9003:443
  
  frontend:
    build:
      context: ./ShopProject.Client
      dockerfile: Dockerfile
    image: vazaras/shopproject-frontend
    container_name: shopproject-frontend
    ports:
      - 9004:80
      - 9005:443

My file tree looks like:

.
├── DuendeIdentityServer
│   ├── Config.cs
│   ├── Data
│   ├── Dockerfile
│   ├── DuendeIdentityServer.csproj
│   ├── HostingExtensions.cs
│   ├── Migrations
│   ├── Models
│   ├── Pages
│   ├── Program.cs
│   ├── Properties
│   ├── SeedData.cs
│   ├── Services
│   ├── appsettings.json
│   ├── bin
│   ├── buildschema.bat
│   ├── keys
│   ├── obj
│   └── wwwroot
├── ShopProject.Api
│   ├── Controllers
│   ├── Dockerfile
│   ├── Program.cs
│   ├── Properties
│   ├── Services
│   ├── ShopProject.Api.csproj
│   ├── appsettings.Development.json
│   ├── appsettings.json
│   ├── bin
│   ├── logs
│   └── obj
├── ShopProject.Application
│   ├── Common
│   ├── DependencyInjection.cs
│   ├── ShopProject.Application.csproj
│   ├── bin
│   └── obj
├── ShopProject.Client
│   ├── App.razor
│   ├── Dockerfile
│   ├── Pages
│   ├── Program.cs
│   ├── Properties
│   ├── Shared
│   ├── ShopProject.Client.csproj
│   ├── _Imports.razor
│   ├── bin
│   ├── obj
│   └── wwwroot
├── ShopProject.Domain
│   ├── Common
│   ├── ShopProject.Domain.csproj
│   ├── bin
│   └── obj
├── ShopProject.Infrastructure
│   ├── DependencyInjection.cs
│   ├── Services
│   ├── ShopProject.Infrastructure.csproj
│   ├── bin
│   └── obj
├── ShopProject.Persistence
│   ├── AppDbContext.cs
│   ├── ConnectionStringDbContext.cs
│   ├── DbContextFactory.cs
│   ├── DependencyInjection.cs
│   ├── DesignTimeDbContextFactoryBase.cs
│   ├── ShopProject.Persistence.csproj
│   ├── bin
│   └── obj
├── ShopProject.Shared
│   ├── ShopProject.Shared.csproj
│   ├── WeatherForecast.cs
│   ├── bin
│   └── obj
├── ShopProject.sln
├── backup-whateverfolder-2023-10-14.txt
└── docker-compose.yaml

Success build when docker build -f ./ShopProject.Api/ShopProject.Api.csproj -t vazaras/shopproject-api . Succes

Failed when docker compose up --build Fail


Solution

  • The docker build options map to Compose options like so:

    docker build -f ./ShopProject.Api/Dockerfile -t vazaras/shopproject-api .
    #               ^^^ dockerfile: ^^^^^^^^^^^^    ^^^ image: ^^^^^^^^^^^^ ^context:
    

    So if that docker build command works, you should set up your Compose file as

    services:
      api:
        build:
          context: .                                # path at end of command
          dockerfile: ./ShopProject.Api/Dockerfile  # -f option
        image: vazaras/shopproject-api              # -t option
    

    (Compose is capable of assigning the image and container names automatically, so if you're not planning to push the images to a registry or run them outside of Compose, you may not need to manually specify image:. Similarly, you do not usually need to specify container_name:.)