Search code examples
c#docker.net-coredocker-composerider

Error: read ECONNRESET with ASP.NET Core Web API on a Docker container


I'm running .NET 8 and have the below Docker Compose and dockerfile files to deploy my API to Docker from Rider for development.

The API is reachable when I run the API with Kestrel server, however, when I deploy to Docker I get

Error: read ECONNRESET

when I attempt to call the API.

The API can reach the Postgres container as I see the EF migrations running in the Rider console and I am also able to connect to Postgres from Postico via localhost:5432.

I'm thinking it must be some .NET configurations I am missing, but I have searched Google and SO without any luck.

I'm accessing the API with the following URL: http://localhost:5068

Docker Compose:

services:
  
  sweetnotes.api:
    image: esausilva/sweetnotes.api
    container_name: SweetNotes.Api
    build:
      context: .
      dockerfile: src/Api/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:5068
      - ASPNETCORE_HTTP_PORTS=5068
    ports:
      - "5068:5068"
    depends_on:
      - sweetnotes.db
    volumes:
      - ~/.aspnet/https:/root/.aspnet/https:ro
      - ~/.microsoft/usersecrets:/root/.microsoft/usersecrets:ro
        
  sweetnotes.db:
    image: postgres:latest
    container_name: SweetNotes.Db
    environment:
      - POSTGRES_USER=[USER]
      - POSTGRES_PASSWORD=[PASSWORD]
      - POSTGRES_DB=SweetNotes
    volumes:
      - ./.containers/database:/var/lib/postgresql/data
    ports:
      - "5432:5432"

Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 5068

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Development
WORKDIR /src
COPY ["src/Api/Api.csproj", "src/Api/"]
COPY ["src/Application/Application.csproj", "src/Application/"]
COPY ["src/Data/Data.csproj", "src/Data/"]
COPY ["src/Domain/Domain.csproj", "src/Domain/"]
RUN dotnet restore "src/Api/Api.csproj"
COPY . .
WORKDIR "/src/src/Api"
RUN dotnet build "Api.csproj" -c $BUILD_CONFIGURATION -o /app/build

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

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

appsettings.json

{
  "ConnectionStrings": {
    "SweetNotes": "Server=sweetnotes.db;Port=5432;Database=SweetNotes;User Id=[USER];Password=[PASSWORD];"
  },
  "urls": "http://localhost:5068",
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http1AndHttp2"
    }
  },
  "CorsSettings": {
    "FrontendOrigin": "http://localhost:3050"
  }
}

This command is what Rider is running

/usr/local/bin/docker-compose -f /Users/[...]/docker-compose.yml -f /Users/[...]/.idea/.idea.SweetNotesApi/Docker/docker-compose.generated.override.yml -p sweetnotesapi up --force-recreate -d

And this is Rider override

# This is a generated file. Not intended for manual editing.
services:
  sweetnotes.api:
    build:
      context: "/Users/[...]/SweetNotesApi"
      dockerfile: "src/Api/Dockerfile"
      target: "base"
      args:
        BUILD_CONFIGURATION: "Debug"
    command: []
    entrypoint:
    - "/opt/rider-debugger/linux-x64/dotnet/dotnet"
    - "/opt/rider-debugger/JetBrains.Debugger.Worker.exe"
    - "--mode=server"
    - "--frontend-port=57100"
    - "--backend-port=57300"
    - "--timeout=60"
    environment:
      DOTNET_USE_POLLING_FILE_WATCHER: "true"
      RIDER_DEBUGGER_LOG_DIR: "/var/opt/rider-debugger"
      RESHARPER_LOG_CONF: "/etc/opt/rider-debugger/backend-log.xml"
    image: "esausilva/sweetnotes.api:dev"
    ports:
    - "127.0.0.1:57019:57100"
    - "127.0.0.1:57219:57300"
    volumes:
    - "/Users/[...]/usersecrets"
    - "/Users/[...]/packages"
    - "/Users/[...]/app:rw"
    - "/Users/[...]/src:rw"
    - "/Users/[...]/rider-debugger"
    - "/Applications/[...]/backend-log.xml"
    - "/Users/[...]/rider-debugger:rw"
    working_dir: "/app"

I'm on MacOS.

Any help is appreciated!


Solution

  • I solved my issue by moving "urls": "http://localhost:5068", from appsettings.json to launchSettings.json and now the API works as expected when running in Docker and non-Docker Kestrel.