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

dgiusagp  于 12个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(123)

我有一个.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文件时,它给了我这个platformservice的错误:

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


当我重新启动platformservice时,它会给我这个:

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


谁能帮帮我。
我已经尝试用EntityFramework创建一个新的迁移文件,但它仍然给我同样的错误

1sbrub3j

1sbrub3j1#

您需要指示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
    ports:
      - "5432: 5432"
  platformservice:
    image: platformservice
    container_name: platform_service
    ports:
      - "8080:80"
    depends_on:
      - "postgres_server"

字符串

相关问题