title: Access Docker postgres container from another container
body: <p>I am trying to make a portable solution to having my application container connect to a postgres container.  By 'portable' I mean that I can give the user two <code>docker run</code> commands, one for each container, and they will always work together.</p>
  
  <p>I have a postgres docker container running on my local PC, and I run it like this,</p>
  
  <pre><code>docker run -p 5432:5432 -v $(pwd)/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=qwerty -d postgres:11
  </code></pre>
  
  <p>and I am able to access it from a python flask app, using the address <code>127.0.0.1:5432</code>.</p>
  
  <p>I put the python app in a docker container as well, and I am having trouble connecting to the postgres container.</p>
  
  <p>Address <code>127.0.0.1:5432</code> does not work.</p>
  
  <p>Address <code>172.17.0.2:5432</code> <strong>DOES</strong> work (172.17.0.2 is the address of the docker container running postgres).  However I consider this not portable because I can't guarantee what the postgres container IP will be.</p>
  
  <p>I am aware of the <code>--add-host</code> flag, but it is also asking for the host-ip, which I want to be the localhost (127.0.0.1).  Despite several hits on <code>--add-host</code> I wasn't able to get that to work so that the final docker run commands can be the same on any computer they are run on.</p>
  
  <p>I also tried this: <a href="https://stackoverflow.com/questions/42318770/docker-container-port-accessed-from-another-container">docker container port accessed from another container</a></p>
  
  <p>My situation is that the postgres and myApp will be containers running on the same computer.  I would prefer a non-Docker compose solution.</p>