Skip to content

Commit cd92ef1

Browse files
authored
Update docker-auto-reload.mdx
1 parent ce75bfd commit cd92ef1

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

docs/technologies/django/docker-auto-reload.mdx

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,88 @@ import SubHeading from "@site/src/components/SubHeading";
77

88
<SubHeading color="#25c2a0">How to execute Django in Docker with Code Persistence</SubHeading>
99

10-
> Code Sample: https://github.com/app-generator/django-corporate-dashboard
10+
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.
1111

12+
Step 1: Create a Django Project
13+
-------------------------------
14+
15+
To get started, clone our example code by running the following command:
16+
17+
shellCopy code
18+
19+
`$ git clone https://github.com/app-generator/django-corporate-dashboard`
20+
21+
This will create a new folder called "django-corporate-dashboard" with the project code.
22+
23+
Step 2: Dockerize the Django Project
24+
------------------------------------
25+
26+
Next, navigate to the project directory and run the application with the following command:
27+
28+
shellCopy code
29+
30+
`$ cd django-corporate-dashboard
31+
$ docker compose up -d --build`
32+
33+
Make sure the container is running properly. You can now open your browser and access the application at [http://localhost:5085](http://localhost:5085/).
34+
35+
Step 3: Change the Code
36+
-----------------------
37+
38+
To see the auto-reload feature in action, follow these steps:
39+
40+
1. Go to the `templates/includes/head.html` file in your project directory.
41+
2. Open the file in a text editor and make changes to the title tag.
42+
3. Save the file.
43+
4. Refresh your browser, and you will see the updated changes.
44+
45+
The Concept
46+
-----------
47+
48+
Let's understand how the auto-reload feature works. It utilizes the `docker volume` and `gunicorn reload` features.
49+
50+
1. In the `Dockerfile`, you will find the following configuration:
51+
52+
DockerfileCopy code
53+
54+
`WORKDIR /app
55+
RUN chmod +x /app/entrypoint.sh
56+
CMD ["bash", "-c", "/app/entrypoint.sh"]`
57+
58+
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.
59+
60+
1. In the `entrypoint.sh` file, you will find the following code:
61+
62+
bashCopy code
63+
64+
`#!/bin/bash
65+
set -e
66+
67+
# Function to start Gunicorn with dynamic reload-extra-file options
68+
start_gunicorn() {
69+
# Generate the reload-extra-file options dynamically
70+
extra_files=$(find /app/templates -name "*.html" -printf "--reload-extra-file %p ")
71+
72+
# Start Gunicorn
73+
echo "Starting Gunicorn..."
74+
gunicorn --config gunicorn-cfg.py --reload --reload-engine=poll $extra_files core.wsgi
75+
}
76+
77+
# Start Gunicorn
78+
start_gunicorn`
79+
80+
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.
81+
82+
1. In the `docker-compose.yml` file, you will find the following configuration:
83+
84+
yamlCopy code
85+
86+
`volumes:
87+
- ./:/app`
88+
89+
This configuration maps the files inside the Docker container, located at `/app`, to the current directory `./` on your host machine.
90+
91+
Conclusion
92+
----------
93+
94+
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.

0 commit comments

Comments
 (0)