【问题标题】:Golang docker error while connecting to postgres连接到 postgres 时出现 Golang docker 错误
【发布时间】:2021-08-30 04:04:26
【问题描述】:

我正在尝试使用 docker-compose.yml 文件对我的 golang rest api 进行 docker 化。

假设目前我没有任何 docker 图像。我只是

运行 docker-compose up -d

它会自动创建 2 个 docker 镜像 1 个数据库和 1 个代码。

但是当我通过运行命令查看日志时

码头工人撰写日志

我得到了这些日志

Attaching to posty-api_api_1, postgres
postgres    | 
postgres    | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres    |
postgres    | 2021-06-14 07:37:46.437 UTC [1] LOG:  starting PostgreSQL 13.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.2.1_pre1) 10.2.1 20201203, 64-bit
postgres    | 2021-06-14 07:37:46.438 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres    | 2021-06-14 07:37:46.438 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres    | 2021-06-14 07:37:46.481 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres    | 2021-06-14 07:37:46.607 UTC [22] LOG:  database system was shut down at 2021-06-14 07:33:40 UTC
postgres    | 2021-06-14 07:37:46.696 UTC [1] LOG:  database system is ready to accept connections
api_1       | main.go start
api_1       | database.go START
api_1       |
api_1       | 2021/06/14 07:37:45 /app/database/database.go:23
api_1       | [error] failed to initialize database, got error failed to connect to `host=postgres user=postgres database=postgres`: dial error (dial tcp 172.22.0.2:5432: connect: connection refused)
api_1       | panic: failed to connect database
api_1       |
api_1       | goroutine 1 [running]:
api_1       | github.com/usman-174/database.ConnectDataBase(0xc0001ae2f0)
api_1       |   /app/database/database.go:26 +0x49a
api_1       | github.com/usman-174/app.Router(0xa7c120)
api_1       |   /app/app/app.go:10 +0x26
api_1       | main.main()
api_1       |   /app/main.go:16 +0x9b

我无法连接数据库。即使我提供了正确的环境变量。

这是我的 .env 文件:

DSN=host=postgres user=postgres password=postgres dbname=postgres port=5432 sslmode=disable
CLIENT_URL=https://clever-montalcini-cedd07.netlify.app/
PORT=:8080

这是我的 dockerfile:

FROM golang:1.16.5-alpine3.13 AS builder
WORKDIR /app
COPY . .
RUN go build -o main main.go

# Run stage
FROM alpine:3.13
WORKDIR /app
COPY --from=builder /app/main .
COPY .env .
EXPOSE 8080

这是我的 docker-compose.yml 文件:

# Specify the version for docker-compose.yml
version: "3.8"

# add the serivces needed (postgres,go)
services:
  postgres:
    container_name: postgres
    image: postgres:13-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: auth
      # Optional: Give a name to the database, otherwise
      # use the default value POSTGRES_USER as a database name (user in this case.)
      POSTGRES_DB: auth
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    ports:
      - "5432:5432"
    volumes:
      - ./pgdata:/var/lib/postgresql/data

  api:
    build:
      context: .
      dockerfile: Dockerfile
    command: ["./wait-for-it.sh", "postgres:5432", "--", "/app/main" ]
    ports:
      - "8080:8080"
    depends_on:
      - postgres
    command: [ "/app/main" ]

这就是我连接数据库的方式:

func ConnectDataBase() *gorm.DB {
fmt.Println("database.go START")
err := godotenv.Load()

if err != nil {
    log.Fatal("Error loading .env file")
}
dsn := os.Getenv("DSN")
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

if err != nil {
    panic("failed to connect database")
}
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Post{})
db.AutoMigrate(&models.Like{})
fmt.Println("database.go STOP")

return db
}

【问题讨论】:

  • 只需使用 docker exec -it /bin/sh 登录到 api 容器,然后运行 ​​curl 或使用其名称(即 postgress) ping postgress 容器。看看你是否可以到达数据库。在 docker-compose 中创建了一个默认网络,所有容器都放置在默认网络中。所以 postgress 数据库应该是可访问的
  • 我可以得到一个连接这是响应: 64 bytes from 172.23.0.2: seq=28 ttl=64 time=0.074 ms 64 bytes from 172.23.0.2: seq=29 ttl=64 time= 0.068 毫秒 64 字节,来自 172.23.0.2:seq=30 ttl=64 time=0.062 ms

标签: postgresql docker go docker-compose


【解决方案1】:

DSN 上的数据库名称似乎不正确。

在您的docker-compose.yml 上是:

POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: auth
...

同时,在您的 .env 上是:

DSN=host=postgres user=postgres password=postgres dbname=postgres ...

尝试在.env 文件上使用dbname=auth 而不是dbname=postgres

【讨论】:

  • 像这样在 docker-compose.yml: environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DBNAME: auth
  • 我的意思是,因为在你的docker-compose.yml 中,数据库是auth,那么在你的.env 上应该是DSN=host=postgres user=postgres password=postgres dbname=auth port=5432 sslmode=disable
  • 如果这个解决方案有效,这个答案可能会被接受。
猜你喜欢
  • 2014-04-25
  • 2019-10-08
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 2017-06-06
  • 2021-07-14
相关资源
最近更新 更多