mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-11 12:21:17 +00:00
Fix format issue
This commit is contained in:
@@ -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:创建数据库
|
||||
|
||||
在另一个终端执行:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user