mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-11 12:21:17 +00:00
style(docs): standardize heading formats and numbering
- Enforce Level 1-3 structural numbering based on SUMMARY.md hierarchy - Remove structural numbering from Level 4+ headings - Eliminate single child headings by converting to bold text - Auto-inject summary text for headings with multiple children missing intro text - Exclude Appendix chapters from structural numbering - Avoid modifying code block contents - Add script to detect non-standard English usage in headers
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
本节将使用 Docker Compose 配置并运行一个 **Django + PostgreSQL** 应用。笔者不仅会介绍具体步骤,还会解释每个配置项的作用,以及开发环境和生产环境的差异。
|
||||
|
||||
### 架构概览
|
||||
### 10.6.1 架构概览
|
||||
|
||||
在开始之前,先看整体架构 (如图 10-1 所示):
|
||||
|
||||
@@ -40,7 +40,7 @@ flowchart TD
|
||||
- 两个服务通过 Docker Compose 自动创建的网络相互通信
|
||||
- `web` 服务可以通过服务名 `db` 访问数据库 (Docker 内置 DNS)
|
||||
|
||||
### 准备工作
|
||||
### 10.6.2 准备工作
|
||||
|
||||
创建一个项目目录并进入:
|
||||
|
||||
@@ -50,7 +50,7 @@ $ mkdir django-docker && cd django-docker
|
||||
|
||||
我们需要创建三个文件:`Dockerfile`、`requirements.txt` 和 `compose.yaml`。
|
||||
|
||||
### 步骤 1:创建 Dockerfile
|
||||
### 10.6.3 步骤 1:创建 Dockerfile
|
||||
|
||||
如下代码块所示,展示了相关示例:
|
||||
|
||||
@@ -90,7 +90,7 @@ COPY . /code/
|
||||
|
||||
> 💡 **笔者建议**:总是将变化频率低的文件先复制,变化频率高的后复制。这样可以最大化利用 Docker 的构建缓存。
|
||||
|
||||
### 步骤 2:创建 requirements.txt
|
||||
### 10.6.4 步骤 2:创建 requirements.txt
|
||||
|
||||
如下代码块所示,展示了相关示例:
|
||||
|
||||
@@ -108,7 +108,7 @@ gunicorn>=21.0,<22.0
|
||||
| `psycopg[binary]` | PostgreSQL 数据库驱动 (推荐使用 psycopg 3)|
|
||||
| `gunicorn` | 生产环境 WSGI 服务器 (可选,开发时可不用)|
|
||||
|
||||
### 步骤 3:创建 compose.yaml
|
||||
### 10.6.5 步骤 3:创建 compose.yaml
|
||||
|
||||
配置如下:
|
||||
|
||||
@@ -192,7 +192,7 @@ web:
|
||||
| `depends_on` + `healthcheck` | 启动顺序 | 确保数据库就绪后 Django 才启动,避免连接错误 |
|
||||
| `environment` | 环境变量 | 推荐用环境变量管理配置,避免硬编码 |
|
||||
|
||||
### 步骤 4:创建 Django 项目
|
||||
### 10.6.6 步骤 4:创建 Django 项目
|
||||
|
||||
运行以下命令创建新的 Django 项目:
|
||||
|
||||
@@ -225,7 +225,7 @@ django-docker/
|
||||
|
||||
> 💡 **Linux 用户注意**:如果遇到权限问题,执行 `sudo chown -R $USER:$USER .`
|
||||
|
||||
### 步骤 5:配置数据库连接
|
||||
### 10.6.7 步骤 5:配置数据库连接
|
||||
|
||||
修改 `mysite/settings.py`,配置数据库连接:
|
||||
|
||||
@@ -252,7 +252,7 @@ ALLOWED_HOSTS = ['*']
|
||||
|
||||
在 Docker Compose 中,各服务通过服务名相互访问。Docker 内置的 DNS 会将 `db` 解析为 db 服务容器的 IP 地址。这是 Docker Compose 的核心功能之一。
|
||||
|
||||
### 步骤 6:启动应用
|
||||
### 10.6.8 步骤 6:启动应用
|
||||
|
||||
运行以下命令:
|
||||
|
||||
@@ -275,7 +275,7 @@ web-1 | Starting development server at http://0.0.0.0:8000/
|
||||
|
||||
打开浏览器访问 http://localhost:8000,可以看到 Django 欢迎页面!
|
||||
|
||||
### 常用开发命令
|
||||
### 10.6.9 常用开发命令
|
||||
|
||||
在另一个终端窗口执行:
|
||||
|
||||
@@ -299,7 +299,7 @@ $ docker compose exec db psql -U django_user -d django_db
|
||||
|
||||
> 💡 笔者建议使用 `exec` 而不是 `run`。`exec` 在已运行的容器中执行命令,`run` 会创建新容器。
|
||||
|
||||
### 常见问题排查
|
||||
### 10.6.10 常见问题排查
|
||||
|
||||
本节涵盖了相关内容与详细描述,主要探讨以下几个方面:
|
||||
|
||||
@@ -338,7 +338,7 @@ $ docker compose logs db
|
||||
$ sudo chown -R $USER:$USER .
|
||||
```
|
||||
|
||||
### 开发 vs 生产:关键差异
|
||||
### 10.6.11 开发 vs 生产:关键差异
|
||||
|
||||
笔者特别提醒,本节的配置是 **开发环境** 配置。生产环境需要以下调整:
|
||||
|
||||
@@ -366,7 +366,7 @@ services:
|
||||
|
||||
```
|
||||
|
||||
### 延伸阅读
|
||||
### 10.6.12 延伸阅读
|
||||
|
||||
- [Compose 模板文件详解](10.5_compose_file.md):深入理解 Compose 文件的所有配置项
|
||||
- [使用 WordPress](10.8_wordpress.md):另一个 Compose 实战案例
|
||||
|
||||
Reference in New Issue
Block a user