Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To establish a setup for running Django in Docker using Docker Compose, you can follow these steps:

  1. Create a new Django project.
$ django-admin startproject myproject
  1. Create a Dockerfile in the project's root directory.
FROM python:3.7

ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
  1. Create a docker-compose.yml file in the project's root directory.
version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: myprojectuser
      POSTGRES_PASSWORD: myprojectpassword
      POSTGRES_DB: myprojectdb
    volumes:
      - postgres_data:/var/lib/postgresql/data/

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

volumes:
  postgres_data:
  1. Create a requirements.txt file in the project's root directory.
Django==2.2
psycopg2==2.8.5
  1. Start the containers.
$ docker-compose up

Once the containers start, you can access the Django project at http://localhost:8000.