Dockerize a Django Application

Đinh Phong
Kudosvn
Published in
3 min readJun 5, 2019

Hey there! In this article, I am going to show you how to dockerize a application built in Django

Table of Contents

  1. What is Docker ?
  2. Installation
  3. Dockerize Django Application

1. What is Docker ?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

2. Installation

Docker Community Edititon

Docker Community Edition (CE) is ideal for developers and small teams looking to get started with Docker and experimenting with container-based apps. Docker CE has three types of update channels, stable, test, and nightly:

  • Stable gives you latest releases for general availability.
  • Test gives pre-releases that are ready for testing before general availability.
  • Nightly gives you latest builds of work in progress for the next major release.

For more information about Docker CE, see Docker Community Edition.

Supported platforms:

3. Dockerize Django Application

Setup a Dockerfile

  1. Create a new file called Dockerfile in your project directory.
  2. The Dockerfile defines an application’s image content via one or more build commands that configure that image. Once built, you can run the image in a container. For more information on Dockerfile, see the Docker user guide and the Dockerfile reference.
  3. Add the following content to the Dockerfile.
# The first instruction is what image we want to base our container on
# We Use an official Python on Alpine runtime as a parent image
FROM python:3.6-alpine
# Expose port 8000
EXPOSE 8000

# The enviroment variable ensures that the python output is set straight
# to the terminal with out buffering it first
ENV PYTHONUNBUFFERED 1

# Install dependencies: Postgres & Pillow
RUN apk update \
&& apk add --no-cache build-base postgresql-dev bash \
# Pillow dependencies
jpeg-dev \
zlib-dev \
freetype-dev \
lcms2-dev \
openjpeg-dev \
tiff-dev \
tk-dev \
tcl-dev \
harfbuzz-dev \
fribidi-dev \
&& rm -vfr \
/tmp/* \
/var/tmp/* \
/var/lib/apt/lists/*

# create root directory for our project in the container
RUN mkdir /sample-project

# Set the working directory to /sample-project
WORKDIR /sample-project

# Copy the current directory contents into the container at /sample-project
ADD ./requirements.txt /sample-project/

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

ADD . /sample-project/

4. This Dockerfile starts with a Python 3 parent image. The parent image is modified by adding a new code directory. The parent image is further modified by installing the Python requirements defined in the requirements.txt file.

5. Save and close the Dockerfile.

Setup Docker Compose file

version: '3'

services:
web:
build: .
network_mode: 'host'
env_file:
- .env
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver"
container_name: sample-project-container
volumes:
- .:/sample-project
ports:
- "8000:8000"

Run application

docker-compose up

--

--