title: Docker, postgres, sqlalchemy - could not connect to server: Cannot assign requested address
body: <p>Hi I am trying setup my project on docker. The structure looks like this</p>
  
  <pre><code>Project
  
  -- docker-compose.yml
  
  -- docker
     -- app
       -- Dockerfile
       -- my-project-0.1.tar.gz
  
     -- db
       -- Dockerfile
       -- db_dump.dump
  </code></pre>
  
  <p>The docker-compose file looks like this: </p>
  
  <pre><code>version: '3'
  services:
    db:
      build: ./docker/db
      ports:
        - "5432:5432"
  
    app:
      build: ./docker/app
      depends_on:
       - db
      command: getdatacommand
      restart: always  
  </code></pre>
  
  <p>app/Dockerfile looks like this</p>
  
  <pre><code>FROM python:3
  
  COPY myproject-0.1.tar.gz /tmp
  WORKDIR /tmp
  
  RUN pip install myproject-0.1.tar.gz
  
  # SETUP DB ENVIROMENT VARIABLES
  ENV DB_DRIVER='postgres'
  ENV DB_HOST='db'
  ENV DB_PORT='5432'
  ENV DB_USERNAME='myuser'
  ENV DB_PASSWORD='secretpass'
  ENV DB_DBNAME='db-dev'
  ...
  
  EXPOSE 5000
  </code></pre>
  
  <p>I refered to: <a href="https://docs.docker.com/engine/examples/postgresql_service/" rel="nofollow noreferrer">https://docs.docker.com/engine/examples/postgresql_service/</a> for creating my postgres Dockerfile</p>
  
  <p>my db dockerfile: </p>
  
  <pre><code>FROM ubuntu
  
  # SETUP DB ENVIROMENT VARIABLES
  ENV BB_DB_PORT='5432'
  ENV BB_DB_USERNAME='myuser'
  ENV BB_DB_PASSWORD='secretpass'
  ENV BB_DB_DBNAME='db-dev'
  
  COPY bb-dev.dump /tmp
  WORKDIR /tmp
  
  RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
  RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" &gt; /etc/apt/sources.list.d/pgdg.list
  RUN apt-get update &amp;&amp; apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
  
  USER postgres
  
  # Start postgres create db and restore dump
  RUN /etc/init.d/postgresql start &amp;&amp;\
      psql --command psql --command "CREATE USER ${BB_DB_USERNAME} WITH SUPERUSER PASSWORD '${BB_DB_PASSWORD}';" &amp;&amp;\
      createdb -O ${BB_DB_USERNAME} ${BB_DB_DBNAME} &amp;&amp;\
      psql ${BB_DB_DBNAME} &lt; bb-dev.dump
  
  # Adjust PostgreSQL configuration so that remote connections to the
  # database are possible.
  RUN echo "host all  all    0.0.0.0/0  md5" &gt;&gt; /etc/postgresql/9.3/main/pg_hba.conf
  
  # And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
  RUN echo "listen_addresses='*'" &gt;&gt; /etc/postgresql/9.3/main/postgresql.conf
  
  # Expose the PostgreSQL port
  EXPOSE ${BB_DB_PORT}
  
  # Set the default command to run when starting the container
  CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
  </code></pre>
  
  <p>However, when I run <code>docker-compose up</code> I get a <code>connection refused error</code>.
  Event though I can access the postgres container from the host machine like this: </p>
  
  <p><code>psql -h localhost -p 5432 -d db-dev -U myuser --password</code></p>
  
  <p>Am I missing something obvious?</p>
  
  <p><strong>Update</strong> </p>
  
  <p>To make sure the database is ready before app starts reading from it, I modified my <code>docker-compose</code> file to look like this: </p>
  
  <pre><code>version: '2.1'
  services:
    db:
      build: ./docker/db
      ports:
        - "5432:5432"
  
      healthcheck:
          test: ["CMD", "pg_isready -h localhost -p 5432 -U myuser"]
          interval: 30s
          timeout: 10s
          retries: 5
    app:
      build: ./docker/app
      depends_on:
          db:
              condition: service_healthy
      command: getdatacommand
      restart: always  
  </code></pre>
  
  <p>The app container in this case fails to start and I get a message saying <code>container is unhealthy</code>. However the pg_isready works fine if I comment out the app container and run <code>docker-compose up</code> with just the <code>db</code> container</p>