doupang3062
doupang3062
2018-07-18 20:52

无法将容器化的应用程序连接到容器化的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 dongya767979565 3年前

    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
    点赞 评论 复制链接分享