asp.netdotnet-aspire

.NET Aspire on existing docker project


I have a project that is already using docker and docker compose my problem is that when I start the project using docker compose, Aspire does not log anything, but when I start the project using dotnet run, its working.

here is my docker compose

services:
  sinamn75api:
    image: sinamn75api
    build: .
    ports:
      - "8090:80"
      - "8091:443"
    environment:
      ASPNETCORE_ENVIRONMENT: "Development"
      ASPNETCORE_URLS: "https://+:443;http://+:80"
      ASPNETCORE_Kestrel__Certificates__Default__Password: "BetterSoft1234"
      ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/sinamn75api.pfx"
      OTEL_EXPORTER_OTLP_ENDPOINT=http://sinamn75api.dashboard:18889:
    volumes:
      - ~/.aspnet/https:/https:ro
      - ./SinaMN75Api/wwwroot:/app/wwwroot
    
  sinamn75api.redis:
    image: redis:latest
    restart: always
    ports:
      - '6379:6379'
    
  sinamn75api.dashboard:
    image: mcr.microsoft.com/dotnet/aspire-dashboard:latest
    container_name: dashboard
    ports:
      - "18888:18888"
      - "18889:18889"
    networks:
      - open

networks:
  open:

here is my docker

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["SinaMN75Api/SinaMN75Api.csproj", "SinaMN75Api/"]
COPY ["Utilities_aspnet/Utilities_aspnet.csproj", "Utilities_aspnet/"]
RUN dotnet restore "SinaMN75Api/SinaMN75Api.csproj" --disable-parallel
COPY . .
WORKDIR "/src/SinaMN75Api"
RUN dotnet build "SinaMN75Api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "SinaMN75Api.csproj" -c Release -o /app/publish /p:UseAppHost=false

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

and here is my OpenTelemetry setup:

builder.Services.AddOpenTelemetry()
    .ConfigureResource(res => res.AddService("SinaMN75Api"))
    .WithMetrics(meter => {
        meter.AddAspNetCoreInstrumentation().AddHttpClientInstrumentation();
        meter.AddOtlpExporter(options => options.Endpoint = new Uri("http://localhost:18889"));
    })
    .WithTracing(t => {
        t.AddAspNetCoreInstrumentation().AddHttpClientInstrumentation().AddEntityFrameworkCoreInstrumentation();
        t.AddOtlpExporter(options => options.Endpoint = new Uri("http://localhost:18889"));
    });

builder.Logging.AddOpenTelemetry(o => {
    o.AddConsoleExporter().SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("SinaMN75Api"));
    o.AddOtlpExporter(options => options.Endpoint = new Uri("http://localhost:18889"));
});

Solution

  • I found the problem, I was using networks for my aspire image but not for other images

    so the compose file should be like this

    services:
      sinamn75api:
        image: sinamn75api
        build: .
        ports:
          - "8090:80"
          - "8091:443"
        environment:
          ASPNETCORE_ENVIRONMENT: "Development"
          ASPNETCORE_URLS: "https://+:443;http://+:80"
          ASPNETCORE_Kestrel__Certificates__Default__Password: "BetterSoft1234"
          ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/sinamn75api.pfx"
          OTEL_EXPORTER_OTLP_ENDPOINT=http://sinamn75api.dashboard:18889:
        volumes:
          - ~/.aspnet/https:/https:ro
          - ./SinaMN75Api/wwwroot:/app/wwwroot
        networks:
          - otel
        
      sinamn75api.redis:
        image: redis:latest
        restart: always
        ports:
          - '6379:6379'
        networks:
          - otel
        
      sinamn75api.dashboard:
        image: mcr.microsoft.com/dotnet/aspire-dashboard:latest
        container_name: dashboard
        ports:
          - "18888:18888"
        networks:
          - otel
    
    networks:
      otel: