Fix format issue

This commit is contained in:
Baohua Yang
2026-02-21 10:19:28 -08:00
parent 063c3f1381
commit 47cfc173a6
45 changed files with 596 additions and 604 deletions

View File

@@ -18,12 +18,12 @@ Linux 系统请使用以下介绍的方法安装。
> **提示**版本更新较快请访问上述链接获取最新版本号替换下方命令中的版本号
例如 Linux 64 位系统上直接下载对应的二进制包 v2.40.3 为例
例如 Linux 64 位系统上直接下载对应的二进制包 v5.0.2 为例
```bash
$ DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
$ mkdir -p $DOCKER_CONFIG/cli-plugins
$ curl -SL https://github.com/docker/compose/releases/download/v2.40.3/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
$ curl -SL https://github.com/docker/compose/releases/download/v5.0.2/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
```
之后执行
@@ -38,7 +38,7 @@ $ chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
```bash
$ docker compose version
Docker Compose version v2.40.3
Docker Compose version v5.0.2
```
### bash 补全命令
@@ -46,7 +46,7 @@ Docker Compose version v2.40.3
运行以下命令
```bash
$ curl -L https://raw.githubusercontent.com/docker/compose/v2.40.3/contrib/completion/bash/docker-compose | sudo tee /etc/bash_completion.d/docker-compose > /dev/null
$ curl -L https://raw.githubusercontent.com/docker/compose/v5.0.2/contrib/completion/bash/docker-compose | sudo tee /etc/bash_completion.d/docker-compose > /dev/null
```
### 卸载

View File

@@ -84,7 +84,7 @@ command: echo "hello world"
### `configs`
仅用于 `Swarm mode`已弃用推荐使用 Kubernetes
`configs` 来自 Compose Specification它在 Swarm 中是原生对象在本地 `docker compose` 模式下通常以文件挂载的形式实现具体能力取决于 Compose 版本与运行平台
### `cgroup_parent`
@@ -108,7 +108,7 @@ container_name: docker-web-container
### `deploy`
仅用于 `Swarm mode`已弃用推荐使用 Kubernetes
`deploy` 用于描述副本数更新策略资源限制等部署参数该字段在 Swarm 中支持最完整在本地 `docker compose up` 场景下通常只有部分字段生效
### `devices`

View File

