无法从Golang容器连接到Postgres Docker容器

编程入门 行业动态 更新时间:2024-10-28 17:23:05
本文介绍了无法从Golang容器连接到Postgres Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个使用golang构建的Web服务器。

I have a web server built using golang. It works successfully when I test it locally.

但是,当我为Web服务器构建docker映像时,它无法连接到正在运行的Postgres容器。

However, when I build a docker image for my web server, it can't connect to a running Postgres container.

这是我的 docker-compose.yml :

version: '2' services: go: image: golang:1.7 volumes: - ./:/server/http ports: - "80:8080" links: - postgres - mongodb - redis environment: DEBUG: 'true' PORT: '8080' postgres: image: onjin/alpine-postgres:9.5 restart: unless-stopped ports: - "5432:5432" environment: LC_ALL: C.UTF-8 POSTGRES_USER: user POSTGRES_PASSWORD: pass POSTGRES_DB: mydb mongodb: image: mvertes/alpine-mongo:3.2.3 restart: unless-stopped ports: - "27017:27017" redis: image: sickp/alpine-redis:3.2.2 restart: unless-stopped ports: - "6379:6379"

我的 Dockerfile :

FROM golang:1.7 RUN mkdir -p /home/app WORKDIR /home/app COPY . /home/app RUN make deps && make ENTRYPOINT ["./bin/api-test"] EXPOSE 8080

我正在使用的Postgres连接字符串:

The Postgres connection string I am using:

postgresql://user:pass@host/mydb?sslmode=disable

对于主机,我尝试使用 localhost 并返回以下错误:

For host, I tried localhost and it returns the following error:

dial tcp [::1]:5432: getsockopt: connection refused

尝试 postgres 并返回以下内容:

Tried postgres and it returns the following:

dial tcp 202.71.99.194:5432: getsockopt: connection refused

尝试运行此命令的IP地址,该命令返回 172.19.0.3 :

Tried the IP address I get running this command which returns 172.19.0.3:

docker inspect apitest_postgres_1 | grep IPAddress

其中 apitest_postgres_1 是Postgres容器名称。它还返回了此错误:

where apitest_postgres_1 is Postgres container name. It also returned this error:

dial tcp 172.19.0.3:5432: getsockopt: connection timed out

你能告诉我我在这里想念的吗?我对Docker缺乏经验,这花了很长时间研究解决方案。

Can you please tell me what I am missing here? I am inexperienced with docker and this took a long time investigating for a solution.

编辑: 我使用以下命令运行golang docker该命令:

I run my golang docker using this command:

docker run --env-file ./example.env --rm -it -p 8080:8080 api-test

example.env 是文件

编辑2: 我将连接字符串更改为以下内容:

Edit 2: I changed the connection string to the following:

postgresql://user:pass@postgres:5432?sslmode=disable

它将返回以下错误:

dial tcp: lookup postgres on 192.168.65.1:53: no such host

我的主意是我的Mac是这里的问题。我的默认DNS是 8.8.8.8 ,这应该没问题。

I'm getting the idea that my mac is the issue here. My default DNS is 8.8.8.8 which should not be a problem.

推荐答案

看起来像是拉图像而不是构建自己的图像。

Looks like you're pulling go image instead of building you're own image.

而不是 image:golang:1.7 替换为 build:。来构建并使用您的 Dockerfile 。

Instead of image: golang:1.7 replace it with build: . to build and use your Dockerfile.

另外,您可能需要传递postgres环境变量 DB_HOST , DB_USER , DB_PASS 等。您可以实现这一点,但是创建例如 docker.env 文件,然后添加 env_file 在您的go app docker-compose.yml 文件下:

Also you might need to pass postgres environment variables DB_HOST, DB_USER, DB_PASS etc. you can achieve that but creating for example docker.env file and then add env_file under your go app docker-compose.yml file:

docker.env示例:

Example docker.env :

DB_HOST=postgres DB_USER=user DB_PASS=pass DB_NAME=mydb

正确的docker-compose.yml:

Corrected docker-compose.yml :

version: '2' services: app: build: . volumes: - ./:/server/http ports: - "80:8080" links: - postgres - mongodb - redis environment: DEBUG: 'true' PORT: '8080' env_file: - docker.env postgres: image: onjin/alpine-postgres:9.5 restart: unless-stopped ports: - "5432:5432" environment: LC_ALL: C.UTF-8 POSTGRES_USER: user POSTGRES_PASSWORD: pass POSTGRES_DB: mydb mongodb: image: mvertes/alpine-mongo:3.2.3 restart: unless-stopped ports: - "27017:27017" redis: image: sickp/alpine-redis:3.2.2 restart: unless-stopped ports: - "6379:6379"

更多推荐

无法从Golang容器连接到Postgres Docker容器

本文发布于:2023-10-14 12:29:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491059.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   连接到   Golang   Postgres   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!