postgresql - docker compose postgresql 连接 ExtendedSocketException : Cannot assign requested address

标签 postgresql .net-core docker-compose

这是我的 postgresql 迁移代码。在 visual studio 中运行项目时没有问题。

services.AddDbContext<myDbContext>(options =>  options.UseNpgsql("User ID=myuser;Password=123;Host=localhost;Port=5432;Database=testDb;Pooling=true;"));

using var scope = services.BuildServiceProvider().CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<myDbContext>();
dbContext.Database.Migrate();

但是当我在 docker compose 模式下运行时,我在 dbContext.Database.Migrate() 行收到此错误

ExtendedSocketException: Cannot assign requested address [::1]:5432 This exception was originally thrown at this call stack: System.Net.Sockets.Socket.DoConnect(System.Net.EndPoint, System.Net.Internals.SocketAddress) System.Net.Sockets.Socket.Connect(System.Net.EndPoint) Npgsql.NpgsqlConnector.Connect(Npgsql.Util.NpgsqlTimeout)

最佳答案

你能试试这个连接字符串吗

"User ID=myuser;Password=123;Host=host.docker.internal;Port=5432;Database=testDb;Pooling=true;"

我的意思是,使用 host.docker.internal 而不是 localhost

关于postgresql - docker compose postgresql 连接 ExtendedSocketException : Cannot assign requested address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69005743/

相关文章:

python - Dockerfile 无法复制 & Docker-compose 卷不与容器同步

bash - 为什么 bash 从字面上使用单引号和双引号?

sql - Postgresql 向列添加规则以在选择时转换数据

sql - PostgreSQL 错误 : operator does not exist: date ~~ unknown

c# - EF Core 中的 AddRange 和 AddRangeAsync 有什么区别

entity-framework - Entity Framework 添加迁移失败,出现 "Could not find assembly"

linux - 将环境变量从 linux 传递到 docker 容器

postgresql - 如何获取 PostgreSQL 中删除的行数?

sql - Postgres - 如何找到某一列的最大交集的行

mysql - ASP.NET Core 与 MySQL 的 session