doupang3062 2018-07-18 20:52
浏览 100
已采纳

无法将容器化的应用程序连接到容器化的postgres,收到拨号TCP连接拒绝错误

This community is my last resort for this problem, as I have been fighting with this for several hours now.

I have a go app running in one container, in the other container I am running a postgres db. I am able to connect to the postgres db from my go application as long as only my postgres is within a container, and my go app is running locally as usual. However, when my go app is trying to access the postgres from within a docker container i am getting the following error:

dial tcp 127.0.0.1:8080: connect: connection refused 

Below I try to provide enough information, but will gladly add more if requested.


I have 2 docker containers running with the following ports:

  • go application, port info: 8081/tcp -> 0.0.0.0:8081
  • postgres db, port info: 5432/tcp -> 0.0.0.0:8080

I am running the go app with:

docker run -it --rm --name gographqlserver --link postgresdb:postgres -d -p 8081:8081 gogogopher;

and the postgres db with:

docker run -it --rm --name postgresdb -e POSTGRES_PASSWORD=hello123 -d -p 8080:5432 postgresimage;

both containers can be started without any problems.


I have also tried connecting both containers within a docker network, which did not help.


help would be immensely appreciated!

  • 写回答

1条回答 默认 最新

  • dongya767979565 2018-07-18 21:01
    关注

    You are using localhost address within the container which is not the same as your host's address. You should do one of the following instead:

    • Use your actual host's IP from app's container
    • Use postgresdb container's IP with the native port (5432). You can discover this IP using docker inspect postgresdb.
    • Use postgresdb as host name and the native port (5432) when connecting both containers to the same network
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的算法代码
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据