How to resolve the host operating system 'localhost' address from within a container? I have two docker containers running:

Postgres 9.4: 0.0.0.0:5431-&gt;5432/tcp
Postgres 11: 0.0.0.0:5432-&gt;5432/tcp


I can connect to both of them via clients on my computer, but I am unable to access one container from within another. I would like to use Postgres's dblink feature - i.e. (in case it matters), I am running a query on one server that looks like this:

select
dp.*
from dblink(
    'dbname=mydb
    port=5431
    host=localhost
    user=postgres
    password=password', 'select 1'
) as dp (i int)


This results in an error along that says:

Could not connect to server: Connection refused
  Is the server running on host "localhost" (127.0.0.1) and accepting
  TCP/IP connections on port 5431?


And I guess this is because localhost is the loopback address inside the container. What is the way around this? (I'm super new to Docker by the way)
You are correct in that using localhost within the container is referencing the container, and not your network.  You could use inspect, find the ip, and hardcode the ip.  However, a simpler and cleaner solution would be to utilize Docker's network features.  When you run the containers you can put the containers inside a defined network.  Containers in this network can resolve requests to other containers using their container name instead of an ip (e.g. Server=container2).

Docker Network Documentation