@@ -6,7 +6,7 @@
### 架构概览
在开始之前让我们先理解我们要构建的架构
在开始之前先看整体架构如图 10-1 所示
```
┌─────────────────────────────────────────────────────────────┐
@@ -28,6 +28,8 @@
(浏览器访问)
```
10-1 Django + PostgreSQL Compose 架构
**关键点**
- `web` 服务运行 Django 应用对外暴露 8000 端口
- `db` 服务运行 PostgreSQL 数据库只在内部网络可访问
@@ -44,7 +46,7 @@ $ mkdir django-docker && cd django-docker
我们需要创建三个文件`Dockerfile``requirements.txt` `compose.yaml`
### Step 1: 创建 Dockerfile
### 步骤 1创建 Dockerfile
```docker
FROM python:3.12-slim
@@ -82,7 +84,7 @@ COPY . /code/
> 💡 **笔者建议**总是将变化频率低的文件先复制变化频率高的后复制这样可以最大化利用 Docker 的构建缓存
### Step 2: 创建 requirements.txt
### 步骤 2创建 requirements.txt
```txt
Django>=5.0,<6.0
@@ -98,9 +100,9 @@ gunicorn>=21.0,<22.0
| `psycopg[binary]` | PostgreSQL 数据库驱动推荐使用 psycopg 3 |
| `gunicorn` | 生产环境 WSGI 服务器可选开发时可不用 |
### Step 3: 创建 compose.yaml
### 步骤 3创建 compose.yaml
Step 3: 创建 `compose.yaml` 配置如下
配置如下
```yaml
services:
@@ -182,7 +184,7 @@ web:
| `depends_on` + `healthcheck` | 启动顺序 | 确保数据库就绪后 Django 才启动避免连接错误 |
| `environment` | 环境变量 | 推荐用环境变量管理配置避免硬编码 |
### Step 4: 创建 Django 项目
### 步骤 4创建 Django 项目
运行以下命令创建新的 Django 项目
@@ -214,7 +216,7 @@ django-docker/
> 💡 **Linux 用户注意**如果遇到权限问题执行 `sudo chown -R $USER:$USER .`
### Step 5: 配置数据库连接
### 步骤 5配置数据库连接
修改 `mysite/settings.py`配置数据库连接
@@ -241,7 +243,7 @@ ALLOWED_HOSTS = ['*']
Docker Compose 各服务通过服务名相互访问Docker 内置的 DNS 会将 `db` 解析为 db 服务容器的 IP 地址这是 Docker Compose 的核心功能之一
### Step 6: 启动应用
### 步骤 6启动应用
运行以下命令

View File

@@ -6,6 +6,8 @@
### 架构概览
如图 10-2 所示Rails PostgreSQL 在同一 Compose 网络中协同工作
```
┌─────────────────────────────────────────────────────────────┐
│ Docker Compose 网络 │
@@ -25,6 +27,8 @@
localhost:3000
```
10-2 Rails + PostgreSQL Compose 架构
### 准备工作
创建项目目录
@@ -35,7 +39,7 @@ $ mkdir rails-docker && cd rails-docker
需要创建三个文件`Dockerfile``Gemfile` `compose.yaml`
### Step 1: 创建 Dockerfile
### 步骤 1创建 Dockerfile
```docker
FROM ruby:3.2
@@ -70,7 +74,7 @@ COPY . /myapp
| `nodejs` | Rails Asset Pipeline 需要 |
| 先复制 Gemfile | 只有依赖变化时才重新 `bundle install` |
### Step 2: 创建 Gemfile
### 步骤 2创建 Gemfile
创建一个初始的 `Gemfile`稍后会被 `rails new` 覆盖
@@ -85,9 +89,9 @@ gem 'rails', '~> 7.1'
$ touch Gemfile.lock
```
### Step 3: 创建 compose.yaml
### 步骤 3创建 compose.yaml
Step 3: 创建 `compose.yaml` 配置如下
配置如下
```yaml
services:
@@ -123,7 +127,7 @@ volumes:
| `depends_on: db` | 确保数据库先启动 |
| `DATABASE_URL` | Rails 12-factor 风格的数据库配置 |
### Step 4: 生成 Rails 项目
### 步骤 4生成 Rails 项目
使用 `docker compose run` 生成项目骨架
@@ -149,7 +153,7 @@ compose.yaml bin db public
> **Linux 用户**如遇权限问题执行 `sudo chown -R $USER:$USER .`
### Step 5: 重新构建镜像
### 步骤 5重新构建镜像
由于生成了新的 Gemfile需要重新构建镜像以安装完整依赖
@@ -157,7 +161,7 @@ compose.yaml bin db public
$ docker compose build
```
### Step 6: 配置数据库连接
### 步骤 6配置数据库连接
修改 `config/database.yml`
@@ -181,7 +185,7 @@ production:
> 💡 使用 `DATABASE_URL` 环境变量配置数据库符合 12-factor 应用原则便于在不同环境间切换
### Step 7: 启动应用
### 步骤 7启动应用
运行以下命令
@@ -201,7 +205,7 @@ web-1 | Puma starting in single mode...
web-1 | * Listening on http://0.0.0.0:3000
```
### Step 8: 创建数据库
### 步骤 8创建数据库
在另一个终端执行

View File

@@ -1,3 +1,3 @@
## 10.9 使用 compose 搭建 LNMP 环境
## 10.9 使用 Compose 搭建 LNMP 环境
本项目的维护者 [khs1994](https://github.com/khs1994) 的开源项目 [khs1994-docker/lnmp](https://github.com/khs1994-docker/lnmp) 使用 Docker Compose 搭建了一套 LNMP 环境,各位开发者可以参考该项目在 Docker 或 Kubernetes 中运行 LNMP。