基于若依RuoYi-Vue3-FastAPI 的 Docker 部署记录
最近在参与导师项目开发过程中,我选择基于若依 FastAPI + Vue3 模板作为系统框架,通过 Docker 实现前后端、数据库和缓存环境的容器化部署。
RuoYi-Vue3-FastAPI的github地址:https://github.com/insistence/RuoYi-Vue3-FastAPI
🛠 使用的技术栈
-
🐍 FastAPI(基于若依后端模板)
-
🖼 Vue3 + Vite
-
🐘 PostgreSQL
-
🧊 Redis
-
🐳 Docker / Docker Compose
🧱 结构
docker-project/ ├── docker-compose.yml ├── backend/ │ ├── Dockerfile │ ├── .env.prod # 后端环境变量配置 │ ├── requirements-pg.txt │ └── app/ # FastAPI 项目代码 │ └── sql/ # 初始化数据库 SQL 脚本 ├── frontend/ │ ├── Dockerfile │ ├── dist/ # 打包后前端文件 │ └── nginx.conf # Nginx 配置
下面是核心配置的内容:
⚙️ docker-compose.yml
version: "3.8"services:frontend:build:context: ./frontendports:- "8080:80"depends_on:- backendbackend:build:context: ./backendports:- "8001:9099"depends_on:db:condition: service_healthyredis:condition: service_starteddb:image: postgres:15restart: alwaysenvironment:POSTGRES_USER: postgrePOSTGRES_PASSWORD: (填你自己的密码)POSTGRES_DB: ruoyi-fastapivolumes:- pgdata:/var/lib/postgresql/data- ./backend/sql:/docker-entrypoint-initdb.dhealthcheck:test: ["CMD-SHELL", "pg_isready -U postgre"]interval: 5stimeout: 5sretries: 5redis:image: redis:7restart: alwaysports:- "6379:6379"volumes:pgdata:
🧩 后端 Dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY ./app /app
COPY ./requirements-pg.txt /app
COPY ./.env.prod /app/.env.prodRUN pip install --no-cache-dir -r requirements-pg.txt
CMD ["python", "app.py", "--env=prod"]
🧩 前端 Dockerfile
FROM nginx:stable-alpine
RUN rm -rf /usr/share/nginx/html/*
COPY ./dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.confEXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
🧩nginx.conf
server {
listen 80;
server_name localhost;location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}# ✅ 代理 API 请求到 FastAPI 后端
location /prod-api/ {
proxy_pass http://backend:9099/;
rewrite ^/prod-api/?(.*)$ /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}}
最后可以在根目录下新建一个start.bat 就可以双击启动了
start.bat内容如下:
@echo off
chcp 65001 >nulecho [1/3] 构建镜像中...
docker compose buildecho [2/3] 启动服务中...
docker compose up -decho [3/3] 成功!请访问:
echo 前端页面:http://localhost:8080
echo 后端接口:http://localhost:8001/docs
pause