Answers: <p>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).</p>
  
  <p><a href="https://docs.docker.com/network/" rel="nofollow noreferrer">Docker Network Documentation</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-07-22T14:10:53.083Z
Answers: 2019-07-22T14:10:53.083Z
Answers: Wesley Rolnick
Answers: <p>Ah. Well a high level answer that allows for me to just 'get by' for now is this:</p>
  
  <p>Docker will automatically create a network when running one or more containers. You can check what a container's IP address is on this virtual network by running:</p>
  
  <pre><code>docker container inspect &lt;container name&gt;
  </code></pre>
  
  <p>I was able to find the gateway address of the postgres container that I want to query from and use it that way. </p>
  
Answers: CC BY-SA 4.0
Answers: 2019-07-22T10:03:42.470Z
Answers: 2019-07-22T12:09:42.080Z
Answers: 2019-07-22T12:09:42.080Z
Answers: Zach Smith
Answers: <p>you can use </p>
  
  <p>"docker network create "</p>
  
  <p>to create your own network. Once you do that when you are creating a container use the </p>
  
  <p>"--network "</p>
  
  <p>to create the 2 containers in the same network.The containers in the same network can interact with each other.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-07-23T08:54:38.610Z
Answers: 2019-07-23T08:54:38.610Z
Answers: Sammy
Body: <p>I have two docker containers running:</p>
  
  <pre><code>Postgres 9.4: 0.0.0.0:5431-&gt;5432/tcp
  Postgres 11: 0.0.0.0:5432-&gt;5432/tcp
  </code></pre>
  
  <p>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 <code>dblink</code> feature - i.e. (in case it matters), I am running a query on one server that looks like this:</p>
  
  <pre><code>select
  dp.*
  from dblink(
      'dbname=mydb
      port=5431
      host=localhost
      user=postgres
      password=password', 'select 1'
  ) as dp (i int)
  </code></pre>
  
  <p>This results in an error along that says:</p>
  
  <pre><code>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?
  </code></pre>
  
  <p>And I guess this is because <code>localhost</code> is the loopback address inside the container. What is the way around this? (I'm super new to Docker by the way)</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-07-22T09:49:53.697Z
Comments: It looks like there is a Stack Overflow answer here: https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
Comments: Zach Smith
Tags: <docker>
Title: How to resolve the host operating system 'localhost' address from within a container?
UserDisplayName: Zach Smith
ThreadText: # How to resolve the host operating system 'localhost' address from within a container?
  
  (Asked by: username_0 on 2019-07-22)
  
  I have two docker containers running:
  
  ```
  Postgres 9.4: 0.0.0.0:5431->5432/tcp
  Postgres 11: 0.0.0.0:5432->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)
  
  **Tags:** docker
  
  > **Comments:**
  > **username_0**: It looks like there is a Stack Overflow answer here: https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2019-07-22. Score: 1 | Accepted answer
  
  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](https://docs.docker.com/network/)
  
  
  ---
  
  ### Answer by username_0 on 2019-07-22. Score: 0
  
  Ah. Well a high level answer that allows for me to just 'get by' for now is this:
  
  Docker will automatically create a network when running one or more containers. You can check what a container's IP address is on this virtual network by running:
  
  ```
  docker container inspect <container name>
  
  ```
  
  I was able to find the gateway address of the postgres container that I want to query from and use it that way.
  
  
  ---
  
  ### Answer by username_2 on 2019-07-23. Score: 0
  
  you can use
  
  "docker network create "
  
  to create your own network. Once you do that when you are creating a container use the
  
  "--network "
  
  to create the 2 containers in the same network.The containers in the same network can interact with each other.
  
  
  ---
  
  