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 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥80 LS dyna mpp并行报错
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入
  • ¥15 NodeBB论坛配置Apache Solr中文搜索引擎的详细教程