dourong9253
2019-07-12 20:07
浏览 170

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drj58429 2019-07-12 20:19

    fixed it now

    I was referencing localhost rather than the docker container host

    by using the link "links: postgres-db" I needed to make sure my service was looking for the host "postgres-db" and not "0.0.0.0" or "localhost"

    version: '3.5'
    services:
        app:
          build: .
          ports:
            - "7000:7000"
          environment:
            pgHost: "postgres-db"
            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:
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题