.NET Web 服务容器无法连接到数据库容器

问题描述 投票:0回答:1

我有一个 .net Web 服务容器和一个 postgresql 服务。 我的 docker-compose 文件是这样的:

version: '0.1'
services:
  postgres_server:
    container_name: postgres_server
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=ggbb2005
      - POSTGRES_DB=Platform
  platformservice:
    image: platformservice
    container_name: platform_service
    ports:
      - "8080:80"
    depends_on:
      - "postgres_server"
    

我的连接字符串是这样的:

"WebApiDatabase": "Host=postgres_server; Port=5432 ; Database=Platform;User Id=postgres; Password=ggbb2005"

但是当我运行 docker-compose 文件时,它给了我这个平台服务错误:

platform_service  | Unhandled exception. System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
platform_service  |  ---> Npgsql.NpgsqlException (0x80004005): Failed to connect to 172.18.0.2:5432
platform_service  |  ---> System.Net.Sockets.SocketException (111): Connection refused
platform_service  |    at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
platform_service  |    at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
platform_service  |    at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
platform_service  |    at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|213_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
platform_service  |    at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
platform_service  |    at Npgsql.PoolingDataSource.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
platform_service  |    at Npgsql.PoolingDataSource.<Get>g__RentAsync|34_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
platform_service  |    at Npgsql.NpgsqlConnection.<Open>g__OpenAsync|42_0(Boolean async, CancellationToken cancellationToken)
platform_service  |    at Npgsql.NpgsqlConnection.Open()
platform_service  |    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
platform_service  |    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
platform_service  |    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
platform_service  |    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__21_0(DbContext _, Enumerator enumerator)
platform_service  |    at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
platform_service  |    --- End of inner exception stack trace ---
platform_service  |    at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
platform_service  |    at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
platform_service  |    at lambda_method4(Closure, QueryContext)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
platform_service  |    at AppContext.prepDb.SeedData(DataContext context) in /app/AppContext/prepDb.cs:line 21
platform_service  |    at AppContext.prepDb.PrepPopulation(IApplicationBuilder app) in /app/AppContext/prepDb.cs:line 15
platform_service  |    at Program.<Main>$(String[] args) in /app/Program.cs:line 27

当我重新启动平台服务时,它给了我这个:

postgres_server   | 2024-01-04 03:46:28.313 UTC [73] ERROR:  relation "Platforms" does not exist at character 39
postgres_server   | 2024-01-04 03:46:28.313 UTC [73] STATEMENT:  SELECT EXISTS (
postgres_server   |         SELECT 1
postgres_server   |         FROM "Platforms" AS p)
platform_service  | Unhandled exception. Npgsql.PostgresException (0x80004005): 42P01: relation "Platforms" does not exist
platform_service  |
platform_service  | POSITION: 39
platform_service  |    at Npgsql.Internal.NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
platform_service  |    at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token)
platform_service  |    at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
platform_service  |    at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
platform_service  |    at Npgsql.NpgsqlDataReader.NextResult()
platform_service  |    at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
platform_service  |    at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
platform_service  |    at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
platform_service  |    at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
platform_service  |    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__21_0(DbContext _, Enumerator enumerator)
platform_service  |    at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
platform_service  |    at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
platform_service  |    at lambda_method4(Closure, QueryContext)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
platform_service  |    at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
platform_service  |    at AppContext.prepDb.SeedData(DataContext context) in /app/AppContext/prepDb.cs:line 21
platform_service  |    at AppContext.prepDb.PrepPopulation(IApplicationBuilder app) in /app/AppContext/prepDb.cs:line 15
platform_service  |    at Program.<Main>$(String[] args) in /app/Program.cs:line 27
platform_service  |   Exception data:
platform_service  |     Severity: ERROR
platform_service  |     SqlState: 42P01
platform_service  |     MessageText: relation "Platforms" does not exist
platform_service  |     Position: 39
platform_service  |     File: parse_relation.c
platform_service  |     Line: 1449
platform_service  |     Routine: parserOpenTable
platform_service exited with code 139

任何人都可以帮助我吗?

我已经尝试使用实体框架创建一个新的迁移文件,但它仍然给我同样的错误

.net postgresql entity-framework docker-compose
1个回答
0
投票

您需要指示 Docker 公开 PostgreSQL 默认使用的端口。将

- "8080:80"
更改为
- "5432: 5432"
应该可以解决问题。

version: '0.1'
services:
  postgres_server:
    container_name: postgres_server
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=ggbb2005
      - POSTGRES_DB=Platform
  platformservice:
    image: platformservice
    container_name: platform_service
    ports:
      - "5432: 5432"
    depends_on:
      - "postgres_server"
© www.soinside.com 2019 - 2024. All rights reserved.