dourong9253 2019-07-12 20:07
浏览 171
已采纳

GoLang服务无法使用docker compose命中Postgres

I have an HTML service written in Go. It uses Postgres, but when bringing it all together using docker compose I get " dial tcp 0.0.0.0:5432: connect: connection refused"

Works when building launching service using just docker and referencing a running image of Postgres

snippet of call from 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
    }

for my own confirmation when debugging

    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)

Dockerfile

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)
  • 写回答

2条回答 默认 最新

  • douhoujun9304 2019-07-12 20:11
    关注

    You should use postgres-db(better to give it simple name without dash, db for example) for database host setting, instead of 0.0.0.0

    A container is created using postgres-db's configuration. It joins the network under the name postgres-db.

    Each container can look up the hostname app or postgres-db and get back the appropriate container’s IP address. For example, app’s application code could connect to the URL postgres://postgres-db:5432 and start using the Postgres database.

    Here is more information

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决