Dockerizing the Django Application

Create a Dockerfile to configure Docker with the Django application.

In this chapter, we will configure Docker with the Django application. This will help us better understand better how Docker works beneath the surface.

Adding a Docker image

A characteristic of projects that use Docker is the presence of files called Dockerfiles in the project. A Dockerfile is a text document that contains all the commands necessary to assemble a Docker image. A Docker image is a read-only template with instructions to create a Docker container.

Creating an image with a Dockerfile is the most popular way to go because we only need to enter the instructions we will require to set up an environment, install the package, make migrations, and a lot more. This is what makes Docker very portable. For example, in the case of our Django application, we will write the Dockerfile based on an existing image for Python 3.10 based on the popular Alpine Linux project. This image has been chosen because of its small size, equal to 5 MB. Inside the Dockerfile, we will also add commands to install Python and Postgres dependencies, and we will further add commands to install packages. Let’s get started with the steps.

Step 1

Start by creating a new file at the root of the Django project called Dockerfile and adding the first line:

Get hands-on with 1200+ tech skills courses.