Hello Coders!
This guide will walk you through the steps to run a Django application in a Docker container while maintaining code persistence. In the end, you will have automatic reloading every time you make changes to your code. Thanks for reading!
π 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-datta-able
This will create a new folder called "django-datta-able" with the project code.
β Dockerize the Django Project
Next, navigate to the project directory and run the application with the following command:
$ cd django-datta-able
$ 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
- π Deploy Projects on Aws, Azure, and Digital Ocean via DeployPRO
- π Create an amazing landing page with Simpllo, an open-source site builder
- π Django App Generator - A 2nd generation App Builder
Top comments (0)