gpt4 book ai didi

postgresql - GoLang 服务无法使用 docker compose 访问 Postgres

转载 作者:数据小太阳 更新时间:2023-10-29 03:08:24 26 4
gpt4 key购买 nike

我有一个用 Go 编写的 HTML 服务。它使用 Postgres,但是当使用 docker compose 将它们组合在一起时,我得到“dial tcp 0.0.0.0:5432: connect: connection refused”

在仅使用 docker 并引用 Postgres 的运行图像构建启动服务时工作

来自 go 的调用片段


psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)

db, err := sql.Open("postgres", psqlInfo)
if err != nil {
fmt.Println(err)
return nil, err
}

调试时自己确认

    host = os.Getenv("pgHost")
port = os.Getenv("pgPort")
user = os.Getenv("pgUser")
password = os.Getenv("pgPassword")
dbname = os.Getenv("pgDbName")

fmt.Println("host = ", host)
fmt.Println("port = ", port)
fmt.Println("user = ", user)
fmt.Println("password = ", password)
fmt.Println("dbname = ", dbname)

docker 文件

FROM golang as builder

ENV GO111MODULE=on

WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /app/doggos


FROM scratch
COPY --from=builder /app/doggos /app/
EXPOSE 7000
ENTRYPOINT ["/app/doggos"]

docker-compose.yml

version: '3.5'
services:
app:
build: .
ports:
- "7000:7000"
environment:
pgHost: "0.0.0.0"
pgPort: 5432
pgUser: "postgres"
pgPassword: "postgres_docker"
pgDbName: "postgres"
dbType: "POSTGRES"
depends_on:
- postgres-db
links:
- postgres-db

postgres-db:
image: postgres:latest
restart: always
environment:
POSTGRES_PASSWORD: "postgres_docker"
POSTGRES_DB: "postgres"
POSTGRES_USER: "postgres"
POSTGRES_HOST: "0.0.0.0"
POSTGRES_PORT: 5432
PGDATA: /var/lib/postgresql/data/pg_data
ports:
- "5432:5432"
volumes:
- ./scripts/postgres/schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
- ./scripts/postgres/table.sql:/docker-entrypoint-initdb.d/2-table.sql

volumes:
pg_data:
postgres-db_1  | 2019-07-12 19:43:36.172 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres-db_1 | 2019-07-12 19:43:36.172 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres-db_1 | 2019-07-12 19:43:36.175 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-db_1 | 2019-07-12 19:43:36.189 UTC [72] LOG: database system was shut down at 2019-07-12 19:43:36 UTC
postgres-db_1 | 2019-07-12 19:43:36.195 UTC [1] LOG: database system is ready to accept connections
app_1 | host = 0.0.0.0
app_1 | port = 5432
app_1 | user = postgres
app_1 | password = postgres_docker
app_1 | dbname = postgres
app_1 | dial tcp 0.0.0.0:5432: connect: connection refused
app_1 | 2019/07/12 19:44:36 http: panic serving 192.168.0.1:56494: runtime error: invalid memory address or nil pointer dereference
app_1 | goroutine 3 [running]:
app_1 | net/http.(*conn).serve.func1(0xc0000b7180)
app_1 | /usr/local/go/src/net/http/server.go:1769 +0x139
app_1 | panic(0x8b8780, 0xd75170)

最佳答案

你应该使用postgres-db(最好是给它一个不带破折号的简单名称,例如db)来设置数据库主机,而不是0.0.0.0

容器是使用postgres-db 的配置创建的。它以名称 postgres-db 加入网络。

每个容器都可以查找主机名 apppostgres-db 并取回相应容器的 IP 地址。例如,app 的应用程序代码可以连接到 URL postgres://postgres-db:5432 并开始使用 Postgres 数据库。

Here is more information

关于postgresql - GoLang 服务无法使用 docker compose 访问 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57013466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com