- Django 3 Web Development Cookbook
- Aidas Bendoraitis Jake Kronika
- 787字
- 2025-04-04 13:15:07
How it works...
The structure of the code in the boilerplate is similar to the one in a virtual environment. The project source files are in the src directory. We have the git-hooks directory for the pre-commit hook that is used to track the last modification date and the config directory for the configurations of the services used in the containers:
django_docker
├── config/
│ └── nginx/
│ └── conf.d/
│ └── myproject.conf
├── git-hooks/
│ ├── install_hooks.sh
│ └── pre-commit
├── src/
│ └── myproject/
│ ├── locale/
│ ├── media/
│ ├── myproject/
│ │ ├── apps/
│ │ │ └── __init__.py
│ │ ├── settings/
│ │ │ ├── __init__.py
│ │ │ ├── _base.py
│ │ │ ├── dev.py
│ │ │ ├── last-update.txt
│ │ │ ├── production.py
│ │ │ ├── staging.py
│ │ │ └── test.py
│ │ ├── site_static/
│ │ │ └── site/
│ │ │ ├── css/
│ │ │ ├── img/
│ │ │ ├── js/
│ │ │ └── scss/
│ │ ├── templates/
│ │ │ ├── base.html
│ │ │ └── index.html
│ │ ├── __init__.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── requirements/
│ │ ├── _base.txt
│ │ ├── dev.txt
│ │ ├── production.txt
│ │ ├── staging.txt
│ │ └── test.txt
│ ├── static/
│ └── manage.py
├── Dockerfile
├── LICENSE
├── README.md
├── build_dev.sh
├── build_dev_example.sh
└── docker-compose.yml
The main Docker-related configurations are at docker-compose.yml and Dockerfile. Docker Compose is a wrapper around Docker's command-line API. The build_dev.sh script builds and runs the Django project under the Gunicorn WSGI HTTP server at port 8000, Nginx at port 80 (serving static and media files and proxying other requests to Gunicorn), and the PostgreSQL database at port 5432.
In the docker-compose.yml file, the creation of three Docker containers is requested:
- nginx for the Nginx web server
- gunicorn for the Django project with the Gunicorn web server
- db for the PostgreSQL database
The nginx and db containers will be created from the official images located at https://hub.docker.com. They have specific configuration parameters, such as the ports they are running on, environment variables, dependencies on other containers, and volumes.
Docker volumes are specific directories that stay untouched when you rebuild the Docker containers. Volumes need to be defined for the database data files, media, static, and the like.
The gunicorn container will be built from the instructions at the Dockerfile, defined by the build context in the docker-compose.yml file. Let's examine each layer (or instruction) there:
- The gunicorn container will be based on the python:3.7 image
- It will take PIP_REQUIREMENTS as an argument from the docker-compose.yml file
- It will set environment variables for the container
- It will install and upgrade pip, setuptools, and virtualenv
- It will create a system user named myproject for the Django project
- It will set myproject as the current user
- It will set the home directory of the myproject user as the current working directory
- It will create a virtual environment there
- It will copy pip requirements from the base computer to the Docker container
- It will install the pip requirements for the current environment defined by the PIP_REQUIREMENTS variable
- It will copy the source of the entire Django project
The content of config/nginx/conf.d/myproject.conf will be saved under /etc/nginx/conf.d/ in the nginx container. This is the configuration of the Nginx web server telling it to listen to port 80 (the default HTTP port) and forward requests to the Gunicorn server on port 8000, except for requests asking for static or media content:
#/etc/nginx/conf.d/myproject.conf
upstream myproject {
server django_docker_gunicorn_1:8000;
}
server {
listen 80;
location / {
proxy_pass http://myproject;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
rewrite "/static/\d+/(.*)" /static/$1 last;
location /static/ {
alias /home/myproject/static/;
}
location /media/ {
alias /home/myproject/media/;
}
}
You can learn more about Nginx and Gunicorn configurations in the Deploying on Nginx and Gunicorn for the staging environment and Deploying on Nginx and Gunicorn for the production environment recipes in Chapter 12, Deployment.