You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法连接Docker中运行的PostgreSQL的问题求助

问题解决办法

核心原因

你的PostgreSQL容器未将5432端口映射到主机:

  • 运行在Docker容器中的C# API和PostgreSQL处于同一Docker内部网络,因此能通过容器内部IP(172.17.0.2)正常连接。
  • 本地环境的VS/VSCode扩展、EF Core迁移工具运行在主机系统,无法直接访问Docker容器的内部IP,导致连接超时或失败。

修复步骤

  1. 停止并移除现有PostgreSQL容器(若数据已备份或无重要数据):

    docker stop gb-postgres
    docker rm gb-postgres
    
  2. 重新启动容器并映射端口:
    执行以下命令,将主机的5432端口映射到容器的5432端口:

    docker run --name gb-postgres -e POSTGRES_PASSWORD=Sila.1009 -p 5432:5432 -d postgres
    
  3. 更新连接字符串:
    将连接字符串中的Host改为localhost(或主机的IP地址),替换原有的容器内部IP:

    Host=localhost;Database=GB3;Username=postgres;Password=Sila.1009
    
  4. 验证连接:

    • 使用VS/VSCode的PostgreSQL扩展重新连接,即可正常访问数据库。
    • 再次运行EF Core的Add-Migration命令,此时能成功连接数据库并生成迁移文件。

原问题相关信息

Docker容器信息

C:\Users\HT>docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                           NAMES
4b22768d9f5e   postgres     "docker-entrypoint.s…"   6 days ago       Up 6 days       5432/tcp                                        gb-postgres

连接字符串

Host=172.17.0.2;Database=GB3;Username=postgres;Password=Sila.1009

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["GB3API/GB3API.csproj", "GB3API/"]
RUN dotnet restore "GB3API/GB3API.csproj"
COPY . .
WORKDIR "/src/GB3API"
RUN dotnet build "GB3API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "GB3API.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "GB3API.dll"]

迁移失败日志

PM> Add-Migration
cmdlet Add-Migration at command pipeline position 1
Supply values for the following parameters:
Name: PassColumnAddedInTestResult
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Failed to connect to 172.17.0.2:5432
To undo this action, use Remove-Migration.

PostgreSQL安装步骤

Step 1 - Install Postgres

In the command line first
docker pull postgres

then 
docker run --name gb-postgres -e POSTGRES_PASSWORD=Sila.1009 -d postgres

Step 2 - Once Postgres is install you need to find out your instance IP address

In the command line first
docker ps

copy the {id  of the container} of the container and run

docker inspect {id  of the container}

copy the IPAddress of the container

Step 3 - Configure GB3 API

Open appSettings.json from the GB3 API

In dbConnectionString entry host should be the containers IP address

Step 4 - Run GB3 API container

if you are running the API from visual studio choose the docker profile and start debugging

if you are running the API out of VS:

from the command line go to the GB3 API folder, be sure you are at the same level of dockerfile, then

docker build --tag 'image_name'
docker run --detach 'image_name'

内容的提问来源于stack exchange,提问作者Syed Rafey Husain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 00:29:53