"Will code for travel"

Search technical info
How to docker-ize a completed django project?
Written: 2019-11-05 00:04:28 Last update: 2019-11-05 00:09:14

How to Docker-ize a Django project after Django with python development is completed? before reading this article, if you don't know what is 'docker' then please read this wiki and why use docker?

Firstly we need to install Docker, I use Docker CE (Community Edition) for my Ubuntu 18.04 system, if you use Ubuntu or other linux distribution then the next step is to add our current linux user into docker group, we need to do this to avoid keep using 'sudo' to invoke docker command.

$ sudo usermod -aG docker ${USER}

# logout and login then verify if our current user is in docker group 
$ id -nG

To create a docker container, we need to create a file named 'Dockerfile' to make container for our Django project, a Dockerfile is enough to create a single image for Django project using sqlite3 (default built-in), if we have other database such as MySQL or require other system such as nginx then we need to install docker-compose and create docker-compose.yml to define additional apps, this is because Dockerfile only able to manage single container and docker-compose.yml can manage multiple containers.

If you want to run your Django as soon as your container is running then you need to set it up manually, below is my custom Dockerfile, requirements.txt and docker-entrypoint.sh, all these 3 files are stored within the same project folder as my Django project, your need will be different and need to customize them, please read the comments inside files.

File: Dockerfile

# download free image from docker hub
# use python (slim version, based on Debian OS), change the version if necessary
FROM python:3.6-slim

ENV PYTHONUNBUFFERED 1

# inside image, create a folder
RUN mkdir /my_django

# set folder as working directory
WORKDIR /my_django
 
# copy all files in current local directory to image
# including requirements.txt, docker-entrypoint.sh
COPY . /my_django/

# define all required applications in separate file 'requirements.txt'
# then run 'pip' command to install all those applications into image 
RUN pip install -r requirements.txt

# copy and create symbolic link to the script (to run the django web service)
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s /usr/local/bin/docker-entrypoint.sh / # backwards compat

# command to run when container starts: start web service 
ENTRYPOINT ["docker-entrypoint.sh"]

File: requirements.txt

# Django version
Django>=2.0,<3.0

# other required python packages
# this is only for my personal case, yours should be different ^.^

# DICOM parser
pydicom>=1.3.0

# pydicom.pixel_array needs numpy
numpy>=1.17

# alternative to standalone numpy, we can use opencv-python (has numpy built-in) 
#opencv-python>=4.0

# Image processing (https://github.com/python-pillow/Pillow, https://python-pillow.org/, https://pypi.org/project/Pillow/)
# to convert pydicom.pixel_array to 'Image' object
Pillow>=6.2

File: docker-entrypoint.sh

#!/bin/sh

# force makemigrations :: if use sqlite database then will create sqlite3 file if not exist
python manage.py makemigrations --noinput

# Apply database migrations
python manage.py migrate

# Start server in port 8000, can use whatever port
python manage.py runserver 0.0.0.0:8000

# server will run in background, pressing CTRL-C will not exit
# to stop use 'docker stop ..' (in terminal)

exec "$@"

After these 3 files are created and defined then we can run our docker container, below is some common ways to build, run, stop and remove container.

# build a docker image, this will always create a new image.
# if this image's name is already exist then the previous image will be 'moved' as history with a tag for reference 
# create whatever name for the image, eg: my_django_docker 
$ docker build -t my_django_docker . 

# see docker images 
$ docker images

# run docker container and let docker set random name 
# give parameter to expose the port number, in my case 8000 (host port number):8000 (guest port number)
$ docker run -p 8000:8000 my_django_docker

# run docker container and set custom name
$ docker run -p 8000:8000 --name my_container_name my_django_docker

# see which containers are running AND also see the NAME (needed to stop it) 
$ docker ps 

# see all containers list (running or not), will show CONTAINER_ID (to be use for removal)
$ docker ps -a 

# stop a running container, we need to know the NAME of the running container, use 'docker ps' to see the name
$ docker stop interesting_yalow 

# remove a container using CONTAINER_ID retrieved from 'docker ps -a'
$ docker rm [CONTAINER_ID]

During docker testing, we may need to make many build, so there will be so many created images, each image may consume large space, in my case using 'FROM python:3.6-slim' the image size with my little Django project is around 347 MB !

It is not too difficult to create docker container for django, so just focus and enjoy on the development and docker-ize it later when development is finished.

Search more info