Skip to content

Commit 1f49499

Browse files
committed
feat(deployment): 添加 Docker 相关配置和部署指南
新增 Dockerfile、docker-compose.yml 和 DOCKER.md 文件,支持使用 Docker 部署前后端服务。更新 README.md 以包含 Docker 部署说明,简化部署流程。
1 parent f2c549c commit 1f49499

File tree

6 files changed

+293
-25
lines changed

6 files changed

+293
-25
lines changed

DOCKER.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Docker 部署指南
2+
3+
本文档提供了使用 Docker 部署博客系统的详细说明。
4+
5+
## 前提条件
6+
7+
- 安装 [Docker](https://docs.docker.com/get-docker/)
8+
- 安装 [Docker Compose](https://docs.docker.com/compose/install/)
9+
10+
## 快速开始
11+
12+
### 1. 克隆仓库
13+
14+
```bash
15+
git clone <仓库地址>
16+
cd Blog
17+
```
18+
19+
### 2. 启动服务
20+
21+
使用以下命令启动所有服务(PostgreSQL 数据库、后端 API 和前端网站):
22+
23+
```bash
24+
docker-compose up -d
25+
```
26+
27+
这将在后台启动所有容器。首次运行时,Docker 会构建镜像,这可能需要一些时间。
28+
29+
### 3. 访问服务
30+
31+
- 前端网站: http://localhost:4321
32+
- 后端 API: http://localhost:8080
33+
- PostgreSQL 数据库: localhost:5432 (用户名: postgres, 密码: 123456, 数据库: Blog)
34+
35+
## 常用命令
36+
37+
### 查看日志
38+
39+
```bash
40+
# 查看所有服务的日志
41+
docker-compose logs
42+
43+
# 查看特定服务的日志
44+
docker-compose logs backend
45+
docker-compose logs frontend
46+
docker-compose logs postgres
47+
48+
# 实时查看日志
49+
docker-compose logs -f
50+
```
51+
52+
### 停止服务
53+
54+
```bash
55+
docker-compose down
56+
```
57+
58+
### 重新构建并启动
59+
60+
如果代码有更新,需要重新构建镜像:
61+
62+
```bash
63+
docker-compose build
64+
docker-compose up -d
65+
```
66+
67+
或者一步完成:
68+
69+
```bash
70+
docker-compose up -d --build
71+
```
72+
73+
## 数据持久化
74+
75+
PostgreSQL 数据存储在名为 `postgres_data` 的 Docker 卷中,即使容器被删除,数据也会保留。
76+
77+
## 环境配置
78+
79+
- 后端配置文件位于 `backend/config.docker.toml`
80+
- 数据库连接信息在 `docker-compose.yml` 中配置
81+
82+
## 生产环境部署注意事项
83+
84+
1. 修改 `backend/config.docker.toml` 中的 JWT 密钥
85+
2. 修改数据库密码
86+
3. 配置 HTTPS
87+
4. 根据需要调整资源限制
88+
89+
## 故障排除
90+
91+
### 数据库连接问题
92+
93+
确保 PostgreSQL 容器已经完全启动,后端服务才能成功连接。如果遇到连接问题,可以检查日志:
94+
95+
```bash
96+
docker-compose logs postgres
97+
docker-compose logs backend
98+
```
99+
100+
### 端口冲突
101+
102+
如果本地已有服务占用了 4321、8080 或 5432 端口,可以在 `docker-compose.yml` 中修改端口映射。

README.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
1-
# blog
2-
3-
**本项目是 EC 的个小站**
4-
5-
## 简介
6-
7-
一个简单的个人网站采用前后端分离架构
8-
9-
前端使用 Nextjs + React + TypeScript + shadcnui + Tailwind CSS 开发
10-
11-
后端使用 Rust + Axum + Sqlx
12-
13-
数据库使用 postgresql
14-
15-
## 感谢
16-
17-
本项目开发时,借鉴了以下这些优秀网站(排名不分先后)的很多设计
18-
19-
- [shadcn/ui](https://ui.shadcn.com/)
20-
- [shadcn-vue](https://www.shadcn-vue.com/)
21-
- [付小晨](https://fuxiaochen.com/)
22-
23-
## LICENCE
24-
25-
MIT
1+
# blog
2+
3+
**本项目是 EC 的个小站**
4+
5+
# blog
6+
## 简介
7+
8+
一个简单的个人网站采用前后端分离架构
9+
10+
前端使用 Nextjs + React + TypeScript + shadcnui + Tailwind CSS 开发
11+
12+
后端使用 Rust + Axum + Sqlx
13+
14+
数据库使用 postgresql
15+
16+
## 部署方式
17+
18+
### Docker 部署
19+
20+
本项目支持使用 Docker 进行部署,详细说明请参考 [Docker 部署指南](DOCKER.md)
21+
22+
```bash
23+
# 使用 Docker Compose 启动所有服务
24+
docker-compose up -d
25+
```
26+
27+
## 感谢
28+
29+
本项目开发时,借鉴了以下这些优秀网站(排名不分先后)的很多设计
30+
31+
- [shadcn/ui](https://ui.shadcn.com/)
32+
- [shadcn-vue](https://www.shadcn-vue.com/)
33+
- [付小晨](https://fuxiaochen.com/)
34+
35+
## LICENCE
36+
37+
MIT

backend/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM rust:1.77 as builder
2+
3+
WORKDIR /usr/src/app
4+
COPY . .
5+
6+
RUN cargo build --release
7+
8+
FROM debian:bookworm-slim
9+
10+
RUN apt-get update && apt-get install -y libssl-dev ca-certificates && rm -rf /var/lib/apt/lists/*
11+
12+
WORKDIR /usr/src/app
13+
14+
COPY --from=builder /usr/src/app/target/release/backend /usr/src/app/backend
15+
COPY --from=builder /usr/src/app/config.docker.toml /usr/src/app/config.toml
16+
17+
EXPOSE 8080
18+
19+
CMD ["./backend"]

backend/config.docker.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 服务器配置
2+
[server]
3+
host = "0.0.0.0"
4+
port = 8080
5+
6+
# 数据库配置
7+
[database]
8+
url = "postgres://postgres:123456@postgres:5432/Blog"
9+
max_connections = 5
10+
11+
# JWT配置
12+
[jwt]
13+
secret = "your_secret_key_change_this_in_production"
14+
expiration = 1140 # 过期时间(分钟)
15+
16+
#跨域配置
17+
[cors]
18+
allowed_origins = ["http://localhost:4321", "https://blog.exquisitecore.xyz", "http://frontend:4321"]
19+
allowed_methods = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
20+
allowed_headers = ["Authorization", "Content-Type"]
21+
allow_credentials = true

blog-web/Dockerfile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Stage 1: Build WASM
2+
# Use a specific Rust version for reproducibility
3+
FROM rust:1.78 as wasm-builder
4+
5+
WORKDIR /usr/src/app
6+
7+
# Install wasm-pack
8+
# cargo install is slow, consider using a pre-built image with wasm-pack or installing from binary if speed is critical
9+
RUN cargo install wasm-pack
10+
11+
# Copy wasm source code
12+
# Only copy what's needed for the WASM build to leverage Docker cache
13+
COPY ./md-wasm ./md-wasm
14+
15+
# Build the wasm package
16+
# The output will be in ./md-wasm/pkg by default
17+
RUN wasm-pack build ./md-wasm --target web
18+
19+
# Stage 2: Build Astro application
20+
FROM node:20-slim as builder
21+
22+
WORKDIR /app
23+
24+
# Install pnpm
25+
RUN npm install -g pnpm
26+
27+
# Copy package.json and lock file
28+
COPY package.json pnpm-lock.yaml ./
29+
30+
# Copy WASM build artifacts from wasm-builder stage to the public directory
31+
# Astro will serve files from the public directory automatically.
32+
# The WASM module can then be imported from /md-wasm-pkg/your_module_name.js
33+
COPY --from=wasm-builder /usr/src/app/md-wasm/pkg ./public/md-wasm-pkg
34+
35+
# Install dependencies
36+
RUN pnpm install
37+
38+
# Copy the rest of the application source code
39+
COPY . .
40+
41+
# Build the Astro application
42+
# The `build:wasm` script in package.json might be redundant now,
43+
# or could be removed/modified if it conflicts.
44+
# For now, we assume Astro's build will pick up the WASM from public.
45+
RUN pnpm build
46+
47+
# Stage 3: Production image
48+
FROM node:20-slim
49+
50+
WORKDIR /app
51+
52+
# Copy built application from the builder stage
53+
COPY --from=builder /app/dist /app/dist
54+
# Copy package.json for running the app (Astro's node adapter might need it)
55+
COPY --from=builder /app/package.json /app/
56+
57+
# Install production dependencies only
58+
# Astro's standalone mode for the node adapter bundles dependencies,
59+
# but following the original Dockerfile's pattern.
60+
RUN npm install --omit=dev
61+
62+
EXPOSE 4321
63+
64+
# Start the application
65+
CMD ["node", "./dist/server/entry.mjs"]

docker-compose.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: '3.8'
2+
3+
services:
4+
postgres:
5+
image: postgres:16
6+
container_name: blog-postgres
7+
environment:
8+
POSTGRES_PASSWORD: 123456
9+
POSTGRES_USER: postgres
10+
POSTGRES_DB: Blog
11+
volumes:
12+
- postgres_data:/var/lib/postgresql/data
13+
ports:
14+
- "5432:5432"
15+
restart: unless-stopped
16+
healthcheck:
17+
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
18+
interval: 10s
19+
timeout: 5s
20+
retries: 5
21+
22+
backend:
23+
build:
24+
context: ./backend
25+
container_name: blog-backend
26+
depends_on:
27+
postgres:
28+
condition: service_healthy
29+
environment:
30+
- DATABASE__URL=postgres://postgres:123456@postgres:5432/Blog
31+
ports:
32+
- "8080:8080"
33+
restart: unless-stopped
34+
35+
frontend:
36+
build:
37+
context: ./blog-web
38+
container_name: blog-frontend
39+
environment:
40+
- HOST=0.0.0.0
41+
- PORT=4321
42+
ports:
43+
- "4321:4321"
44+
depends_on:
45+
- backend
46+
restart: unless-stopped
47+
48+
volumes:
49+
postgres_data:

0 commit comments

Comments
 (0)