0

I am using Docker on Mac. I run container1 with Postgres, publishing port 5432. From the host, I can connect using psql: psql -h localhost -d postgres -U postgres -W.

I then run container2. From container2, I can run ping host.docker.internal and see that it's working. However, I cannot access Postgres with the same psql command.

Why doesn't this work, and what can I do to make it work? I thought that, because I can access the host from within container2, trying to connect to port 5432 would be mapped to container1.

allstar
  • 1,155
  • 4
  • 13
  • 29
  • try with this command: `psql -U postgres -p 5432 -h -w -d ` – Ali Jun 23 '22 at 14:47
  • Each container is its own `localhost`. You need to run both containers on the same Docker network, and then the `docker run --name` of the first container will be usable as a DNS name from the second container. If you're using Docker Compose the setup is done automatically for you. The linked question has some more examples (see its Compose and `docker network` answer, ignore the "links" ones). You do not need `host.docker.internal` or to manually find the other container's Docker-internal IP address. – David Maze Jun 23 '22 at 15:22
  • Thanks! I had thought _anything_ accessing the physical host port 5432 is directed to container1, regardless of whether that request originates on the physical host, container2, another machine, etc. Is it that it's the Docker backend process, which is what's actually listening on 5432, determines what can talk to what based on the networking you set up in Docker? – allstar Jun 24 '22 at 08:53

0 Answers0