Dockerize a Django project

Edit

Published on March 02, 2022

Author: Meet Rajesh Gor

#django #docker

Dockerize a Django project

We can run our Django projects in a Docker Container by creating a Docker image for our project. It is really easy and intuitive to create a Dockerfile for any given application as it really is a matter of writing commands in a file and basically running it is a isolated environment. To create a Docker image, we first need a Dockerfile. A Dockerfile is simply a Blueprint to create a image in Docker. In this file we specify the instructions/commands/environment variables to create a image for our app to run.

Create a Docker File

To create a dockerfile, simply create a file named as Dockerfile without any extension(not a .txt file). We need to then add the following code into the file.


dockerfile
FROM python:3.9-buster

ENV PYTHONUNBUFFERED=1

WORKDIR /code

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["python","manage.py","runserver","0.0.0.0:8000"]

Build the image

Using the Dockerfile, we can create the image for the Django project by a simple command.


docker build . -t django-app

Assuming the Dockerfile is in the same folder from which you are running this command.

Build the django-app image

Build Complete Success

Run the Image and Create a Container


docker run -p 8000:8000 django-app

Create Container

You can use any port like 3000 or 4000 on your local system by changing the first port number as 3000:8000 and so on. So, the first port number is the local system's port number and the port number followed by a : is the port number for the environment inside the container of the django project. The container port is the same which we have exposed while creating the image. You can now go to 127.0.0.1:8000 or your chosen port in your local environment and see the django app running.

If you are using a Docker as a Virtual Machine, you need to find the IP-address of that Virtual-Machine(docker-machine ip) and use that instead of localhost or 127.0.0.1.

Cleaning the Container for iterations

We need to stop the container to run it again after making a few changes in the app if any. Simply pressing the CTRL + D option wont stop the container here. We need to stop the container by executing:


docker ps

docker stop <container-id>

This will stop the container. Also if you want to remove the images you built, you can run the command:


docker image ls

docker rmi -f <image-id>

So, we now have a image of our Django project, this image can be used by anyone inside a docker environment and thus creating much more easier to test/work on a given project irrespective of the system is being used.

<a class='prev' href='/golang-test-output-json'>

    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M13.5 8.25L9.75 12L13.5 15.75" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"> </path>
    </svg>
    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>prev</p>
        <p class='prevnext-title'>Golang: Test Output JSON</p>
    </div>
</a>

<a class='next' href='/django-form-load-frontmatter'>

    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>next</p>
        <p class='prevnext-title'>Django Blog DevLog: Load Frontmatter data into Template/Model Form Fields</p>
    </div>
    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M10.5 15.75L14.25 12L10.5 8.25" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
    </svg>
</a>