Django - HOT Reload in Docker
This page explains how to add HIT reload in Docker for a Django project
This guide will walk you through the steps to run a Django application in a Docker container while maintaining code persistence. You will have automatic reloading every time you make changes to your code.
The Coding sample for this tutorial is published on GH: Datta Able Django
✅ Prerequisites​
Before we begin, make sure you have Docker
and Docker Compose
installed on your system.
Docker allows us to containerize our Django application, while Docker Compose helps manage multiple containers and their configurations.
If you haven't installed Docker yet, you can find detailed installation instructions for your operating system on the official Docker website.
✅ Create a Django Project​
To get started, clone our example code by running the following command:
$ git clone https://github.com/app-generator/django-corporate-dashboard
This will create a new folder called "django-corporate-dashboard" with the project code.
✅ Dockerize the Django Project​
Next, navigate to the project directory and run the application with the following command:
$ cd django-corporate-dashboard
$ docker compose up -d --build
Make sure the container is running properly. You can now open your browser and access the application at http://localhost:5085.
✅ Change the Code​
To see the auto-reload feature in action, follow these steps:
- Go to the
templates/includes/head.html
file in your project directory. - Open the file in a text editor and make changes to the title tag.
- Save the file.
- Refresh your browser, and you will see the updated changes.
✅ The Concept​
Let's understand how the auto-reload feature works. It utilizes the docker volume
and gunicorn reload
features.
- In the
Dockerfile
, you will find the following configuration:
WORKDIR /app
RUN chmod +x /app/entrypoint.sh
CMD ["bash", "-c", "/app/entrypoint.sh"]
These configurations ensure that the application is stored in the /app
directory and that the entrypoint.sh
script is executed every time the container is run.
- In the
entrypoint.sh
file, you will find the following code:
#!/bin/bash
set -e
# Function to start Gunicorn with dynamic reload-extra-file options
start_gunicorn() {
# Generate the reload-extra-file options dynamically
extra_files=$(find /app/templates -name "*.html" -printf "--reload-extra-file %p ")
# Start Gunicorn
echo "Starting Gunicorn..."
gunicorn --config gunicorn-cfg.py --reload --reload-engine=poll $extra_files core.wsgi
}
# Start Gunicorn
start_gunicorn
This code scans all files with the .html
extension, and whenever any of these files change, Gunicorn will automatically reload the server.
We don't need to add .py
files in the entrypoint.sh
because Gunicorn automatically detects changes in .py
files.
- In the
docker-compose.yml
file, you will find the following configuration:
volumes:
- ./:/app
This configuration maps the files inside the Docker container, located at /app
, to the current directory ./
on your host machine.
✅ Conclusion​
That concludes the explanation of how to use the auto-reload feature with Docker. Now you can make changes to your Django code, and the server will automatically reload, providing a seamless development experience.
✅ Resources​
- 👉 Access AppSeed for more starters and support
- 👉 Deploy Projects on Aws, Azure and DO via DeployPRO
- 👉 Create landing pages with Simpllo, an open-source site builder
- 👉 Build apps with Django App Generator (free service)