Running Django applications in a Docker container

by Alex
Running Django applications in a Docker container

If you’ve been programming for a while, you’ve probably heard of Docker or the term containers. In this guide, we’ll understand what it means using Docker as an example, and we’ll also look at how to containerize a simple Django application. At the end of the topic you will know the following:

  • Virtualization
  • Containerization (with Docker)
  • Docker
  • Creating a Dockerfile
  • Docker Compose
  • Configuring a Django application in a Docker environment with Dockerfile and docker-compose

Terms

To handle this tutorial you need to have the following:

  • Git/GitHub
  • PyCharm (or any other code editor)
  • Experience with Django

A ready-made repository with a Django application, as always on GitLab: https://gitlab.com/PythonRu/django-docker.

What is virtualization

Typically, when you deploy a web application to a hosting service (like DigitalOcean or Linode), you set up a virtual machine or virtual computer where all the code will be migrated using git, FTP, or other means. This is called virtualization. Over time, developers began to see the disadvantages of this process – at least the cost of adapting to changes in the operating system. They wanted to merge the development and production environments, and hence the idea of containerization emerged.

What are containers, and what’s so special about them?

A container, in simple terms, is a place for the development environment, that is, your application and the dependencies that are required for it to work. Containers allow the developer to package the application with all its dependencies and transfer it between different environments without any changes. Because containerization is a much more portable, scalable, and efficient solution, platforms like Docker are becoming a popular choice for developers.

Introduction to Docker

Docker is a toolkit with which you can create, manage, and run applications in containers. It allows you to easily package and run applications as portable, independent and lightweight containers that can run anywhere.

Installing Docker

To install Docker on your computer, use the instructions from the official website. Each operating system has its own version of the application.

Setting up the application

For this tutorial, we’ll use the repository of a survey application written in Django. As we go through this tutorial we will set up the Dockerfile, which will designate instructions for the container inside which the application will run. After that we will also configure the docker-compose.yml file to simplify the whole process. On a PC with git installed, go to the selected folder and clone the following repository from GitLab: git clone https://gitlab.com/PythonRu/django-docker.git After that, go to the root of that folder and open it in an editor with this command: cd django-docker && code . Запуск Django-приложения в Docker контейнере In that folder, create a Dockerfile (case sensitive) with no format. Inside it will be the container settings. The instructions from it will be executed by the computer every time you run the docker build command. The next step is to create a requirements.txt file which will list all dependencies. This will later be used for the Dockerfile, which also needs to list all the required dependencies. In the requirements.txt file, add Django version 3.1.2 in this format: Django==3.1.2

What is a Dockerfile

The idea of writing a Dockerfile might sound complicated, but don’t forget that it’s just a set of instructions (a set of steps) for creating your own images. A Dockerfile will contain the following:

  1. A basic image on which you need to build your own. It acts as a kind of foundation for your application. It can be an operating system, a programming language (Python in our case) or a framework.
  2. Packages and additional tools for the image.
  3. The scripts and files you want to copy into the image. This is usually the source code of the application.

When reading or writing such a file, it is convenient to keep the following in mind:

  • The lines with instructions usually start with a keyword, for example: RUN, FROM, COPY, WORKDIR, etc.
  • Comments begin with a # character. When executing instructions from a file, such comments are usually ignored.

Creating a Dockerfile

The application will run based on the official Python image. Let’s write the following instructions: # Instructs Docker to use the official python 3 image with dockerhub as the base image
FROM python:3
# Sets an environment variable that ensures that output from python is sent directly to the terminal without pre-buffering
ENV PYTHONUNBUFFERED 1
# Sets the container working directory to "app"
WORKDIR /app
# copies all the files from our local project to the container
ADD ./app
# Starts a pip install for all the libraries listed in requirements.txt
RUN pip install -r requirements.txt

Docker Compose file

Docker Compose is a great tool to help you define and run applications that require multiple services. Typically, Docker Compose uses the docker-compose.yml file to configure the services that the application will use. These services are started with the docker-compose up command. This creates and runs all the services from the file. Most web applications need a web server (like nginx) and a database (like PostgreSQL). In this application, we will use SQLite, so no external database is needed. To use the features of Docker Compose, you need to create a file docker-compose.yml in the same folder where Dockerfile is located and add the following code there: version: '3.8'
services:
web:
build: .
command: python manage.py runserver localhost:8000
ports:
– 8000:8000 Next let’s analyze the contents of the file line by line: version: '3.8' This line tells Docker which version of docker-compose should be used to start the file. At the time of writing this tutorial, the latest version is 3.8, but usually the syntax doesn’t change much as later versions are released. After configuring docker-compose, open a terminal and run the command docker-compose up -d to start the application. Next, open the localhost:8000 link in your browser to see the application in action: Запуск Django-приложения в Docker контейнере Use the docker-compose down command to close the container.

Retrieved from

Project repository: https://gitlab.com/PythonRu/django-docker. In this tutorial you learned about virtualization, containerization and other terms from the Docker world. You also now know what a Dockerfile is and how to create one to run a containerized Django application. Finally, you figured out how to configure docker-compose with the docker-compose.yml file for the services that the application itself depends on. There is no single correct way to use Docker in a Django application, but it is considered good practice to follow the official instructions to keep your application as secure as possible.

Related Posts

LEAVE A COMMENT