DEV Community

Cover image for HOT Reload in Docker
Sm0ke
Sm0ke

Posted on • Originally published at docs.appseed.us

HOT Reload in Docker

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
Enter fullscreen mode Exit fullscreen mode

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:

  1. Go to the templates/includes/head.html file in your project directory.
  2. Open the file in a text editor and make changes to the title tag.
  3. Save the file.
  4. 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.

  1. In the Dockerfile, you will find the following configuration:
WORKDIR /app
RUN chmod +x /app/entrypoint.sh
CMD ["bash", "-c", "/app/entrypoint.sh"]
Enter fullscreen mode Exit fullscreen mode

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.

  1. 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
Enter fullscreen mode Exit fullscreen mode

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.

  1. In the docker-compose.yml file, you will find the following configuration:
volumes:
  - ./:/app
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)