DEV Community

Cover image for Dockerizing Django, A Step-by-Step Guide
Sudhanshu
Sudhanshu

Posted on

Dockerizing Django, A Step-by-Step Guide

Docker is a popular containerization tool that allows you to package your applications and their dependencies into lightweight, portable containers. Docker makes it easy to run your applications in a consistent environment, regardless of the host operating system.

Let's see, what we will learn.

  1. Creating a quick Django project

    1. Dockerizing the Django app using two methods:
    2. Using only Docker File
    3. Using Docker Compose (.yml file)
    4. Understanding the syntax of a docker file
    5. Understanding the syntax for Docker-compose file
    6. Make real-time changes in the docker image as you make changes in your local files (Volume mount)

Step 1: Create the Django project

First, let’s create a Django project named dockertestusing the following command:

This will create the basic file structure for the Django project, including the manage.py file, which is used to manage the project. The file structure should look like this:

dockertest
|
|-- dockertest
|    |-- __init__.py
|    |-- asgi.py
|    |-- settings.py
|    |-- urls.py
|    |-- wsgi.py
|
|-- manage.py
|-- db.sqlite3
Enter fullscreen mode Exit fullscreen mode

Now you should navigate to the parent dockertest folder in your command prompt (CMD/Bash) such that the path should look like this

To check if the application is running properly run the following command in the terminal

This should give you this page.

**Django default landing page**

If you can see the above page you have successfully created the basic Django application.

Step 2: Create the requirements.txt file

Next, we need to create a requirements.txt the file that specifies the Python libraries and dependencies required to run the Django project. Run the following command to create the requirements.txt file:

This will create a requirements.txt file in the same directory as your Django project. The file structure should now look like this:

dockertest
|
|-- dockertest
|    |-- __init__.py
|    |-- asgi.py
|    |-- settings.py
|    |-- urls.py
|    |-- wsgi.py
|
|-- manage.py
|-- db.sqlite3
|-- requirements.txt
Enter fullscreen mode Exit fullscreen mode

Step 3: Dockizing

There are two methods for Dockerizing the Django project: using only a Dockerfile, or using Docker Compose.

Method A: Using only a Dockerfile

This method is suitable for smaller applications. Now, we need to create a Dockerfile that specifies the steps needed to build a Docker image for the Django project. Such that the file structure should look like

dockertest
|
|-- dockertest
|    |-- __init__.py
|    |-- asgi.py
|    |-- settings.py
|    |-- urls.py
|    |-- wsgi.py
|
|-- manage.py
|-- db.sqlite3
|-- requirements.txt
|-- Dockerfile
Enter fullscreen mode Exit fullscreen mode

Add the following content in the docker file:

This Dockerfile specifies the following:

  • The base image to use is python:3.8-slim-buster, which is a minimal Python image based on Debian Buster. We can use different base images which you can find on the docker hub.

  • The working directory is set to /app.This basically creates a folder in which we can place all our project files, and all the commands below will work on this folder by default.

  • The COPY directive copies the requirements.txt file from the host to the image. This will copy the requirements.txt file into the WORKDIR specified above.

  • The RUN directive installs the required libraries using pip.

  • The COPY . . the command copies the rest of the Django project files from the host to the image. The first . represents the source directory (i.e. the current directory on the host), and the second . represents the destination directory (i.e. the working directory specified by the WORKDIR directive in the Dockerfile). This command will copy all the Django project files into the /app directory in the image.

  • CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]. This command will tell the docker how to run the python application created within the container.

Method B: Using Docker Compose

This method is suitable for larger or more complex applications that involve multiple services. When using the docker-compose file we can make small changes in the docker file created above.

First, create a docker-compose.yml file

dockertest
|
|-- dockertest
|    |-- __init__.py
|    |-- asgi.py
|    |-- settings.py
|    |-- urls.py
|    |-- wsgi.py
|
|-- manage.py
|-- db.sqlite3
|-- requirements.txt
|-- Dockerfile
|-- docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

And add the below content

This docker-compose.yml the file specifies the following:

  • version: In a docker-compose.yml file, the version the field specifies the version of the Docker Compose file format. Docker Compose uses this field to determine how to interpret the rest of the file.

  • A service name myapp is defined, with the following configuration:
    — The service is built using Dockerfile the current directory.
    — The current directory is mounted as a volume in the container.
    — Port 8000 on the host is mapped to port 8000 in the container.
    — The service is given the name docker_image_1 and the container is given the name docker_container_1.
    — The command to run the Django server is specified as python manage.py runserver 0.0.0.0:8000.

Step 4: Running the Dockerized Django Project

Now that you have Dockerized your Django project, you can use the following commands to run and manage your containers.

Before moving ahead let’s quickly learn about Volume in docker.

Solving the Issue of Persistent Changes in Volumes

One issue with Docker is that changes made to the files on the host system are not reflected in the container unless the container is rebuilt. This means that if you make changes to the files on your local machine and want to see those changes in the container, you will have to rebuild the container every time. This can be time-consuming and inconvenient, especially if you are making frequent code changes. To solve this issue, you can use a volume to mount the host directory in the container. This allows you to make real-time changes to the files on your local machine and have those changes immediately reflected in the container. This way, you don’t have to rebuild the container every time you make a change, which saves time and makes it easier to work with your Django project.

Method A: Using a Dockerfile

  1. Build the Docker image using the following command:

Replace MyDockerImage with the name, you want to give to your Docker image. The . at the end specifies the location of the Dockerfile.

  1. Run the Docker image to create a container using the following command:

This command will create an interactive terminal (-it), delete the container after it is stopped ( — rm), map port 8080 on your host to port 8000 in the container (-p 8080:8000), and mount the volume (-v $(pxd):/app) to enable real-time updates between your local files and the container. Replace MyDockerImage with the name of your Docker image.

If you are on windows use *“%cd%” **instead *$(pxd). **They return the path to the current directory.

Method B: Using Docker Compose

  1. If you are using a docker-compose file you can build the image easily by using

This will build the images for all the services defined in the file.

  1. Note that the docker-compose build the command only builds the images for the services, it does not start the containers. To start the containers, you can use the docker-compose up command.

Some useful commands

docker images: This command lists all the Docker images on your system. It displays the image ID, the repository and tag, the image size, and the creation date.

docker run -it Image_Name bash: This command runs the specified Docker image and starts a bash shell inside the container. The -it flag creates an interactive terminal and allows you to input commands directly into the container.

docker rm: This command removes one or more Docker containers. You can specify the container ID or name as an argument to remove a specific container.

docker rmi: This command removes one or more Docker images. You can specify the image ID or repository and tag it as an argument to remove a specific image.

docker ps -a: This command lists all the Docker containers on your system, both running and stopped. The -a flag shows all containers, not just the running ones. The output includes the container ID, the image used to create the container, the command is run, the created and status, and the container name.

To look at all the code visit my GitHub

https://github.com/Sudhanshu1304/Dockerizing-Django-Application

Conclusion

Congratulations on completing this tutorial on Dockerizing a Django project! By following the steps outlined in this tutorial, you have successfully set up Docker for your Django project.

We hope you found this tutorial on Dockerizing a Django project helpful and informative, please consider giving it a clap👏 and following me on Medium.

Thank you for reading this tutorial!!!.

Top comments (0)