简体   繁体   English

Docker golang gin postgres

[英]Docker golang gin postgres

I am trying to set up a docker for golang app with Postgres.我正在尝试使用 Postgres 为 golang 应用程序设置 docker。 The go app works fine in a container if I remove/comment Postgres.如果我删除/评论 Postgres,go 应用程序在容器中运行良好。 And similarly, I am able to spin up Postgres container and log into it.同样,我能够启动 Postgres 容器并登录它。 I am able do docker-compose up.我可以做 docker-compose up。 But when I make a API call, like for eg: localhost:3000/api/admin/users .但是当我进行 API 调用时,例如: localhost:3000/api/admin/users It gives and error:它给出和错误:

error: {
        "error": "+dial tcp 127.0.0.1:5432: connect: connection refused"
    }

The Postgres connection string is like this: Postgres 连接字符串是这样的:

connStr := fmt.Sprintf("host=postgres user=anurag password=anu_12345 dbname=bankingapp sslmode=disable")

db, err := sql.Open("postgres", connStr) db, err := sql.Open("postgres", connStr)

Dockerfile文件

FROM golang:1.13


WORKDIR /go/src/banking-app
COPY . .

RUN go get -d -v ./...
RUN go install -v ./...

CMD ["go" , "run", "main.go"]


docker-compose.yml docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
      - "3000:3000"
  postgres:
    image: "postgres"
    environment:
      POSTGRES_USER: 'anurag'
      POSTGRES_PASSWORD: 'anu_12345'
      POSTGRES_DB: 'bankingapp'


Seems to be some missing ports to be exposed as mentioned by @Oras ports: 5432:5432 just add it to exclude ports from the issue, also from the error you have, it seems that your docker app container depends on the database container so you need to have a way to wait until your database container is up and your app container can connect it, check depends_on for docker compose:似乎是@Oras 端口提到的一些缺少的端口要公开: 5432:5432只需添加它以从问题中排除端口,也从您遇到的错误中排除端口,似乎您的 docker 应用程序容器依赖于数据库容器,因此您需要有一种方法来等待您的数据库容器启动并且您的应用程序容器可以连接它,检查depends_on compose 的depends_on

https://docs.docker.com/compose/compose-file/ https://docs.docker.com/compose/compose-file/

version: '3'
services:
web:
  build: .
  ports:
    - "3000:3000"
  depends_on:
    postgres
  restart_policy:
    condition: on-failure
  postgres:
    image: "postgres"
    ports:
      - "3000:3000"
    environment:
      POSTGRES_USER: 'anurag'
      POSTGRES_PASSWORD: 'anu_12345'
      POSTGRES_DB: 'bankingapp'

There are several things to be aware of when using depends_on:使用depends_on时需要注意以下几点:

  1. depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started.在启动 web 之前,depends_on 不会等待 db 和 redis “准备好” - 只在它们启动之前。 If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.如果您需要等待服务准备就绪,请参阅控制启动顺序以了解有关此问题的更多信息以及解决该问题的策略。

  2. Version 3 no longer supports the condition form of depends_on.版本 3 不再支持depends_on 的条件形式。

  3. The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.在使用版本 3 Compose 文件以 swarm 模式部署堆栈时,将忽略depends_on 选项。

and based on the the above note you may still face the issue for but the restart policy will restart the app container and you will connect to the database.并且根据上面的说明,您可能仍然会遇到问题,但重启策略将重启应用程序容器,您将连接到数据库。

I found the answer.我找到了答案。 Just need to rebuild image or load using mount.只需要重建镜像或使用 mount 加载。 The code was not refreshed.代码没有刷新。

Sorry to bother.抱歉打扰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM