From d60bb77bf05c8d29e5c9cdd8728ff60c94ea7059 Mon Sep 17 00:00:00 2001 From: Petar Zivkovic Date: Fri, 6 Feb 2026 22:54:28 +0100 Subject: [PATCH] add: Docker support with multi-stage build and Compose --- .dockerignore | 28 +++++++++++++++++++ .env.docker | 7 +++++ .gitignore | 2 +- Dockerfile | 63 ++++++++++++++++++++++++++++++++++++++++++ README-zh.md | 21 +++++++++++++- README.md | 19 ++++++++++++- compose.yml | 33 ++++++++++++++++++++++ frontend/.dockerignore | 25 +++++++++++++++++ frontend/.gitignore | 2 +- frontend/Dockerfile | 33 ++++++++++++++++++++++ 10 files changed, 229 insertions(+), 4 deletions(-) create mode 100644 .dockerignore create mode 100644 .env.docker create mode 100644 Dockerfile create mode 100644 compose.yml create mode 100644 frontend/.dockerignore create mode 100644 frontend/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..027657ff --- /dev/null +++ b/.dockerignore @@ -0,0 +1,28 @@ +.git +.gitignore +.gitattributes + +*.pyc +.DS_Store +Thumbs.db +.idea/ +.vscode/ +__pycache__/ +.venv/ +env/ +venv/ +.uv-cache +.env +.env.* + +node_modules/ +WareHouse/ +data/ +temp/ +logs +.aider* + +/frontend +README* +compose.yml +Dockerfile diff --git a/.env.docker b/.env.docker new file mode 100644 index 00000000..1f49c7e2 --- /dev/null +++ b/.env.docker @@ -0,0 +1,7 @@ +BACKEND_BIND=0.0.0.0 + +FRONTEND_HOST=0.0.0.0 +FRONTEND_PORT=5173 + +# Frontend points to the backend service name and exposed port +VITE_API_BASE_URL=http://backend:6400 diff --git a/.gitignore b/.gitignore index 96b6481f..ca4ef755 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,4 @@ logs/ node_modules/ data/ temp/ -WareHouse/ \ No newline at end of file +WareHouse/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..954d39c9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,63 @@ +# ---- Builder: install deps with compilers and uv ---- +FROM python:3.12-slim AS builder +ARG DEBIAN_FRONTEND=noninteractive + +WORKDIR /app + +# System deps required to build Python packages +RUN apt-get update && apt-get install -y --no-install-recommends \ + pkg-config \ + build-essential \ + python3-dev \ + libcairo2-dev \ + && rm -rf /var/lib/apt/lists/* + +# Install uv just for dependency resolution/install +RUN pip install --no-cache-dir uv + +# Install the project virtualenv outside /app so bind-mounts don't hide it +ENV UV_PROJECT_ENVIRONMENT=/opt/venv + +# Copy dependency files first to maximize cache +COPY pyproject.toml ./ +# If you have a lock file, uncomment the next line for reproducible builds: +# COPY uv.lock ./ + +# Create the project virtualenv and install deps +RUN uv sync --no-cache + +# ---- Runtime: minimal image with only runtime libs + app ---- +FROM python:3.12-slim AS runtime +ARG DEBIAN_FRONTEND=noninteractive +ARG BACKEND_BIND=0.0.0.0 + +WORKDIR /app + +# Install only runtime system libraries (no compilers) +# Keep libcairo if your deps need it; remove if unnecessary +RUN apt-get update && apt-get install -y --no-install-recommends \ + libcairo2 \ + && rm -rf /var/lib/apt/lists/* + +# Copy the prebuilt virtualenv from the builder +COPY --from=builder /opt/venv /opt/venv + +# Copy the rest of the application code +COPY . . + +# Use the venv Python by default and keep Python output unbuffered. +# Bake default bind/port into the image; can be overridden at runtime. +ENV PATH="/opt/venv/bin:${PATH}" \ + PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + BACKEND_BIND=${BACKEND_BIND} + +# Drop privileges +RUN useradd -m appuser && chown -R appuser:appuser /app +USER appuser + +# EXPOSE is informational; compose controls published ports +EXPOSE 6400 + +# Command to run the backend server, parameterized by env +CMD ["sh", "-c", "python server_main.py --port 6400 --host ${BACKEND_BIND:-0.0.0.0}"] diff --git a/README-zh.md b/README-zh.md index 4f3dc76a..db73c288 100755 --- a/README-zh.md +++ b/README-zh.md @@ -118,7 +118,7 @@ ChatDev 已从一个专门的软件开发多智能体系统演变为一个全面 cd frontend && npm install ``` -### ⚡️ 运行应用 +### ⚡️ 运行应用(本地) #### 使用 Makefile(推荐) @@ -171,6 +171,25 @@ make dev 检查所有 YAML 文件的语法与 schema 错误。 +### 🐳 使用 Docker 运行 +你也可以通过 Docker Compose 运行整个应用。该方式可简化依赖管理,并提供一致的运行环境。 + +1. **前置条件**: + * 已安装 [Docker](https://docs.docker.com/get-docker/) 和 [Docker Compose](https://docs.docker.com/compose/install/)。 + * 请确保在项目根目录中存在用于配置 API Key 的 `.env` 文件。 + +2. **构建并运行**: + ```bash + # 在项目根目录执行 + docker compose up --build + ``` + +3. **访问地址**: + * **后端**:`http://localhost:6400` + * **前端**:`http://localhost:5173` + +> 服务在异常退出后会自动重启,本地文件的修改会同步映射到容器中,便于实时开发。 + ### 🔑 配置 * **环境变量**:在项目根目录创建一个 `.env` 文件。 diff --git a/README.md b/README.md index f9329855..38669c8e 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,6 @@ See our paper in [Multi-Agent Collaboration via Evolving Orchestration](https:// ### ⚡️ Run the Application - #### Using Makefile (Recommended) **Start both Backend and Frontent**: @@ -183,6 +182,24 @@ make dev ``` Checks all YAML files for syntax and schema errors. +### 🐳 Run with Docker +Alternatively, you can run the entire application using Docker Compose. This method simplifies dependency management and provides a consistent environment. + +1. **Prerequisites**: + * [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed. + * Ensure you have a `.env` file in the project root for your API keys. + +2. **Build and Run**: + ```bash + # From the project root + docker compose up --build + ``` + +3. **Access**: + * **Backend**: `http://localhost:6400` + * **Frontend**: `http://localhost:5173` + +> The services will automatically restart if they crash, and local file changes will be reflected inside the containers for live development. --- diff --git a/compose.yml b/compose.yml new file mode 100644 index 00000000..d2490116 --- /dev/null +++ b/compose.yml @@ -0,0 +1,33 @@ +services: + backend: + build: + context: . + dockerfile: Dockerfile + target: runtime + container_name: chatdev_backend + volumes: + - .:/app + ports: + - "6400:6400" + env_file: + - .env + - .env.docker + restart: unless-stopped + + frontend: + build: + context: ./frontend + dockerfile: Dockerfile + target: dev + container_name: chatdev_frontend + volumes: + - ./frontend:/app + - /app/node_modules + ports: + - "${FRONTEND_PORT:-5173}:5173" + env_file: + - .env + - .env.docker + depends_on: + - backend + restart: unless-stopped diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 00000000..8a7c39a8 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,25 @@ +.git +.gitignore +.gitattributes + +*.pyc +.DS_Store +Thumbs.db +.idea/ +.vscode/ +__pycache__/ +.venv/ +env/ +venv/ +.uv-cache +.env +.env.* + +node_modules/ +temp/ +logs +.aider* + +README* +compose.yml +Dockerfile diff --git a/frontend/.gitignore b/frontend/.gitignore index a547bf36..a0cc46da 100755 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -7,7 +7,7 @@ yarn-error.log* pnpm-debug.log* lerna-debug.log* -node_modules +node_modules/ dist dist-ssr *.local diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 00000000..8204de88 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,33 @@ +# ---- Dependencies: install node_modules once (cached) ---- +FROM node:24-alpine AS deps +WORKDIR /app +COPY package*.json ./ +# Prefer reproducible installs; fall back if no lockfile +RUN npm ci --no-audit --no-fund || npm install --no-audit --no-fund + +# ---- Dev runtime: hot-reload server ---- +FROM node:24-alpine AS dev +WORKDIR /app +ENV NODE_ENV=development +COPY --from=deps /app/node_modules /app/node_modules +COPY . . +EXPOSE 5173 +CMD ["npm", "run", "dev", "--", "--host"] + +# ---- Build: create production assets ---- +FROM node:24-alpine AS build +WORKDIR /app +ENV NODE_ENV=production +ARG VITE_API_BASE_URL=http://backend:6400 +ENV VITE_API_BASE_URL="" +COPY --from=deps /app/node_modules /app/node_modules +COPY . . +RUN npm run build + +# ---- Prod runtime: serve static dist with nginx ---- +FROM nginx:alpine AS prod +# For SPA deep links you might add a custom nginx.conf with index.html fallback. +# COPY frontend/nginx.conf /etc/nginx/conf.d/default.conf +COPY --from=build /app/dist /usr/share/nginx/html +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"]