title: Connection Error with docker postgres using network=host
body: <h1>Problem</h1>
  <p>I'm unable to connect to a PostgreSQL database (14.1/latest) running in a docker image on my laptop.
  I'm trying to connect to the database both with .NET EntityFrameworkCore/Npgsql running in another container on the laptop, and with <code>psql</code> running directly on the laptop.</p>
  <h2>Environment</h2>
  <ul>
  <li>MacOS 10.15.7</li>
  <li>Docker Desktop 4.3.2 (72729)</li>
  </ul>
  <h1>Recreate</h1>
  <p>I start my postgres container like so. Note that since it's in host network mode, I don't publish ports.</p>
  <pre class="lang-sh prettyprint-override"><code>docker run -d --network=host --name=my-database -v dbdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres postgres
  docker ps
  CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS      NAMES
  18e154fe815c   postgres   &quot;docker-entrypoint.s…&quot;   4 minutes ago   Up 4 minutes   5432/tcp   my-database
  
  </code></pre>
  <p>Logs from the <code>my-database</code> container show</p>
  <pre><code>...
  2022-01-11 16:56:18.179 UTC [1] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
  2022-01-11 16:56:18.179 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
  2022-01-11 16:56:18.179 UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
  2022-01-11 16:56:18.182 UTC [1] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
  2022-01-11 16:56:18.188 UTC [26] LOG:  database system was shut down at 2022-01-11 16:35:54 UTC
  2022-01-11 16:56:18.203 UTC [1] LOG:  database system is ready to accept connections
  </code></pre>
  <p>At this point I expect to be able to connect to the database, but I get an error.</p>
  <pre class="lang-sh prettyprint-override"><code>$ psql -h localhost -p 5432 -U postgres
  psql: error: connection to server at &quot;localhost&quot; (::1), port 5432 failed: Connection refused
          Is the server running on that host and accepting TCP/IP connections?
  connection to server at &quot;localhost&quot; (127.0.0.1), port 5432 failed: Connection refused
          Is the server running on that host and accepting TCP/IP connections?
  
  </code></pre>