Deploying a simple Django project to Docker

Md. Abdullah Al Arif
3 min readJan 20, 2023

--

Greetings,
We will deploy a simple Django project that is a DjangoREST api to Docker container.

Why use Docker?

  • Portability Across Machines
  • Rapid Performance
  • Light Weight
  • Isolation

I hope you have Docker and docker-compose installed on your machine. If not, follow these https://docs.docker.com/desktop/ , https://docs.docker.com/compose/install/other/

We have a working Django project that provides JWT tokens (access token & refresh token) as per request.
This project works fine in local machine.

To deploy it on docker, we need a these following 3 files: Dockerfile, docker-compose.yml and entrypoint.sh

Put those 3 files on Django project root for easy setup.

Now let’s write the Dockerfile

FROM python:3.10

EXPOSE 8000

WORKDIR /jwt

COPY requirements.txt /jwt

RUN pip install -U pip &&\
pip install -r requirements.txt

COPY . /jwt


COPY entrypoint.sh /jwt
ENTRYPOINT ["sh", "/jwt/entrypoint.sh"]

Now let’s get introduced to the syntaxes.

FROM, EXPOSE, WORKDIR, COPY, RUN, ENTRYPOINT are Dockerfile commands.

  • We are using Python v3.10 FROM the docker hub.
  • Port 8000 will be EXPOSEd or be opened for requests operation with the api.
  • Our api provides JWT tokens. So we named the working directory, WORKDIR as /jwt. This WORKDIR will also create the directory on the container by itself.
  • COPY our projects requirements.txt to the container /jwt directory
  • RUN the pip commands to upgrade to the latest pip version and install the packages in requirements.txt
  • COPY the project files/directory as well as the entrypoint.sh to the container /jwt directory
  • ENTRYPOINT will execute the entrypoint.sh file with bash (sh) compiler

Here is the entrypoint.sh file

python manage.py makemigrations
python manage.py migrate
DJANGO_SUPERUSER_PASSWORD=$SUPER_USER_PASSWORD python manage.py createsuperuser --username $SUPER_USER_NAME --email $SUPER_USER_EMAIL --noinput
python manage.py runserver 0.0.0.0:8000

$SUPER_USER_PASSWORD, $SUPER_USER_NAME, $SUPER_USER_EMAIL are environment variables listed on .env file

Now we are going to write the docker-compose.yml file

version: '3.9'

services:
jwt_wala:
image: jwt:latest
container_name: jwt_wala
build:
context: .
network: host
env_file:
- .env
ports:
- "8000:8000"

We will be using YAML version 3.9
Our service name is jwt_wala
The docker image name will be jwt and tag will be latest, you can change whatever you want. Container name will be jwt_wala.
Context is the same directory and network is the host itself.
env_file is the path to the .env file. And the working ports for this api is 8000.

Now let’s build and containerize the microservice to Docker

$ docker-compose up --build 

Run the command in the same directory where docker-compose file is.

Now it will start to install the necessary packages as well as requirements packages. It will create the container, image and be hosted on docker after the execution is complete. Sometimes it takes a long time.

Terminal output

You can see it. It is running on docker container.

After you have done building the container, you can terminate the process and start again with docker command. As it created a docker image for you.

Let’s see our created image on docker

$ docker images
$ docker container ls -a #if your container is stopped try -a argument, if running no need to use -a

If you have stopped your container by CTRL+c you can start the container with another command whenever you want. The container is now on docker. Try,

$ docker start jwt_wala #jwt_wala or your created container name

This is how you can containerize a simple Django project with docker. You need to modify Dockerfile, docker-compose.yml and entrypoint.sh files when the projects get more complex. If you use PostgreSQL, RabbitMQ, Redis or other technologies you need to add those service also in docker-compose file.

Thank you so much for reading the blog.

--

--

Md. Abdullah Al Arif

Software Engineer | Python | DjangoREST | Linux | PostgreSQL | DevOps