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 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。