[英]docker-compose postgres dial error (dial tcp 172.23.0.3:5432: connect: connection refused)
I try to connect from backend container to postgres container.我尝试从后端容器连接到 postgres 容器。 Here is my docker-compose file:
这是我的 docker-compose 文件:
version: "3.9"
services:
imgress-producer:
build:
context: ./producer
dockerfile: Dockerfile.producer
target: prod
container_name: imgress-producer
ports:
- 8080:8080
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- DATABASE_HOST=${DATABASE_HOST}
- DATABASE_PORT=${DATABASE_PORT}
depends_on:
- imgress-db
volumes:
- ./:/app
networks:
- imgress-network
imgress-db:
image: postgres
container_name: imgress-db
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- DATABASE_HOST=${DATABASE_HOST}
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- 5432:5432
networks:
- imgress-network
restart: always
volumes:
postgres-data:
networks:
imgress-network:
driver: bridge
The.env file: .env 文件:
POSTGRES_USER=postgres
POSTGRES_PASSWORD=root
POSTGRES_DB=imgress
DATABASE_HOST=imgress-db
DATABASE_PORT=5432
And here is how I try to connect to db:这是我尝试连接到数据库的方式:
package database
import (
"fmt"
"log"
"os"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var GDB *gorm.DB
func ConnectDB() {
var err error
pgPort := os.Getenv("DATABASE_PORT")
pgHost := os.Getenv("DATABASE_HOST")
pgUser := os.Getenv("POSTGRES_USER")
pgPassword := os.Getenv("POSTGRES_PASSWORD")
pgName := os.Getenv("POSTGRES_DB")
configData := fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
pgUser,
pgPassword,
pgHost,
pgPort,
pgName,
)
for i := 0; i < 5; i++ {
GDB, err = gorm.Open(postgres.Open(configData), &gorm.Config{})
if err == nil {
break
}
time.Sleep(10 * time.Second)
}
if err != nil {
log.Println("Producer: Error Connecting to Database")
} else {
log.Println("Producer: Connection Opened to Database")
}
}
So, in the last part, I retry until db container is ready.因此,在最后一部分,我重试直到数据库容器准备就绪。 So it should log an error when db connection is unsuccessful.
所以当数据库连接不成功时它应该记录一个错误。 But instead, it fails to connect and logs a success.
但相反,它无法连接并记录成功。
imgress-producer | 2022/12/03 20:28:24 /go/pkg/mod/gorm.io/gorm@v1.24.1/gorm.go:206
imgress-producer | [error] failed to initialize database, got error failed to connect to `host=imgress-db user=postgres database=imgress`: dial error (dial tcp 172.23.0.3:5432: connect: connection refused)
imgress-producer | 2022/12/03 20:28:34 Producer: Connection Opened to Database
There is a lot of connection refused related questions asked on SO, but none of them helped me. SO 上有很多连接被拒绝的相关问题,但没有一个对我有帮助。 So any kind of help is appreciated.
因此,我们将不胜感激任何形式的帮助。
I simplified your code a little bit and I was able to make it work on my machine.我稍微简化了您的代码,并且能够在我的机器上运行它。 Let's see it.
让我们来看看它。 The repo structure is:
回购结构是:
.env
docker-compose.yaml
Dockerfile
main.go
Let's start with the main.go
file.让我们从
main.go
文件开始。
main.go
package main
import (
"fmt"
"log"
"os"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var GDB *gorm.DB
func ConnectDB() {
var err error
pgPort := os.Getenv("DATABASE_PORT")
pgHost := os.Getenv("DATABASE_HOST")
pgUser := os.Getenv("POSTGRES_USER")
pgPassword := os.Getenv("POSTGRES_PASSWORD")
pgName := os.Getenv("POSTGRES_DB")
configData := fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
pgUser,
pgPassword,
pgHost,
pgPort,
pgName,
)
for i := 0; i < 5; i++ {
GDB, err = gorm.Open(postgres.Open(configData), &gorm.Config{})
if err == nil {
break
}
time.Sleep(1 * time.Second) // change back to 10s
}
if err != nil {
log.Println("Producer: Error Connecting to Database")
} else {
log.Println("Producer: Connection Opened to Database")
}
}
func main() {
ConnectDB()
}
No relevant changes here.这里没有相关的变化。
.env
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
DATABASE_HOST=imgress-db
DATABASE_PORT=5432
Also here nothing to mention.这里也没什么好说的。
Dockerfile
FROM golang:1.19.3-alpine3.17 AS build
WORKDIR /go/src/app
COPY ./main.go ./main.go
RUN go mod init pgdockercompose
RUN go mod tidy
RUN go build -o ./bin/webserver ./main.go
FROM alpine:3.17
COPY --from=build /go/src/app/bin /go/bin
EXPOSE 8080
ENTRYPOINT go/bin/webserver
Here we used the multi-staged build to build and copy our Go program.这里我们使用多阶段构建来构建和复制我们的 Go 程序。 In the
build
stage we used a bigger image to initialize a go module, restore the dependencies and, to build the source code.在
build
阶段,我们使用了一个更大的图像来初始化 go 模块,恢复依赖关系,并构建源代码。 While in the leaner image we copy the outcome of our build process.在更精简的图像中,我们复制构建过程的结果。
docker-compose.yaml
version: "3.9"
services:
imgress-producer:
build: "."
ports:
- 8080:8080
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- DATABASE_HOST=${DATABASE_HOST}
- DATABASE_PORT=${DATABASE_PORT}
depends_on:
- imgress-db
networks:
- imgress-network
imgress-db:
image: postgres
container_name: imgress-db
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- DATABASE_HOST=${DATABASE_HOST}
ports:
- 5432:5432
restart: always
networks:
- imgress-network
networks:
imgress-network:
driver: bridge
For the sake of the demo, I'll leave out the volumes.为了演示,我将省略卷。 It shouldn't big a pain to integrate also them too.
整合它们也不应该很痛苦。
Hope that this clarifies a little bit your doubt!希望这可以澄清您的疑问!
A workaround solution.解决方法。 if you looked here I bet you have been through a log of "changing hostname from localhost to DB".
如果你看过这里,我敢打赌你已经浏览过“将主机名从 localhost 更改为 DB”的日志。 None of that worked for me.
这些都不适合我。 I have gone through every documentation for the docker-compose.network but it just does not work.
我已经阅读了 docker-compose.network 的所有文档,但它不起作用。 Here is the solution
这是解决方案
app:
// other stuff
- network_mode: "host"
db:
- expose:
- "5432" // or the port you want
this will expose your DB to the host machine.network and your app will connect to your DB through the host machine.这会将您的数据库暴露给主机。网络,您的应用程序将通过主机连接到您的数据库。 No very ideal, but the best solution found so far.
不是很理想,但目前找到的最佳解决方案。 If anyone has a real solution and has tried it yourself.
如果有人有真正的解决方案并且自己尝试过。 Please let me know.
请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.