Dockerize your Django Application

In this article, we’ll cover how to dockerize your Django application.

Django is an open-source Python framework that is designed with speed, security, and scalability in mind. Django encourages clean design and rapid development.

Django on Docker Hub

Search Docker Hub (in your console, GUI, or the website itself) for django. More detailed steps can be found in the article Using Docker Hub).

You’ll find that there is an official repository for Django. It’s generally recommended to use the official repository when available.

Enter docker run django in your terminal. Docker will install Python 3 with Django 1.9+ unless you request a different tag. A full list of tags are available here.

Django Dockerfile

Writing your own Dockerfile is generally recommended, as it helps ensure you’re familiar with what is included in your image and container. It also simplifies creating future containers when developing your app.

Using the existing Django image, your Dockerfile would look like the following:

FROM django

ADD . /my-django-app

WORKDIR /my-django-app

RUN pip install -r requirements.txt

CMD [ "python", "./ runserver" ]

If you want to create a new Django project and its scaffolding, use this Dockerfile instead:

FROM django

WORKDIR /usr/src/app

CMD [ "django-admin", "startproject hello_world_django" ]

CMD [ "python", " runserver" ]

There are a couple of new commands in these Dockerfiles:

  • ADD allows you to add local directories (local to the current folder) to your Docker container
  • WORKDIR sets your working directory (so you do not have to run everything from the / or /root directories)

Dockerizing without a Dockerfile

Instead of creating and building a Dockerfile, you can leverage the official Django image that’s stored on Docker Hub to run your app.

docker run --name hello-world-django -v "$PWD":/usr/src/app -w /usr/src/app -p 8000:8000 -d django bash -c "pip install -r requirements.txt && python runserver"

The above command assumes the following:

  • Your Django app is named hello-world-django
  • Your app is stored in /usr/src/app

If you want to bootstrap a new application, use the following command (using the same directory and app name as above):

docker run -it --rm --user "$(id -u):$(id -g)" -v "$PWD":/usr/src/app -w /usr/src/app django startproject hello_world_django
  • requirements.txt allows you to use the standard pip install support with requirements.txt. If you need components that do not come with the Django image, you need to use this option.

At the end, you use Python to run the development server via and bind that to the wildcard IP on port 8000. Since we already bound the container’s port 8000 to the host’s, we want to make sure the development server runs on port 8000 (Python’s default).

Further information

If you do not need the requirements.txt file, leave it out of your command (remove the line RUN pip install -r requirements.txt from your Dockerfile, or do not pass it to bash in the interactive CLI approach).

If you need to pip install packages or components not found in a package, you need to install those dependencies (using either pip, requirements.txt, or both). In the example above, we use requirements.txt, but we could equally pass a pip install package_name if we know what package(s) to look for.

While Django runs its development web server on port 8000 by default, its recommended to set port 8000 for the web server, in the event a default changes or the image you use utilizes a different default port.

DO NOT use Django’s webserver in production. Use NGINX or Apache with the appropriate proxy (usually WCGI) running Python and Django.

Next: Dockerize your Flask Application
Get started with dockerizing your Flask application.