0

I am trying to run Postgres in a container.

When I start the container using the following command wherein I map the port 5432 of my machine with that of the container, the Postgres accepts connections from another process and everything works as intended.

docker run --name postgres --rm -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 -d postgres

netstat-ing the port also works well i.e

nc -z localhost 5432
Connection to localhost port 5432 [tcp/postgresql] succeeded!

Now if I use the host mode to run the postgres container, it stops accepting connections. Basically the following doesn't work:

docker run --name postgres --rm -e POSTGRES_HOST_AUTH_METHOD=trust --net=host -d postgres

I saw a similar question on StackOverflow but it doesn't explain why things don't work. Here is the link to that question: Connection Error with docker postgres using network=host

Any ideas why the second command doesn't work are appreciated. Thank you.

arl
  • 27
  • 1
  • 7
  • [I can't reproduce your issue](https://gist.github.com/zeitounator/9be26c49de0613992eac60ba5567d6da) – Zeitounator Mar 13 '22 at 09:03
  • `--net host` is almost never necessary. Since you have a working setup with the correct `docker run -p` option, can you just use that? When you say "it doesn't work", what does happen? – David Maze Mar 13 '22 at 10:52
  • @DavidMaze Agreed. I can simply use the docker run -p option. However, it is really troubling me that I cannot use the host network. Also when I say it doesn't work, I mean I am unable to connect to postgres from the process I am trying to run. – arl Mar 13 '22 at 11:57

1 Answers1

0

I wasn't aware of the fact that host networking is not supported on mac.

A snippet from https://docs.docker.com/network/host/

The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

Another related SO question: How to tell docker on mac to use host network for all purpose?

arl
  • 27
  • 1
  • 7