how to run django project using docker

  django, docker, docker-compose, python

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

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?

Source: Docker Questions

LEAVE A COMMENT