skip to Main Content

I’ve been trying to dockerise my django project with postgresql but have been running into this same problem time and time again.

web_1  | CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

I’ve my environment variables in a .env set to:

.env:

DEBUG = True
ALLOWED_HOSTS=localhost 127.0.0.1 0.0.0.0:8000

and use the python-decouple module to configure them in settings.py

from decouple import config
DEBUG = config('DEBUG', default=False)
ALLOWED_HOSTS = config('ALLOWED_HOSTS').split(sep=' ')

Running the project:

python manage.py runserver 

works perfectly fine with no errors. However, when it comes to running:

docker-compose up

I get the following output

Starting postgres_db ... done
Starting project_web_1 ... done
Attaching to postgres_db, project_web_1
postgres_db |
postgres_db | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_db |
postgres_db | 2021-07-30 17:58:52.695 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_db | 2021-07-30 17:58:52.696 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_db | 2021-07-30 17:58:52.696 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_db | 2021-07-30 17:58:52.747 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_db | 2021-07-30 17:58:52.887 UTC [27] LOG:  database system was shut down at 2021-07-30 17:58:34 UTC
postgres_db | 2021-07-30 17:58:52.950 UTC [1] LOG:  database system is ready to accept connections
web_1  | CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
project_web_1 exited with code 1

I’ve tried setting ALLOWED_HOSTS=['*'] and have included other hosts too, but I cannot find anything that works.

Dockerfile

FROM python:3.9

COPY . /usr/src/app
WORKDIR /usr/src/app
RUN pip install -r requirements.txt
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

docker-composse.yml

version: "3.8"
services:
  web:
    build: .
    volumes: 
      - .:/django
    ports:
      - "8000:8000"
    depends_on: 
      - db
  
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    container_name: postgres_db

2

Answers


  1. Chosen as BEST ANSWER

    So I found no evident solution to the problem, thus, seeing that nothing was logically incorrect, I decided to start the whole project again (beginning from a docker container) and copy over the django files that I needed. It now works like a charm.

    I appreciate the people who took the time to answer. Follow https://docs.docker.com/samples/django/ as @markwalker_ suggested above.


  2. for some weird reason it worked for me only when i started django with gunicorn and nginx idk why, i even cloned my repository somewhere else but with no effect, also i started django shell and settings was debug is False although it was hardcoded True…
    this thing killed my day (:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search