title: Docker: How to fix "could not translate host name "postgres" to address" when connecting from one container to another using container name?
body: <p>I start 3 container:</p>
  <pre><code>docker run -d --rm --name redis    --publish 6379:6379 redis 
  docker run -d --rm --name postgres --publish 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=hi postgres:9.6
  docker run -d --rm --name web      --publish 8000:8000 python:3.6.8-stretch sleep 1000
  </code></pre>
  <p>Doing <code>docker ps</code> shows:
  <a href="https://i.stack.imgur.com/DV82N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DV82N.png" alt="enter image description here" /></a></p>
  <p>Then I exec into <code>web</code>:</p>
  <pre><code>docker exec -it web bash
  </code></pre>
  <p>Then I install <code>psycopg2</code>:</p>
  <pre><code>$ pip install psycopg2
  </code></pre>
  <p>Then I try to connect to the <code>host: postgres</code>, <code>port:5432</code>, <code>database:hi</code>:</p>
  <p>I start python:</p>
  <pre><code>$ python
  </code></pre>
  <p>Then I run these commands:</p>
  <pre><code>import psycopg2
  conn = psycopg2.connect(host=&quot;postgres&quot;, port = 5432, database=&quot;hi&quot;, user=&quot;postgres&quot;, password=&quot;&lt;passwd&gt;&quot;)
  </code></pre>
  <p>And I get the following error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py&quot;, line 127, in connect
      conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  psycopg2.OperationalError: could not translate host name &quot;postgres&quot; to address: Name or service not known
  </code></pre>
  <p>I thought it might be a docker network issue, so I went to inspect it, and all the hostnames are there:</p>
  <pre><code>{
      &quot;Name&quot;: &quot;bridge&quot;,
      &quot;Id&quot;: &quot;8e37606d20ae80189da4d7ecdb7673274fa2e0c9dd2b5c2b4c3db4e545c1ef20&quot;,
      &quot;Created&quot;: &quot;2020-05-14T19:10:43.532158344Z&quot;,
      &quot;Scope&quot;: &quot;local&quot;,
      &quot;Driver&quot;: &quot;bridge&quot;,
      &quot;EnableIPv6&quot;: false,
      &quot;IPAM&quot;: {
          &quot;Driver&quot;: &quot;default&quot;,
          &quot;Options&quot;: null,
          &quot;Config&quot;: [
              {
                  &quot;Subnet&quot;: &quot;172.17.0.0/16&quot;,
                  &quot;Gateway&quot;: &quot;172.17.0.1&quot;
              }
          ]
      },
      &quot;Internal&quot;: false,
      &quot;Attachable&quot;: false,
      &quot;Ingress&quot;: false,
      &quot;ConfigFrom&quot;: {
          &quot;Network&quot;: &quot;&quot;
      },
      &quot;ConfigOnly&quot;: false,
      &quot;Containers&quot;: {
          &quot;3bb2a6cf4fbb235324d335f238c20fe026e37bccc717d1cd0bd1df43e70dbec6&quot;: {
              &quot;Name&quot;: &quot;redis&quot;,
              &quot;EndpointID&quot;: &quot;f927fc2a199d033bbbc8ecc8ff1bccf508e607384e4820884f3236f11a961c04&quot;,
              &quot;MacAddress&quot;: &quot;02:42:ac:11:00:02&quot;,
              &quot;IPv4Address&quot;: &quot;172.17.0.2/16&quot;,
              &quot;IPv6Address&quot;: &quot;&quot;
          },
          &quot;545065d96fb6f867724ece2cf11881af26be9885f16212bef4260bccba00a2be&quot;: {
              &quot;Name&quot;: &quot;web&quot;,
              &quot;EndpointID&quot;: &quot;750133a6bb72e23929c57f32aa7776bc85d3dd255a16473a4afe230d0c6cb9c2&quot;,
              &quot;MacAddress&quot;: &quot;02:42:ac:11:00:04&quot;,
              &quot;IPv4Address&quot;: &quot;172.17.0.4/16&quot;,
              &quot;IPv6Address&quot;: &quot;&quot;
          },
          &quot;cfb2e556ca9f26ff8c7063f2347b81b2a90365cfa476cab8611edf1586404a88&quot;: {
              &quot;Name&quot;: &quot;postgres&quot;,
              &quot;EndpointID&quot;: &quot;069411d5c42b3b968ce38b92b7122c608f1dca696517122728f23086fdce53c7&quot;,
              &quot;MacAddress&quot;: &quot;02:42:ac:11:00:03&quot;,
              &quot;IPv4Address&quot;: &quot;172.17.0.3/16&quot;,
              &quot;IPv6Address&quot;: &quot;&quot;
          }
      },
      &quot;Options&quot;: {
          &quot;com.docker.network.bridge.default_bridge&quot;: &quot;true&quot;,
          &quot;com.docker.network.bridge.enable_icc&quot;: &quot;true&quot;,
          &quot;com.docker.network.bridge.enable_ip_masquerade&quot;: &quot;true&quot;,
          &quot;com.docker.network.bridge.host_binding_ipv4&quot;: &quot;0.0.0.0&quot;,
          &quot;com.docker.network.bridge.name&quot;: &quot;docker0&quot;,
          &quot;com.docker.network.driver.mtu&quot;: &quot;1500&quot;
      },
      &quot;Labels&quot;: {}
  }
  </code></pre>
  <p>It seems that the hostname <code>postgres</code> should be resolved to the <code>ip</code> of <code>172.17.0.3</code>.</p>
  <p>When I do that in python, it works:</p>
  <pre><code>import psycopg2
  conn = psycopg2.connect(host=&quot;172.17.0.3&quot;, port = 5432, database=&quot;hi&quot;, user=&quot;postgres&quot;, password=&quot;&quot;)
  </code></pre>
  <p>How can I connect to the container named <code>postgres</code> using the its name as a host?</p>