gpt4 book ai didi

asp.net - "Application startup exception: System.Net.Sockets.SocketException (111): Connection refused"与 docker-compose

转载 作者:行者123 更新时间:2023-12-02 18:13:28 24 4
gpt4 key购买 nike

我对 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"]

我在 appsettings.json 中的 ConnectionString 看起来像这样:
 "ConnectionStrings": {
"DbContext": "@Host=127.0.0.1;Port=5432;Database=kitchen_table;UserID=<user>Password=<password>;"
}

最佳答案

您可以通过它的名称调用服务,127.0.0.0.1localhost是客户端容器本身。尝试将连接字符串更改为以下 -

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

关于asp.net - "Application startup exception: System.Net.Sockets.SocketException (111): Connection refused"与 docker-compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60364008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com