Cannot access Django project homepage (Docker)

I'm building an app with Django, but although my app runs fine (at least, it's what CLI tells me) I can´t access it via browser.

The docker-compose build and up works just fine, I get this output:

 matt $ docker-compose run app sh -c "python manage.py runserver"             
[+] Creating 2/2
 ✔ Network nexus_default  Created                                                                         0.0s 
 ✔ Container nexus-db-1   Created                                                                         0.1s 
[+] Running 1/1
 ✔ Container nexus-db-1  Started                                                                          0.2s 
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 08, 2024 - 23:21:49
Django version 5.0.3, using settings 'app.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

But I can't access it through my browser (Screenshot attached) browser-error

My configs are below

Dockerfile:

FROM python:3.12-alpine3.19
LABEL maintainer="Matheus Tenório"

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /tmp/requirements.txt
COPY ./requirements.dev.txt /tmp/requirements.dev.txt
COPY ./app /app
WORKDIR /app
EXPOSE 8000

ARG DEV=false
RUN python -m venv /py && \
    /py/bin/pip install --upgrade pip && \
    apk add --update --no-cache postgresql-client && \
    apk add --update --no-cache --virtual .tmp-build-deps \
        build-base postgresql-dev musl-dev && \
    /py/bin/pip install -r /tmp/requirements.txt && \
    if [ $DEV = "true" ]; \
      then /py/bin/pip install -r /tmp/requirements.dev.txt ; \
    fi && \
    rm -rf /tmp && \
    apk del .tmp-build-deps && \
    adduser \
        --disabled-password \
        --no-create-home \
    djangouser && \
    chown djangouser:djangouser -R /app/

ENV PATH="/py/bin:$PATH"

USER djangouser

docker-compose.yml:

version: "3.12"

services:
  app:
    build:
      context: .
      args:
        - DEV=true
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
        sh -c "python manage.py wait_for_db &&
               python manage.py migrate &&
               python manage.py runserver 0.0.0.0:8000"
    environment:
      - DB_HOST=db
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
    depends_on:
      - db

  db:
    image: postgres:16-alpine3.19
    volumes:
      - dev-db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=${DB_NAME}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}


volumes:
  dev-db-data:

.env

DB_NAME="my_db"
DB_USER="my_user"
DB_PASSWORD="my_password"

settings.py: (Here's just the configs I changed from default)

import os

ALLOWED_HOSTS = [
    "0.0.0.0",
    "127.0.0.1",
    "localhost",
]

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': os.environ.get('DB_HOST'),
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
    }
}

Also, this is my docker ps docker-ps

TEST

Okay, let's get a working Django app up and running. Then you can add your specific functionality on top of that.

I created a simple app and the project has the following layout. I applied the same changes as you to settings.py.

├── app
│   ├── app
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── manage.py
│   └── project
│       ├── asgi.py
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
├── docker-compose.yml
├── Dockerfile
└── requirements.txt

To keep things simple the settings are just specifying a SQLite database. You'll want to change this to PostgreSQL but the database is not the source of your problems right now.

🗎 Dockerfile

FROM python:3.12-alpine3.19
LABEL maintainer="Matheus Tenório"

ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY requirements.txt requirements.txt

RUN apk add --update --no-cache postgresql-client && \
    apk add --update --no-cache --virtual .tmp-build-deps \
        build-base postgresql-dev musl-dev && \
    pip install -r requirements.txt && \
    rm -f requirements.txt && \
    apk del .tmp-build-deps && \
    adduser \
        --disabled-password \
        --no-create-home \
    djangouser && \
    chown djangouser:djangouser -R /app/

COPY app /app

USER djangouser

RUN python manage.py migrate

CMD python manage.py runserver 0.0.0.0:8000

The Dockefile is applying the migrations at build time and then running the server.

🗎 docker-compose.yml

version: "3.12"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"

To build and run:

docker-compose build && docker-compose up

Once you have this running I suggest as next steps:

  1. Add the db service to your docker-compose.yml.
  2. Change settings.py to use the PostgreSQL database. Set up credentials via environment variables in docker-compose.yml.
  3. Add in your specific application code.

enter image description here

Back to Top