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

Docker环境下Golang容器无法连接PostgreSQL容器求助

问题:Golang Fiber容器无法连接Docker中的PostgreSQL数据库

单独运行PostgreSQL容器并在本地启动Go项目时可正常连接,但从Golang容器连接时提示连接拒绝。已确认网络可正常解析主机,需排查无法连接的原因。


相关配置与代码

docker-compose.yml

version: '3.8'

services:

  web:
    container_name: web
    build:
      context: .
      dockerfile: docker/Dev.Dockerfile
    ports:
      - 3000:3000
    networks:
      my_network:
    volumes:
      - ./:/app
    depends_on:
      - nim_auth_postgres

  nim_auth_postgres:
    container_name: nim_auth_postgres
    image: postgres
    ports:
      - 5433:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=nim_auth_db
    networks:
      my_network:

networks:
  my_network:

Go数据库连接代码

package config

import (
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)

var DB *gorm.DB

func DbConnection(){
    var err error
    dsn := "host=nim_auth_postgres user=postgres password=postgres dbname=nim_auth_db port=5433 sslmode=disable TimeZone=Asia/Dhaka"
    DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})

    if err != nil{
        panic(err)
    }
}

Dev.Dockerfile

FROM golang:1.21-alpine

WORKDIR /app

RUN go install github.com/cosmtrek/air@latest

COPY go.mod go.sum ./
RUN go mod download

CMD ["air", "-c", ".air.toml"]

启动服务后出现的错误

web                | 2023/09/22 09:17:45 /app/src/config/connect_postgres_database.go:13
web                | [error] failed to initialize database, got error failed to connect to `host=nim_auth_postgres user=postgres database=postgres`: dial error (dial tcp 172.27.0.2:5433: connect: connection refused)
web                | panic: failed to connect to `host=nim_auth_postgres user=postgres database=postgres`: dial error (dial tcp 172.27.0.2:5433: connect: connection refused)
web                | 
web                | goroutine 1 [running]:
web                | github.com/fahad-md-kamal/numbus-authenticator/src/config.DbConnection()
web                |    /app/src/config/connect_postgres_database.go:16 +0x10e
web                | main.main()
web                |    /app/main.go:10 +0xf

环境版本

  • Docker version 24.0.6, build ed223bc
  • Docker Compose version v2.20.3
  • go version go1.21.1 linux/amd64

解决方案

核心问题:容器间连接误用宿主机映射端口

同一Docker Compose网络内的容器互相访问时,需使用容器内部监听的端口,而非ports字段中映射到宿主机的端口。

你的配置中:

  • PostgreSQL容器内部默认监听5432端口
  • 5433是映射到宿主机的端口,仅用于宿主机访问容器内的PostgreSQL

但Go代码的DSN中错误使用了port=5433,需改为容器内部端口5432。

修改后的Go代码DSN

dsn := "host=nim_auth_postgres user=postgres password=postgres dbname=nim_auth_db port=5432 sslmode=disable TimeZone=Asia/Dhaka"

额外优化建议

  1. 确保数据库就绪后再启动web服务:depends_on仅保证启动顺序,不保证PostgreSQL初始化完成。可添加健康检查:
    修改docker-compose.yml中的nim_auth_postgres服务:
    nim_auth_postgres:
      container_name: nim_auth_postgres
      image: postgres
      ports:
        - 5433:5432
      environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
        - POSTGRES_DB=nim_auth_db
      networks:
        my_network:
      healthcheck:
        test: ["CMD-SHELL", "pg_isready -U postgres -d nim_auth_db"]
        interval: 5s
        timeout: 5s
        retries: 5
    
    同时修改web服务的depends_on:
    depends_on:
      nim_auth_postgres:
        condition: service_healthy
    
  2. 你的容器已在同一my_network网络下,主机名解析正常(错误信息已解析到172.27.0.2),修改端口后即可正常连接。

内容的提问来源于stack exchange,提问作者Fahad Md Kamal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 08:52:46