跳到主要内容

Docker 部署

本文档介绍如何使用 Docker 部署 OpenHuman 应用。

前置要求

  • Docker 20.10+
  • Docker Compose 2.0+(可选)

构建 Docker 镜像

创建 Dockerfile

# 构建阶段
FROM node:20-alpine AS builder

WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build

# 运行阶段
FROM nginx:alpine

COPY --from=builder /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

创建 nginx.conf

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;

location / {
try_files $uri $uri/ /index.html;
}

location /zh/ {
try_files $uri $uri/ /zh/index.html;
}
}
}

构建镜像

docker build -t openhumanwiki:latest .

运行容器

docker run -d -p 8080:80 openhumanwiki:latest

访问 http://localhost:8080 查看网站。

使用 Docker Compose

创建 docker-compose.yml

version: '3.8'

services:
web:
build: .
ports:
- "8080:80"
restart: unless-stopped

# 可选:添加 Nginx 反向代理
proxy:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./ssl:/etc/nginx/ssl
restart: unless-stopped

启动服务

docker-compose up -d

生产环境配置

使用 Caddy(自动 HTTPS)

version: '3.8'

services:
web:
build: .
expose:
- "80"

caddy:
image: caddy:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- ./data:/data
depends_on:
- web

Caddyfile 配置

openhumanwiki.com {
reverse_proxy web:80
encode gzip
}

数据管理

备份

docker run --rm -v $(pwd)/data:/data alpine tar czf backup.tar.gz /data

恢复

docker run --rm -v $(pwd)/data:/data alpine tar xzf backup.tar.gz -C /

常见问题

中文路由 404

确保 nginx 配置了 try_files 正确处理 SPA 路由。

静态资源加载失败

检查 nginx 配置的 root 路径是否正确。

Docker 构建慢

可以使用 npm ci --production 跳过开发依赖。

下一步