how to run django project using docker

Question:

I am trying to create a docker container to run my django project.
Following different other questions, I managed to obtain a successful build with docker.

I have created a Dockerfile, docker-compose and I have created a local postgres database.
and if I run the project from the entrypoint (chain of makemigration, migrate and runserver) the service can be reached.

the problem is when I dockerize the project.

my docker-compose.yml file is

version: "3.9"

services:
  db:
    restart: always
    image: postgres:12.0-alpine
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=signalcrossingrestapi
      - POSTGRES_USER=$POSTGRES_LOCAL_USER
      - POSTGRES_PASSWORD=$POSTGRES_LOCAL_PASSWORD
    env_file:
      - ./.env
  web:
    restart: always
    build:
      dockerfile: Dockerfile
      context: .
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    env_file: .env
    command: ./run.sh
volumes:
  postgres_data:
    driver: local

in here the run.sh script is just

#!/bin/bash
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsu
python3 manage.py runserver

the command createsu is located inside project/app/managment/commands/

from pathlib import Path

from decouple import AutoConfig
from django.core.management.base import BaseCommand

from accounts.models import CustomUser

BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent
print(BASE_DIR)
config = AutoConfig(search_path=BASE_DIR)
admin = config("ADMIN")
admin_email = config("ADMIN_EMAIL")
admin_password = config("ADMIN_PASSWORD")


class Command(BaseCommand):
    def handle(self, *args, **options):
        if not CustomUser.objects.filter(username=admin).exists():
            CustomUser.objects.create_superuser(admin, admin_email, admin_password)
            self.stdout.write(self.style.SUCCESS("Successfully created superuser"))

and the dockerfile is

FROM python:3.8-slim
# set up the psycopg2
RUN apt-get update && apt-get install -y libpq-dev gcc postgresql-client


ENV PYTHONUNBUFFERED=1
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"


WORKDIR /code
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /code/
RUN pip install psycopg2==2.8.3

RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt


RUN apt-get autoremove -y gcc

EXPOSE 8000

when I execute

docker-compose up –build

it is successful but when I open localhost:8000 I cannot reach the page.

what is the solution to this issue?

Asked By: bruvio

||

Answers:

manage.py runserver starts the server acessible only from localhost/127.0.0.1, you have to make it accessible from other locations too. So edit your run.sh the following way

python3 manage.py runserver 0.0.0.0:8000
Answered By: yedpodtrzitko

You have to serve your page from Django- but not only on domain like ‘localhost’ but also on specified port number like ‘localhost:80’ (for example).

Answered By: forDeFan