diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..3642c955 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,37 @@ +.git +.gitignore +.env +venv/ +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg +debug.log +.idea/ +.vscode/ +*.swp +*.bak +.DS_Store +node_modules/ +static/ +media/ +logs/ +data_backup.json +docker-compose*.yml +Dockerfile* \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..af98fab6 --- /dev/null +++ b/.env.example @@ -0,0 +1,15 @@ +# 数据库配置 +DATABASE_NAME=rolebasedfilemanagement +DATABASE_USER=root +DATABASE_PASSWORD=Ooin2025! +DATABASE_HOST=db +DATABASE_PORT=3306 + +# Redis配置 +REDIS_HOST=redis +REDIS_PORT=6379 + +# Django设置 +DEBUG=False +SECRET_KEY=your-secret-key-here +ALLOWED_HOSTS=localhost,127.0.0.1,your-domain.com \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..b10a7324 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +FROM python:3.11-slim + +# 设置工作目录 +WORKDIR /app + +# 安装系统依赖 +RUN apt-get update && apt-get install -y --no-install-recommends \ + default-libmysqlclient-dev \ + build-essential \ + gcc \ + && rm -rf /var/lib/apt/lists/* + +# 复制依赖文件并安装 +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# 复制项目文件 +COPY . . + +# 创建静态文件和媒体文件目录 +RUN mkdir -p /app/static /app/media + +# 收集静态文件 +RUN python manage.py collectstatic --noinput + +# 暴露端口 +EXPOSE 8000 + +# 启动命令 +CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", "role_based_system.asgi:application"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 00000000..5a0a3b9c --- /dev/null +++ b/README.md @@ -0,0 +1,79 @@ +# 基于角色的系统 - Docker部署指南 + +## 项目概述 +这是一个基于Django的基于角色的系统,使用MySQL数据库和Redis进行WebSocket通信。 + +## Docker部署步骤 + +### 前置要求 +- 安装 [Docker](https://docs.docker.com/get-docker/) +- 安装 [Docker Compose](https://docs.docker.com/compose/install/) + +### 部署步骤 + +1. **准备环境变量** + + 复制示例环境变量文件并根据需要修改: + ```bash + cp .env.example .env + ``` + +2. **构建并启动容器** + + ```bash + docker-compose up -d --build + ``` + +3. **执行数据库迁移** + + ```bash + docker-compose exec web python manage.py migrate + ``` + +4. **创建超级用户(可选)** + + ```bash + docker-compose exec web python manage.py createsuperuser + ``` + +5. **查看日志(如需调试)** + + ```bash + docker-compose logs -f + ``` + +### 访问应用 + +应用将在以下地址运行:`http://localhost:8000` + +### 停止服务 + +```bash +docker-compose down +``` + +### 完全重置(包括数据卷) + +```bash +docker-compose down -v +``` + +## 数据备份与恢复 + +### 备份数据库 + +```bash +docker-compose exec db mysqldump -u root -p rolebasedfilemanagement > backup.sql +``` + +### 恢复数据库 + +```bash +docker-compose exec -T db mysql -u root -p rolebasedfilemanagement < backup.sql +``` + +## 注意事项 + +- 在生产环境中,请确保修改默认密码和密钥 +- 生产环境应启用HTTPS +- 定期备份数据库和媒体文件 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..ea495cc6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,46 @@ +version: '3.8' + +services: + web: + build: . + restart: always + volumes: + - ./static:/app/static + - ./media:/app/media + - ./logs:/app/logs + ports: + - "8000:8000" + depends_on: + - db + - redis + environment: + - DATABASE_HOST=db + - DATABASE_USER=root + - DATABASE_PASSWORD=Ooin2025! + - DATABASE_NAME=rolebasedfilemanagement + - REDIS_HOST=redis + - REDIS_PORT=6379 + + db: + image: mysql:8.0 + restart: always + volumes: + - mysql_data:/var/lib/mysql + environment: + - MYSQL_DATABASE=rolebasedfilemanagement + - MYSQL_ROOT_PASSWORD=Ooin2025! + ports: + - "3306:3306" + command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci + + redis: + image: redis:7 + restart: always + volumes: + - redis_data:/data + ports: + - "6379:6379" + +volumes: + mysql_data: + redis_data: \ No newline at end of file