You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
11
11
12
+
Step 1: Create a Django Project
13
+
-------------------------------
14
+
15
+
To get started, clone our example code by running the following command:
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
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