1

My django app is failing to connect to the psql container with the standard connection refused error. I used django-cookiecutter which supplies the psql username and password automatically via environment variables and then this I gather is passed back into django with via a .env file that hosts a DATABASE_URL string.

Error

django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?

When I set a breakpoint in django settings I can see that the DATABASE_URL seems to be converted appropriately into the standard db dict:

{'NAME': 'hustlestat', 'USER': 'HjhPLEwuVjUIIKEHebPqNG<redacted>', 'PASSWORD': 'I43443fR42wRkUaaQ8mkd<redacted>', 'HOST': 'postgres', 'PORT': 5432, 'ENGINE': 'django.db.backends.postgresql'}

When I exec into the psql container with psql hustlestat -U HjhPLEwuVjUIIKEHebPqN<redcated> I can connect to the db using that username. I'm not 100% on the password as it isn't asking me for one when I try to connect.

Here is the docker compose which is generated automatically by cookie cutter:

version: '3'

volumes:
  local_postgres_data: {}
  local_postgres_data_backups: {}

services:
  django: &django
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: hustlestat_local_django
    container_name: django
    depends_on:
      - postgres
      - mailhog
    volumes:
      - .:/app:z
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres
    ports:
      - "8000:8000"
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: hustlestat_production_postgres
    container_name: postgres
    volumes:
      - local_postgres_data:/var/lib/postgresql/data:Z
      - local_postgres_data_backups:/backups:z
    env_file:
      - ./.envs/.local/.postgres

  docs:
    image: hustlestat_local_docs
    container_name: docs
    build:
      context: .
      dockerfile: ./compose/local/docs/Dockerfile
    env_file:
      - ./.envs/.local/.django
    volumes:
      - ./docs:/docs:z
      - ./config:/app/config:z
      - ./hustlestat:/app/hustlestat:z
    ports:
      - "7000:7000"
    command: /start-docs

  mailhog:
    image: mailhog/mailhog:v1.0.0
    container_name: mailhog
    ports:
      - "8025:8025"

  redis:
    image: redis:5.0
    container_name: redis

  celeryworker:
    <<: *django
    image: hustlestat_local_celeryworker
    container_name: celeryworker
    depends_on:
      - redis
      - postgres
      - mailhog
    ports: []
    command: /start-celeryworker

  celerybeat:
    <<: *django
    image: hustlestat_local_celerybeat
    container_name: celerybeat
    depends_on:
      - redis
      - postgres
      - mailhog
    ports: []
    command: /start-celerybeat

  flower:
    <<: *django
    image: hustlestat_local_flower
    container_name: flower
    ports:
      - "5555:5555"
    command: /start-flower

  node:
    build:
      context: .
      dockerfile: ./compose/local/node/Dockerfile
    image: hustlestat_local_node
    container_name: node
    depends_on:
      - django
    volumes:
      - .:/app:z
      # http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html
      - /app/node_modules
    command: npm run dev
    ports:
      - "3000:3000"
      # Expose browsersync UI: https://www.browsersync.io/docs/options/#option-ui
      - "3001:3001"

The only oddity I have noticed is that despite django being named in the docker compose, when I view the running containers it has a random name such as:

hustlestat_django_run_37888ff2c9ca

Not sure if that is relevant.

Thanks for any help!

Cam Rail
  • 177
  • 1
  • 2
  • 11

1 Answers1

2

Okay have figured this out. I set a DATABASE_URL environment variable because I was originally getting an error saying it was unset. After googling I came across a cookie cutter doc that said to set it but didn't read it well enough to realise that the instruction was intended for non-docker setups. Mine is docker.

The reason I was getting that error is because I was exec'ing into the container and running management commands like this: docker exec -it django bash then python manage.py migrate

The way this project is setup and environment variables are setup, you can't do that, you have to use this method from outside the exec:

docker-compose -f local.yml run --rm django python manage.py migrate

I thought the two methods were interchangeable but they are not. Everything works now.

Cam Rail
  • 177
  • 1
  • 2
  • 11