title: Docker Django could not connect to server: Connection refused
body: <p>I'm new to Docker, and I'm trying to put my Django rest API in a container with Nginx, Gunicorn and Postgres, using docker-compose and docker-machine. Following this tutorial: <a href="https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/" rel="noreferrer">https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/</a></p>
  
  <p>Most of my code is the same as the tutorial's (<a href="https://github.com/realpython/dockerizing-django" rel="noreferrer">https://github.com/realpython/dockerizing-django</a>). with some minor name changes.</p>
  
  <p>this my <code>docker-compose.yml</code> (I changed the gunicorn command to runserver for debugging purposes)</p>
  
  <pre><code>web:
    restart: always
    build: ./web
    expose:
      - "8000"
    links:
      - postgres:postgres
      - redis:redis
    volumes:
      - /usr/src/app
      - /usr/src/app/static
    env_file: .env
    environment:
      DEBUG: 'true'
    command: /usr/local/bin/python manage.py runserver
  
  nginx:
    restart: always
    build: ./nginx/
    ports:
      - "80:80"
    volumes:
      - /www/static
    volumes_from:
      - web
    links:
      - web:web
  
  postgres:
    restart: always
    image: postgres:latest
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data/
  
  redis:
    restart: always
    image: redis:latest
    ports:
      - "6379:6379"
    volumes:
      - redisdata:/data
  </code></pre>
  
  <p>And this is in my settings.py of Django:</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': 'postgres',
          'USER': 'postgres',
          'PASSWORD': 'postgres',
          'HOST': 'postgres',
          'PORT': '5432',
      }
  }
  </code></pre>
  
  <p>Nginx and postgres (and redis) are up and running, however my django server wont start, by this error:</p>
  
  <pre><code>web_1       | django.db.utils.OperationalError: could not connect to server: Connection refused
  web_1       |   Is the server running on host "localhost" (::1) and accepting
  web_1       |   TCP/IP connections on port 5432?
  web_1       | could not connect to server: Connection refused
  web_1       |   Is the server running on host "localhost" (127.0.0.1) and accepting
  web_1       |   TCP/IP connections on port 5432?
  </code></pre>
  
  <p>I've googled a lot and I've verified that postgres is running, on port 5432, I can connect to it using psql command . </p>
  
  <p>I am lost. What is my mistake?</p>
  
  <p>EDIT: It appears that it is not using my settings.py file or something, since it's asking if the server is running on localhost, while settings should be looking for postgres.</p>