4

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!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jonas Bergner
  • 1,663
  • 4
  • 15
  • 22
  • Possible duplicate of [Allow docker container to connect to a local/host postgres database](https://stackoverflow.com/questions/31249112/allow-docker-container-to-connect-to-a-local-host-postgres-database) – David Maze Jul 18 '18 at 22:56

1 Answers1

12

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
r3mus n0x
  • 5,954
  • 1
  • 13
  • 34