This page looks best with JavaScript enabled

Packaging a Django Development Environment with Docker from Scratch (4) Project Organization

 ·  β˜• 3 min read

1. Deployment Architecture

The program processing flow when a browser visits a page:

(1) The browser sends a request to Nginx. If it matches a static URL on the Nginx side, for example files such as js, css, or 404.html under the /static directory, Nginx returns the file directly. Other request URLs are forwarded to uWSGI for processing via the uwsgi_pass configuration.

(2) uWSGI parses the request headers and request body, converts the http protocol into wsgi protocol content, and finally forwards the request to Django.

(3) Through URL matching, Django finds the specified View function, accesses services such as MySQL and Redis, and assembles a Response to return to uWSGI.

(4) uWSGI generates the response headers and response body from the returned Response, converts the uWSGI protocol into http protocol content, and finally forwards the request to Nginx.

(5) The browser renders the content returned by Nginx on the page and presents it to the user.

It should be noted that Nginx, as a reverse proxy, provides load balancing capability, and at the same time, thanks to Nginx’s excellent static file serving capability, it improves the concurrency of the system. uWSGI improves Django’s concurrency through multiple workers, fully leveraging the advantages of multiple cores.

The system deployment structure diagram is as follows:

Docker Compose provides the ability to assemble multi-container applications, and can assemble multiple individual containers into a single whole. The containers needed here are:

  • One Nginx container
  • One uWSGI + Django container
  • One MySQL container
  • One Redis container
  • One RabbitMQ container

2. The File Organization Structure

The entire project is divided into six parts

  • django-devops-mysql builds the MySQL image
  • django-devops-nginx builds the Nginx image
  • django-devops-rabbitmq builds the RabbitMQ image
  • django-devops-redis builds the Redis image
  • django-devops-uwsgi builds the uWSGI and Django image
  • django-devops-compose orchestrates the five images above, mounts directories, and provides configuration files, logs, code management, the runtime environment, and so on.

The image-building directories mainly need a Dockerfile. In the django-devops-compose directory

  • The www/app directory holds the Django code
  • The www/conf directory holds configuration information for containers such as MySQL and uWSGI
  • The www/env directory holds the Python virtual runtime environment created by virtualenv
  • The www/log directory holds the various logs
  • docker-compose.yml holds the container orchestration information
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
β”œβ”€β”€ django-devops-compose
β”‚Β Β  β”œβ”€β”€ docker-compose.yml
β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β  β”œβ”€β”€ reset_data.sh
β”‚Β Β  β”œβ”€β”€ reset_docker.sh
β”‚Β Β  └── www
β”‚Β Β      β”œβ”€β”€ app
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ books_cbv
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ books_fbv
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ books_fbv_user
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ __init__.py
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ manage.py
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ README.md
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ requirements.txt
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ settings.py
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ static
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ urls.py
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ views.py
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ wsgi.py
β”‚Β Β      β”œβ”€β”€ conf
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ env
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ my.cnf
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ nginx.conf
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ uwsgi.ini
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ uwsgi.params
β”‚Β Β      β”‚Β Β  └── uwsgi.sock
β”‚Β Β      β”œβ”€β”€ env
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ bin
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ include
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ lib
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ lib64 -> lib
β”‚Β Β      β”‚Β Β  └── pip-selfcheck.json
β”‚Β Β      β”œβ”€β”€ log
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ mysql
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ nginx
β”‚Β Β      β”‚Β Β  └── uwsgi
β”‚Β Β      └── mysql
β”œβ”€β”€ django-devops-mysql
β”‚Β Β  β”œβ”€β”€ Dockerfile
β”‚Β Β  β”œβ”€β”€ mysql.repo
β”‚Β Β  └── start.sh
β”œβ”€β”€ django-devops-nginx
β”‚Β Β  β”œβ”€β”€ Dockerfile
β”‚Β Β  └── nginx.repo
β”œβ”€β”€ django-devops-rabbitmq
β”‚Β Β  └── Dockerfile
β”œβ”€β”€ django-devops-redis
β”‚Β Β  └── Dockerfile
└── django-devops-uwsgi
    β”œβ”€β”€ Dockerfile
    └── start.sh

WeChat Official Account
WRITTEN BY
WeChat Official Account