How to Run Your Django Project With Docker
This article was written as an individual review assignment of PPL CSUI 2021
As developers, I believe we can all agree that much work needs to be done when it comes to developing apps. These days, it takes much more than just writing code. We may need multiple languages, libraries, and frameworks to work together so that our apps can do what they are made to do.
If we do need various sorts of dependencies for our app, then what’s the problem?
Well, ever heard of the “But, it works on my machine” problem?
In short, it is a problem where your code works perfectly on your own machine but doesn’t on others due to the different environments that different host machines have.
Here is where Docker comes in. Docker is a popular technology that many developers currently use to solve the “But, it works on my machine” problem. This article aims to explain what Docker is and how to run your Django application using Docker.
About Docker
Docker is an open platform that can be used for developing, shipping, and running our applications. Docker allows developers to work in standardized environments using what is so-called ‘containers’.
Docker containers
A Docker container is a process that can run on your machine and is isolated from any other processes. It is a unit that can be used for distributing and testing our developed applications.
When running a container, an isolated filesystem is used by the container, which means that it contains isolated dependencies, configuration, scripts, binaries, etcetera. Thus, for a case when you want your fellow developers to work with an application that you’ve made, you can share it using Docker so that they can easily obtain the needed environment to run and work on the application.
Docker images
A Docker image is a read-only template with instructions for creating the Docker container. There are two ways for using Docker images that is by creating your own images or by using other people’s images.
To create your own image, you have to create a Dockerfile
that will define the steps needed to create the image and run it. An example of a Dockerfile
will be shown below.
How to Run a Django Project Using Docker
Install Docker
First, make sure that you have Docker installed on your machine. You can do so by following an excellent guide by the official Docker itself here:
https://docs.docker.com/get-docker/
Prepare the requirements.txt
Go to your Django project directory and make sure that all of the requirements for your Django project is prepared in the requirements.txt
file.
Create Dockerfile
Create a file called Dockerfile
(without any file extension) and put in the steps needed to create the Docker image like so:
Let me explain to you what all the instructions from the above means:
1. FROM python:3
A Dockerfile
has to begin with a FROM
instruction because it specifies the base image. The instruction from the above means that you want the base image to be python:3
.
2. ENV PYTHONUNBUFFERED 1
ENV
is an instruction that sets an environment variable, and so here you’re setting a variable called PYTHONUNBUFFERED
with a value of 1.
3. RUN mkdir /code
RUN
is an instruction that will run a Linux command. We can use this to call commands like mkdir
, which from the above would make a directory called code
.
4. WORKDIR /code
WORKDIR
is an instruction that will set a directory as a working directory for the instructions that follow. From the above, we set the directory code
to be a working directory.
5. ADD requirements.txt /code/
ADD
is an instruction that copies files and directories to the container. So from the above, we copy our requirements.txt to the destination folder code
.
6. RUN pip install -r requirements.txt
This RUN
instruction will install the requirements using pip.
7. ADD . /code/
This ADD
instruction will copy all the contents in our project directory to the destination folder code
.
Edit or Create Your Project’s .env
Create a file called .env
if you haven’t created one, or edit the one you have now, and make sure you put in these database variables to your .env
according to your liking:
- DATABASE_NAME
- DATABASE_USER
- DATABASE_PASSWORD
Here’s an example of the database variables my .env
file from my PPL group project:
Create docker-composse.yml
Create a file called docker-compose.yml
inside your Django project directory and add the following configuration:
docker-compose.yml
is a file that describes the services that make our app.
As you have probably noticed, we are using the database variables that we have set in our .env
file. In addition to that, you may also notice an unfamiliar customized command called createsu
. We will create this command in the next step.
Create the createsu Command
The createsu
command is a customized command that will be used to create a default superuser for our Django project.
To create it, create a directory inside one of your Django app directories and call it management
.
Then create an empty__init__.py
file inside that management
folder.
Then inside the management
directory create another directory called commands
.
Finally, inside the commands
folder create a file called createsu.py
and configure your createsu
command according to your own user model.
Here is an example of the command from my PPL group project:
Reconfigure settings.py
Open your Django project’s settings.py
and in the DATABASES configuration, change it and set it to this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DATABASE_NAME'),
'USER': os.environ.get('DATABASE_USER'),
'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
'HOST': 'db',
'PORT': 5432,
}
}
Here, we are also using the database variables that we have set in our .env
file.
Run the Project!
We use docker-compose to run our project. Use the command here to run it:
docker-compose up
Then grab some popcorn and watch docker try to run your project!
You will then see something familiar like this when Docker successfully ran your project:
It is now finished! You can now go check localhost:8000/admin/
and see for yourself:
And there it is.
I hope this article helps you out.
That is all from me, friends!
God bless.