title: Docker container for Postgres 9.1 not exposing port 5432 to host
body: <p>I'm trying to use a Docker container to run a PostgreSQL server, and connect with it from my host machine.</p>
  
  <p>My configuration is:</p>
  
  <ul>
  <li>Host machine: Mac OS X 10.10.5</li>
  <li>Docker 1.10.1</li>
  </ul>
  
  <p>I've done this:</p>
  
  <p><strong>Step 1</strong>: create a volume for permanent postgres data</p>
  
  <pre><code>docker volume create --name postgres_data
  </code></pre>
  
  <p><strong>Step 2</strong>: Start the postgres instance</p>
  
  <p><strong>UPDATE</strong>: As suggested in comments, I specified port mapping when running the container</p>
  
  <pre><code>docker run --name my_postgres_container -e POSTGRES_PASSWORD=my_password -v postgres_data:/var/lib/postgresql/data -p 5432:5432 -d postgres:9.1
  </code></pre>
  
  <p><strong>Step 3</strong>: connect to Docker instance by doing this:</p>
  
  <pre><code>docker run -it --link my_postgres_container:postgres --rm postgres:9.1 sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
  </code></pre>
  
  <p>But I want to connect to that instance just by:</p>
  
  <pre><code>psql -h localhost -p 5432 -U postgres
  </code></pre>
  
  <p>Like if I had installed Postgres locally in my host machine. </p>
  
  <p>The problem is <strong>port 5432 is not exposed</strong>. So, I can't connect with it:</p>
  
  <p><code>sudo lsof -i -P | grep -i "listen"</code> --> no port 5432 open</p>
  
  <p><strong>UPDATE 2</strong>: Even stranger. I've also done this:</p>
  
  <p>Stop Docker. Then, run a normal PostgreSQL 9.4.4 instance <strong>in my host machine</strong> (no docker involved here, just postgres running in my Mac OS X host, listening on port 5432). Everything is normal:</p>
  
  <pre><code>sudo lsof -i -P | grep -i "postgres"
  
  postgres  14100          jorge    6u  IPv4 0x780274158cebef01      0t0  TCP localhost:5432 (LISTEN)
  </code></pre>
  
  <p>I can connect with my local postgres instance without any problem (look the output of the command: is the postgres compiled for Mac OS X, my host):</p>
  
  <pre><code>psql -h localhost -U postgres -c "select version()"
                                                                  version
  ---------------------------------------------------------------------------------------------------------------------------------------
   PostgreSQL 9.4.4 on x86_64-apple-darwin14.3.0, compiled by Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn), 64-bit
  (1 row)
  </code></pre>
  
  <p>Now the fun part. I start my Docker instance again, <strong>while the host PostgreSQL instance is running.</strong> </p>
  
  <p>It starts! (and it shouldn't). I can even connect using docker run...</p>
  
  <pre><code>docker run -it --link my_postgres_instance:postgres --rm postgres:9.1 sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
  </code></pre>
  
  <p>If I run select version() now, <strong>it shows postgres running inside my docker instance at the same time postgres is running in my host, out of docker, using the same 5432 port</strong>.  (Look at the ouput, is postgres compiled for Debian, the OS inside the postgres:9.1 container)</p>
  
  <pre><code>postgres=# select version();
                                              version
  ------------------------------------------------------------------------------------------------
   PostgreSQL 9.1.20 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
  (1 row)
  </code></pre>
  
  <p>Why?</p>
  
  <p>Does it make sense? My final goal is <strong>to run a Django app in another Docker container and connect with my Postgres instance</strong>. How could I do that? </p>