Running Django App Image in Docker

Adi Ramadhan
2 min readFeb 16, 2023

Simple Django Apps Image in Docker.
Official docs of django and docker.
Prerequisite: python and docker installed.

Step 1: Preparation, Create Django Project, Inital Migration
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django==3.2
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Initial Migration: python manage.py migrate

Step 2: Create requirements.txt
create requirements.txt: pip freeze> requirements.

Step 3: Create Dockerfile in myproject

# syntax=docker/dockerfile:1
FROM python:3.8

# set environment variables
ENV APP_HOME=/app

# set work directory
WORKDIR $APP_HOME

# update pip, install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt $APP_HOME
RUN pip install -r requirements.txt

# copy project directory to app dir
COPY . $APP_HOME

#expose port
EXPOSE 8000

#run check, test and migrate
RUN python manage.py check
RUN python manage.py test
RUN python manage.py migrate

#development runserver
CMD exec python manage.py runserver 0.0.0.0:8000

Step 4: Docker Build

with command:

docker build -t myproject_image:latest .

Step 5: Run Image Docker

with command

docker run -p 8000:8000 myproject_image:latest
Django Apps Image Running in Docker

Final project file structure:

project file structure

--

--