Running Django App Image in Docker

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 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

# set work directory

# 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

#expose port

#run check, test and migrate
RUN python check
RUN python test
RUN python migrate

#development runserver
CMD exec python runserver

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



Software Engineer, Indonesia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store