«Исключение при запуске приложения: System. Net .Sockets.SocketException (111): соединение отклонено» с docker -compose - PullRequest
2 голосов
/ 23 февраля 2020

У меня проблема с командами Docker и docker -compose.

У меня есть приложение, использующее React / PostgreSQL / C#. Когда я запускаю docker run с отдельным docker .dev файлом, все работает нормально, но как только я пытаюсь запустить их все вместе с compose, у меня появляется ошибка на моем C# сервере:

 Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {5db3b6a2-4d79-4057-aee4-fb7ca9ab5b3f} may be persisted to storage in unencrypted form.
Application startup exception: System.Net.Sockets.SocketException (111): Connection refused
   at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnection.Open()
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at Kitchen_Table.Api.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in /app/Kitchen-Table.Api/Startup.cs:line 133
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Mvc.Internal.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6]
      Application startup exception
System.Net.Sockets.SocketException (111): Connection refused
   at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnection.Open()
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at Kitchen_Table.Api.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in /app/Kitchen-Table.Api/Startup.cs:line 133
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Mvc.Internal.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Unhandled Exception: System.Net.Sockets.SocketException: Connection refused
   at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnection.Open()
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at Kitchen_Table.Api.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in /app/Kitchen-Table.Api/Startup.cs:line 133
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Mvc.Internal.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String shutdownMessage)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
   at Kitchen_Table.Api.Program.Main(String[] args) in /app/Kitchen-Table.Api/Program.cs:line 18

Мой docker -compose.yml выглядит так:

version: "3.7"

networks:
  kitchen-table-dev:
    driver: bridge

services:
  server:
    image: server
    depends_on:
      - "postgres"
    build:
      context: ./server
      dockerfile: Dockerfile.dev
    ports:
      - 5001:5001
    environment:
      DB_CONNECTION_STRING: ${DB_CONNECTION_STRING}
    networks:
      - kitchen-table-dev
  client:
    ports:
      - 3000:3000
    build:
      dockerfile: Dockerfile.dev
      context: ./client
    volumes:
      - /app/node_modules
      - ./client:/app
  postgres:
    image: postgres:11.4
    ports:
      - 5432:5432
    restart: always
    environment:
      POSTGRES_PORT: ${POSTGRES_PORT}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: "kitchen_table"
    networks:
      - kitchen-table-dev
volumes:
  db_volume:
    driver: local

Dockerfile.dev внутри папки сервера выглядит так:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ["Kitchen-Table.Api/Kitchen-Table.Api.csproj", "Kitchen-Table.Api/"]
COPY ["Kitchen-Table.Database/Kitchen-Table.Database.csproj", "Kitchen-Table.Database/"]
COPY ["Kitchen-Table.Database.Entity/Kitchen-Table.Database.Entity.csproj", "Kitchen-Table.Database.Entity/"]
RUN dotnet restore "Kitchen-Table.Api/Kitchen-Table.Api.csproj"

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

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app/Kitchen-Table.Api/out .
EXPOSE 5001
ENTRYPOINT ["dotnet", "Kitchen-Table.Api.dll"]

и мой ConnectionString внутри настроек приложения. json выглядит вот так:

 "ConnectionStrings": {
    "DbContext": "@Host=127.0.0.1;Port=5432;Database=kitchen_table;UserID=<user>Password=<password>;"
  }

1 Ответ

3 голосов
/ 23 февраля 2020

Вы можете вызывать сервисы по его имени, 127.0.0.0.1 или localhost - это сам контейнер клиента. Попробуйте изменить строку подключения ниже -

"ConnectionStrings": {
    "DbContext": "@Host=postgres;Port=5432;Database=kitchen_table;UserID=<user>Password=<password>;"
  }
...