npm i -g @cloudbase/cli
在仓库根目录下增加 .ide/Dockerfile 文件,自定义 Python 3.13.11 和 Django 5.2.10(LTS 最稳定版本)的开发环境。
# 使用 Python 3.13.11 官方镜像作为基础镜像(最新维护版本) FROM python:3.13.11-slim # 安装必要的系统依赖 RUN apt-get update && apt-get install -y \ curl \ git \ wget \ unzip \ openssh-server \ build-essential \ libpq-dev \ nodejs \ npm \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # 安装 code-server 和 vscode 常用插件 RUN curl -fsSL https://code-server.dev/install.sh | sh \ && code-server --install-extension cnbcool.cnb-welcome \ && code-server --install-extension redhat.vscode-yaml \ && code-server --install-extension dbaeumer.vscode-eslint \ && code-server --install-extension waderyan.gitblame \ && code-server --install-extension mhutchie.git-graph \ && code-server --install-extension donjayamanne.githistory \ && code-server --install-extension tencent-cloud.coding-copilot \ && echo done # 安装 Python 相关的 VSCode 插件 RUN code-server --install-extension ms-python.python \ && code-server --install-extension ms-python.vscode-pylance \ && code-server --install-extension ms-python.debugpy \ && code-server --install-extension ms-python.isort \ && code-server --install-extension ms-python.black-formatter \ && code-server --install-extension njpwerner.autodocstring \ && echo "Python extensions installed" # 配置 pip 使用国内镜像源并升级 pip RUN pip install --upgrade pip \ && pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple # 安装 Django 5.2.10(LTS 长期支持版本,最稳定)及常用开发工具 # 注意:如需使用最新版本 Django 6.0.1,可将 Django==5.2.10 替换为 Django==6.0.1 RUN pip install --no-cache-dir \ Django==5.2.10 \ djangorestframework==3.15.2 \ django-cors-headers==4.5.0 \ python-decouple==3.8 \ psycopg2-binary==2.9.10 \ redis==5.2.0 \ celery==5.4.0 \ gunicorn==23.0.0 \ uvicorn==0.32.0 \ pillow==11.0.0 \ django-filter==24.3 \ django-extensions==3.2.3 \ drf-spectacular==0.27.2 \ coverage==7.6.1 \ pytest==8.3.3 \ pytest-django==4.9.0 \ black==24.10.0 \ flake8==7.1.1 \ isort==5.13.2 \ mypy==1.11.2 \ django-debug-toolbar==4.4.6 \ && echo "Django and Python packages installed" # 指定字符集支持命令行输入中文 ENV LANG C.UTF-8 ENV LANGUAGE C.UTF-8 ENV PYTHONUNBUFFERED 1 # 设置工作目录 WORKDIR /workspace
# .cnb.yml
$:
vscode:
- docker:
build: .ide/Dockerfile
# 可选择是否同时定义 build 和 image
# 此时会优先使用 .ide/Dockerfile 构建镜像
# 如果 .ide/Dockerfile 构建失败,则使用 image 指定的镜像保证环境能启动成功
# image: python:3.13
services:
- vscode
- docker
# 开发环境启动后会执行的任务
stages:
- name: check-python
script: python --version
- name: check-django
script: python -c "import django; print('Django version:', django.__version__)"
- name: check-node
script: node --version
- name: list-installed
script: pip list | head -20
开发环境启动后,会自动检查 Python 和 Django 版本。
# 在终端中运行
init-django.sh
或手动创建:
# 创建 Django 项目
django-admin startproject myproject
# 或在当前目录创建
django-admin startproject config .
# 运行数据库迁移
python manage.py migrate
# 创建超级管理员
python manage.py createsuperuser
# 启动开发服务器
python manage.py runserver
| 包名 | 版本 | 用途 |
|---|---|---|
| Django | 5.2.10 | Web 框架(LTS 长期支持版本,最稳定) |
| djangorestframework | 3.15.2 | Django REST API 框架 |
| django-cors-headers | 4.5.0 | 跨域请求处理 |
| psycopg2-binary | 2.9.10 | PostgreSQL 数据库驱动 |
| redis | 5.2.0 | Redis 客户端 |
| celery | 5.4.0 | 异步任务队列 |
| gunicorn | 23.0.0 | WSGI HTTP 服务器 |
| uvicorn | 0.32.0 | ASGI 服务器 |
| drf-spectacular | 0.27.2 | OpenAPI schema 生成 |
| pytest-django | 4.9.0 | Django 测试框架 |
# 代码格式化
black .
isort .
# 代码检查
flake8 .
# 类型检查
mypy .
# 运行所有测试
pytest
# 查看测试覆盖率
pytest --cov=.
# 生成覆盖率报告
coverage report
coverage html
可以在项目根目录创建 .env 文件配置环境变量:
# .env
DEBUG=True
SECRET_KEY=your-secret-key-here
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
REDIS_URL=redis://localhost:6379/0
ALLOWED_HOSTS=localhost,127.0.0.1
# 创建 Django 应用
python manage.py startapp myapp
# 数据库迁移
python manage.py makemigrations
python manage.py migrate
# 收集静态文件
python manage.py collectstatic
# Django Shell
python manage.py shell
# 查看所有 URL
python manage.py show_urls
与原 CNB 自定义开发环境文档相比,本配置有以下增强:
FROM node:20FROM python:3.13-slim(同时包含 Node.js 和 npm)ms-python.python - Python 语言支持ms-python.vscode-pylance - 智能代码提示ms-python.debugpy - 调试器ms-python.isort - import 排序ms-python.black-formatter - 代码格式化njpwerner.autodocstring - 自动生成文档字符串nodejs 和 npm - Node.js 支持build-essential - 编译工具链libpq-dev - PostgreSQL 开发库完整的 Django 开发栈,包含 24 个常用包:
# 新增环境检查任务
- check-python # Python 版本检查
- check-django # Django 版本检查
- check-node # Node.js 版本检查
- list-installed # 已安装包列表(前20个)
本配置同时支持:
python:3.13.11-slim 镜像,体积较小PYTHONUNBUFFERED=1 确保 Python 输出实时显示LANG=C.UTF-8 配置Django==5.2.10 替换为 Django==6.0.1