title: docker-compose with two containers: web can not connect to db
body: <p>docker-compose fails to build web component because it can not connect to the previously created db component </p>
  
  <p>Mac OSX 10.13.6, conda 4.5.11, Python 3.6.8, Docker version 18.09.1, docker-compose version 1.23.2</p>
  
  <p>django 1.8.3 gets installed with requirements.txt from Dockerfile. Not at liberty to upgrade.</p>
  
  <p>Several very similar discussions on SO did not help (like this one: <a href="https://stackoverflow.com/questions/41573313/docker-compose-with-django-could-not-translate-host-name-db-to-address-name-o">Docker-compose with django could not translate host name &quot;db&quot; to address: Name or service not known</a>).</p>
  
  <p>I have a <code>docker-compose.yml</code> with a network and two components:</p>
  
  <pre><code>version: '3'
  networks:
    bridge:
     driver: bridge
  services:
    db:
      image: postgres:10
      container_name: myapp-db
      volumes:
        - ./postgres_data:/var/lib/postgresql/data/
      ports:
        - "5432:5432"
      environment:
       POSTGRES_DB: actionability-master
       POSTGRES_PASSWORD: postgres
       POSTGRES_USER: postgres
      networks:
        - bridge
  
  
    web:
      restart: unless-stopped
      container_name: myapp-web
      build: .
      command: /start_gunicorn.sh
      ports:
        - "8080:8080"
      environment:
        PRODUCTION: 'true'
      networks:
        - bridge
  </code></pre>
  
  <p>In my <code>settings.py</code> I have DATABASES section:</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': INSTANCE_NAME,
          'USER': 'postgres',
          'PASSWORD': 'postgres',
          'HOST': 'db',
          'PORT': '5432'
      },
  
  }
  </code></pre>
  
  <p>When I run <code>$ docker-compose up  -d</code>,
  the first image (db) gets created and its container gets started. 
  Can see it's running and listening on port 5432 with <code>docker ps</code> and <code>lsof</code> . The same thing happens if I remove <code>web:</code> component from <code>docker-compose.yml</code> file</p>
  
  <p>Now, the second component (web) has Dockerfile that contains these two lines (among many others):</p>
  
  <pre><code>RUN python manage.py makemigrations myapp
  RUN python manage.py migrate
  </code></pre>
  
  <p>The "migrate" like dies with this error:</p>
  
  <pre><code>Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
      self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 119, in connect
      self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
      connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
      conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  psycopg2.OperationalError: could not connect to server: Connection refused
  Is the server running on host "db" (37.34.32.51) and accepting TCP/IP connections on port 5432?
  </code></pre>
  
  <p>I tired to several tweaks.
   * changed <code>version '3.7'</code>
   * added to db section </p>
  
  <pre><code>expose:
      - "5432"
  </code></pre>
  
  <ul>
  <li>added to web component:</li>
  </ul>
  
  <p><code>depends_on:
     - "db"</code></p>
  
  <ul>
  <li>added to web component:</li>
  </ul>
  
  <p><code>links:
     - "db"</code></p>
  
  <ul>
  <li>set PRODUCTION to false</li>
  </ul>
  
  <p><code>environment:
    PRODUCTION: 'false'</code></p>
  
  <ul>
  <li>changed HOST in <code>settings.py</code> to container name, to image name, to tags, to container id, to 'localhost', to '127.0.0.1', etc.. the error is the same, just mentioning the new HOST name instead of 'db' </li>
  <li>ran outside of conda env</li>
  <li>ran with <code>--build</code> switch ( <code>docker-compose up  -d --build</code> )</li>
  <li>did <code>docker system prune</code> and ran again</li>
  </ul>
  
  <p>All the same error.</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong>
  After a sugestion that it docker-compose does not work this way I tried to split it into two separate tasks. First I build myapp-db container and make sure it's running on the correct port :</p>
  
  <pre><code>$ docker container ps
  
  
  CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
  c110e8361cda        postgres:10         "docker-entrypoint.s…"   4 hours ago         Up 4 hours          0.0.0.0:5432-&gt;5432/tcp     myapp-db
  </code></pre>
  
  <p>Then I build myapp-web:</p>
  
  <pre><code>docker build -t myapp-web .
  </code></pre>
  
  <p>The same error still happens. So, why is it not finding the db container NOW?</p>