title: Docker Compose and Postgres : Name does not resolve
body: <p>In Docker, I am trying to configure Postgres, get it up and running in another container, and link it to my <code>users</code> service, set up with the following structure:</p>
  
  <pre><code>docker-compose-dev.yml
  services/
          users/
               manage.py
               Dockerfile-dev
               entrypoint.sh
               project/
                      __init__.py
                      config.py
                      db/
                        create.sql
                        Dockerfile
  </code></pre>
  
  <p><strong>docker-compose-dev.yml</strong></p>
  
  <pre><code>version: '3.7'
  
  services:
  
    users:
      build:
        context: ./services/users
        dockerfile: Dockerfile-dev
      volumes:
        - './services/users:/usr/src/app'
      ports:
        - 5001:5000
      environment:
        - FLASK_APP=project/__init__.py
        - FLASK_ENV=development
        - APP_SETTINGS=project.config.DevelopmentConfig
        - DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev 
        - DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test  
      depends_on:  
        - users-db
  
    users-db:  
      build:
        context: ./services/users/project/db
        dockerfile: Dockerfile
      ports:
        - 5435:5432
      environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
  </code></pre>
  
  <p><strong>Dockerfile-dev</strong></p>
  
  <pre><code># base image
  FROM python:3.7.2-alpine
  
  # install dependencies
  RUN apk update &amp;&amp; \
      apk add --virtual build-deps gcc python-dev musl-dev &amp;&amp; \
      apk add postgresql-dev &amp;&amp; \
      apk add netcat-openbsd
  
  # set working directory
  WORKDIR /usr/src/app
  
  # add and install requirements
  COPY ./requirements.txt /usr/src/app/requirements.txt
  RUN pip install -r requirements.txt
  
  # add entrypoint.sh
  COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
  RUN chmod +x /usr/src/app/entrypoint.sh
  
  # add app
  COPY . /usr/src/app
  
  # run server
  CMD ["/usr/src/app/entrypoint.sh"]
  </code></pre>
  
  <p>Here I try to extend the official Postgres image by adding a SQL file to the <code>"docker-entrypoint-initdb.d"</code> directory in the container.</p>
  
  <p><strong>Dockerfile</strong></p>
  
  <pre><code># base image
  FROM postgres:11.1-alpine
  
  # run create.sql on init
  ADD create.sql /docker-entrypoint-initdb.d
  </code></pre>
  
  <p><strong>create.sql</strong></p>
  
  <pre><code>CREATE DATABASE users_prod;
  CREATE DATABASE users_dev;
  CREATE DATABASE users_test;
  </code></pre>
  
  <p>Since the <em>"users"</em> service is dependent not only on the container being up and running but also the actual Postgres instance being up and healthy, I added an <code>entrypoint.sh</code> file to <em>"users"</em>:</p>
  
  <p><strong>entrypoint.sh</strong></p>
  
  <pre><code>#!/bin/sh
  
  echo "Waiting for postgres..."
  
  while ! nc -z users-db 5432; do
    sleep 0.1
  done
  
  echo "PostgreSQL started"
  
  python manage.py run -h 0.0.0.0
  </code></pre>
  
  <p><strong>manage.py</strong></p>
  
  <pre><code>from flask.cli import FlaskGroup
  from project import app, db
  
  cli = FlaskGroup(app)
  @cli.command('recreate_db')
  def recreate_db():
      db.drop_all()
      db.create_all()
      db.session.commit()
  if __name__ == '__main__':
      cli()
  </code></pre>
  
  <p><strong>config.py</strong></p>
  
  <pre><code>import os  
  
  class BaseConfig:
      """Base configuration"""
      TESTING = False
      SQLALCHEMY_TRACK_MODIFICATIONS = False  
  
  class DevelopmentConfig(BaseConfig):
      """Development configuration"""
      SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') 
  
  class TestingConfig(BaseConfig):
      """Testing configuration"""
      TESTING = True
      SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL')  
  
  class ProductionConfig(BaseConfig):
      """Production configuration"""
      SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')  
  </code></pre>
  
  <p>I run and successfully build it with:</p>
  
  <pre><code>docker-compose -f docker-compose-dev.yml up -d --build
  </code></pre>
  
  <p>but I'm having some network issue. If I run:</p>
  
  <pre><code>docker-compose -f docker-compose-dev.yml logs
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>Attaching to dev2_users_1
  users_1  | Waiting for postgres...
  users_1  | nc: getaddrinfo: Name does not resolve
  users_1  | nc: getaddrinfo: Name does not resolve
  users_1  | nc: getaddrinfo: Name does not resolve
  users_1  | nc: getaddrinfo: Name does not resolve
  users_1  | nc: getaddrinfo: Name does not resolve
  (...)
  
  CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
  6ca5718c9867        dev2_users          "/usr/src/app/entryp…"   2 minutes ago       Up 2 minutes        0.0.0.0:5001-&gt;5000/tcp   dev2_users_1
  </code></pre>
  
  <p>What am I missing in the code above?</p>