HOT Reload in Docker
Tags: docker, django, webdev
Published: 2023-09-15 17:18:12

---
title: HOT Reload in Docker
published: true
description: This page explains how to add HIT reload in Docker for a Django project
tags: docker, django, webdev
cover_image: https://github-production-user-asset-6210df.s3.amazonaws.com/51070104/268349538-567987be-5875-4baa-bf9f-4b69be4f0a51.png
canonical_url: https://docs.appseed.us/technologies/django/docker-auto-reload/
---
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](https://github.com/app-generator/django-datta-able)  

---

## ✅ 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](https://docs.docker.com/get-docker/).

---

## ✅ 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](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"]
```

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:

```bash
#!/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.

1.  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](https://www.docs.deploypro.dev/) via **DeployPRO**
- 👉 Create an amazing landing page with [Simpllo, an open-source site builder](https://www.simpllo.com/)
- 👉 [Django App Generator](https://app-generator.dev/django/) - A 2nd generation App Builder