[英]Can not resolve postgres hostname from other service on docker-compose
I have a docker-compose file which run 2 containers.我有一个运行 2 个容器的 docker-compose 文件。 One of the service is postgres and the other one is my backend service.
其中一项服务是 postgres,另一项是我的后端服务。 When I try to resolve postgres or try to connect it from cli of the backend service, it is successfull.
当我尝试解析 postgres 或尝试从后端服务的 cli 连接它时,它是成功的。 But when I try to initialize database with hostname, I got following error:
但是当我尝试使用主机名初始化数据库时,出现以下错误:
2022/09/01 08:49:53 /src/domain/domain.go:29 [error] failed to initialize database, got error failed to connect to
host=postgres user=devuser database=backenddev
: hostname resolving error (lookup postgres: device or resource busy) panic: failed to connect tohost=postgres user=devuser database=backenddev
: hostname resolving error (lookup postgres: device or resource busy)2022/09/01 08:49:53 /src/domain/domain.go:29 [错误] 无法初始化数据库,出错无法连接到
host=postgres user=devuser database=backenddev
:主机名解析错误(查找 postgres :设备或资源忙)恐慌:无法连接到host=postgres user=devuser database=backenddev
:主机名解析错误(查找postgres:设备或资源忙)goroutine 1 [running]: github.com/anilkusc/backend/api.(*Api).Init(0xc00000e150) /src/api/api.go:34 +0x76b github.com/anilkusc/backend/api.(*Api).Start(0xc00000e150) /src/api/api.go:87 +0x25 main.main() /src/main.go:48 +0x15f
goroutine 1 [running]: github.com/anilkusc/backend/api.(*Api).Init(0xc00000e150) /src/api/api.go:34 +0x76b github.com/anilkusc/backend/api.(*Api ).Start(0xc00000e150) /src/api/api.go:87 +0x25 main.main() /src/main.go:48 +0x15f
when I try to connect postgresql from another container cli, I got the following:当我尝试从另一个容器 cli 连接 postgresql 时,我得到以下信息:
root@8a0824fca084:/# nc -vz postgres 5432
Connection to postgres (172.25.0.2) 5432 port [tcp/postgresql] succeeded!
root@8a0824fca084:/# curl postgres:5432
curl: (52) Empty reply from server
Here is related code block:这是相关的代码块:
d.Database, err = gorm.Open(postgres.Open(os.Getenv("DB_CONN")), &gorm.Config{})
if err != nil {
return err
}
Here is compose file:这是撰写文件:
version: '3.6'
services:
postgres:
image: postgres:14.5
restart: always
environment:
POSTGRES_PASSWORD: <>
POSTGRES_DB: backenddev
POSTGRES_USER: devuser
ports:
- 5432:5432
backend:
image: my-service:v0.0.2
restart: always
environment:
ENV: dev
STORE_KEY: 1234
DB_CONN: host=postgres user=devuser password=<> dbname=backenddev port=5432
ports:
- 8080:8080
depends_on:
- postgres
Here is dockerfile of backend service:这是后端服务的 dockerfile:
FROM golang:1.18.4 as build
WORKDIR /src
COPY go.sum go.mod ./
RUN go mod download
COPY . .
RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o /bin/app.
FROM alpine
WORKDIR /app
COPY --from=build /bin/app .
CMD ["./app"]
If I try to connect to database with external ip and port on backend service it is also successfull but no luck for internal connection to postgresql.如果我尝试使用外部 ip 和后端服务端口连接到数据库,它也是成功的,但内部连接到 postgresql 没有运气。
Why my application can not resolve postgres host even though its host container can?为什么我的应用程序无法解析 postgres 主机,即使它的主机容器可以?
thanks to @Brits.感谢@Brits。 It is probably related with incompatibility between alpine container and golang 1.18.4 container dns libraries.
这可能与 alpine 容器和 golang 1.18.4 容器 dns 库之间的不兼容有关。 I changed my container image to debian from alpine and the issue has been resolved.
我将容器映像从 alpine 更改为 debian,问题已解决。
FROM golang:1.18.4 as build
WORKDIR /src
COPY go.sum go.mod ./
RUN go mod download
COPY . .
RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o /bin/app.
FROM debian:buster-slim #just changed here!
WORKDIR /app
COPY --from=build /bin/app .
CMD ["./app"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.