diff --git a/.container/Dockerfile b/.container/Dockerfile index d055697..af08c65 100644 --- a/.container/Dockerfile +++ b/.container/Dockerfile @@ -1,107 +1,57 @@ -# 使用ARG定义可配置的构建参数 | Using ARG to define configurable build parameters -ARG PYTHON_VERSION=3.10 -ARG PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple -ARG PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright +FROM python:3.10-slim -# 第一阶段:构建依赖 | Stage 1: Build dependencies -FROM python:${PYTHON_VERSION}-slim AS builder +# 设置环境变量 +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PIP_NO_CACHE_DIR=0 \ + PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple \ + PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright \ + PLAYWRIGHT_BROWSERS_PATH=/root/.cache/ms-playwright \ + DEBIAN_FRONTEND=noninteractive -# 设置工作目录 | Set working directory -WORKDIR /build - -# 设置pip镜像源以加速下载 | Set pip mirror to accelerate downloads -ARG PIP_INDEX_URL -RUN pip config set global.index-url ${PIP_INDEX_URL} - -# 安装构建依赖 | Install build dependencies -RUN apt-get update && apt-get install -y --no-install-recommends \ - build-essential \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* - -# 复制并安装requirements.txt | Copy and install requirements.txt -COPY requirements.txt . -RUN pip install --no-cache-dir --prefix=/install -r requirements.txt - -# 第二阶段:运行时环境 | Stage 2: Runtime environment -FROM python:${PYTHON_VERSION}-slim - -# 添加构建信息标签 | Add build information labels -ARG BUILD_DATE -ARG VERSION -LABEL org.opencontainers.image.created="${BUILD_DATE}" \ - org.opencontainers.image.version="${VERSION}" \ - org.opencontainers.image.title="OWL Project" \ - org.opencontainers.image.description="OWL Project Docker Image" \ - org.opencontainers.image.source="https://github.com/yourusername/owl" - -# 设置工作目录 | Set working directory +# 设置工作目录 WORKDIR /app -# 设置pip镜像源以加速下载 | Set pip mirror to accelerate downloads -ARG PIP_INDEX_URL -RUN pip config set global.index-url ${PIP_INDEX_URL} - -# 从builder阶段复制已安装的Python包 | Copy installed Python packages from builder stage -COPY --from=builder /install /usr/local - -# 优化apt安装,减少层数 | Optimize apt installation, reduce layers +# 安装系统依赖(合并为一个RUN命令减少层数) RUN apt-get update && apt-get install -y --no-install-recommends \ - curl \ - git \ - ffmpeg \ - libsm6 \ - libxext6 \ - # 添加xvfb和相关依赖 | Add xvfb and related dependencies - xvfb \ - xauth \ - x11-utils \ + curl git ffmpeg libsm6 libxext6 xvfb xauth x11-utils \ + gcc python3-dev \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* -# 安装 Playwright 依赖(使用国内镜像源) | Install Playwright dependencies (using Chinese mirror) -ENV PLAYWRIGHT_BROWSERS_PATH=/root/.cache/ms-playwright -ARG PLAYWRIGHT_DOWNLOAD_HOST -ENV PLAYWRIGHT_DOWNLOAD_HOST=${PLAYWRIGHT_DOWNLOAD_HOST} -RUN pip install --no-cache-dir playwright && \ - playwright install --with-deps chromium +COPY pyproject.toml . +# 创建README.md文件以避免构建错误 +RUN echo "# OWL Project\n\n这是OWL项目的Docker环境。" > README.md +# 安装uv工具 +RUN pip install uv -# 创建非root用户 | Create non-root user -RUN groupadd -r owl && useradd -r -g owl -m owl +# 创建虚拟环境并安装依赖 +RUN uv venv .venv --python=3.10 && \ + . .venv/bin/activate && \ + uv pip install -e . -# 复制项目文件 | Copy project files + +# 复制项目文件 COPY owl/ ./owl/ COPY licenses/ ./licenses/ COPY assets/ ./assets/ COPY README.md . COPY README_zh.md . - -# 创建启动脚本 | Create startup script +# 创建启动脚本 RUN echo '#!/bin/bash\nxvfb-run --auto-servernum --server-args="-screen 0 1280x960x24" python "$@"' > /usr/local/bin/xvfb-python && \ chmod +x /usr/local/bin/xvfb-python -# 创建欢迎脚本 | Create welcome script +# 创建欢迎脚本 RUN echo '#!/bin/bash\necho "欢迎使用OWL项目Docker环境!"\necho "Welcome to OWL Project Docker environment!"\necho ""\necho "可用的脚本 | Available scripts:"\nls -1 *.py | grep -v "__" | sed "s/^/- /"\necho ""\necho "运行示例 | Run examples:"\necho " xvfb-python run.py # 运行默认脚本 | Run default script"\necho " xvfb-python run_deepseek_example.py # 运行DeepSeek示例 | Run DeepSeek example"\necho ""\necho "或者使用自定义查询 | Or use custom query:"\necho " xvfb-python run.py \"你的问题 | Your question\""\necho ""' > /usr/local/bin/owl-welcome && \ chmod +x /usr/local/bin/owl-welcome -# 设置工作目录 | Set working directory +# 设置工作目录 WORKDIR /app/owl -# 设置适当的权限 | Set appropriate permissions -RUN chown -R owl:owl /app -RUN mkdir -p /root/.cache && chown -R owl:owl /root/.cache -RUN chmod 644 /app/owl/.env - - -USER owl -# 切换到非root用户 | Switch to non-root user -# 注意:如果需要访问/dev/shm,可能仍需要root用户 | Note: If you need to access /dev/shm, you may still need root user -# USER owl - -# 添加健康检查 | Add health check +# 添加健康检查 HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD python -c "import sys; sys.exit(0 if __import__('os').path.exists('/app/owl') else 1)" -# 容器启动命令 | Container startup command +# 容器启动命令 CMD ["/bin/bash", "-c", "owl-welcome && /bin/bash"] \ No newline at end of file diff --git a/.container/docker-compose.yml b/.container/docker-compose.yml index 8b2969f..21048d4 100644 --- a/.container/docker-compose.yml +++ b/.container/docker-compose.yml @@ -3,47 +3,29 @@ services: build: context: .. dockerfile: .container/Dockerfile - args: - # 构建参数 | Build arguments - BUILDKIT_INLINE_CACHE: 1 - # 使用BuildKit加速构建 | Use BuildKit to accelerate build - cache_from: - - python:3.10-slim volumes: - # 挂载.env文件,方便配置API密钥 | Mount .env file for easy API key configuration + # 挂载.env文件,方便配置API密钥 - ../owl/.env:/app/owl/.env - # 可选:挂载数据目录 | Optional: Mount data directory + # 挂载数据目录 - ./data:/app/data - # 挂载缓存目录,避免重复下载 | Mount cache directories to avoid repeated downloads - - playwright-cache:/root/.cache/ms-playwright - - pip-cache:/root/.pip/cache + # 挂载缓存目录,避免重复下载 + - ~/.cache/pip:/root/.pip/cache + - ~/.cache/playwright:/root/.cache/ms-playwright environment: - # 可以在这里设置环境变量,覆盖.env文件中的设置 | Set environment variables here to override settings in .env file - OPENAI_API_KEY=${OPENAI_API_KEY} - # 添加显示相关的环境变量 | Add display-related environment variables - DISPLAY=:99 - - PLAYWRIGHT_BROWSERS_PATH=/root/.cache/ms-playwright - # 设置Python不生成.pyc文件,减少磁盘IO | Set Python to not generate .pyc files, reduce disk IO - PYTHONDONTWRITEBYTECODE=1 - # 设置Python不缓冲输出,方便查看日志 | Set Python to not buffer output for easier log viewing - PYTHONUNBUFFERED=1 - # 设置终端颜色 | Set terminal color - TERM=xterm-256color - # 启用pip缓存 | Enable pip cache - - PIP_CACHE_DIR=/root/.pip/cache ports: - # 如果项目有Web界面,可以映射端口 | If the project has a web interface, map ports - "8000:8000" - # 使用交互模式运行容器 | Run container in interactive mode stdin_open: true tty: true - # 添加共享内存大小,提高浏览器性能 | Add shared memory size to improve browser performance shm_size: 2gb - # 设置资源限制 | Set resource limits + # 简化资源限制 deploy: resources: limits: - cpus: '2' memory: 4G # 定义持久化卷,用于缓存 | Define persistent volumes for caching diff --git a/.container/run_in_docker.bat b/.container/run_in_docker.bat index 355a728..9e74c66 100644 --- a/.container/run_in_docker.bat +++ b/.container/run_in_docker.bat @@ -165,7 +165,10 @@ REM 在容器中运行指定的脚本,传递查询参数 REM Run the specified script in container, passing query parameter echo 在Docker容器中使用!PYTHON_CMD!运行脚本... echo Running script in Docker container using !PYTHON_CMD!... -%COMPOSE_CMD% exec -T !SERVICE_NAME! !PYTHON_CMD! !SCRIPT_NAME! "!QUERY!" + +REM 修改执行命令,按照README中的方式执行 +REM Modify execution command according to README +%COMPOSE_CMD% exec -T !SERVICE_NAME! bash -c "cd .. && source .venv/bin/activate && cd owl && !PYTHON_CMD! !SCRIPT_NAME! \"!QUERY!\"" if errorlevel 0 ( echo 查询完成! diff --git a/.container/run_in_docker.sh b/.container/run_in_docker.sh index 5709b11..76926a8 100755 --- a/.container/run_in_docker.sh +++ b/.container/run_in_docker.sh @@ -36,13 +36,13 @@ else fi # 检查脚本是否存在 | Check if the script exists -if [ ! -f "owl/$SCRIPT_NAME" ]; then - echo "错误 | Error: 脚本 | Script 'owl/$SCRIPT_NAME' 不存在 | does not exist" +if [ ! -f "../owl/$SCRIPT_NAME" ]; then + echo "错误 | Error: 脚本 | Script '../owl/$SCRIPT_NAME' 不存在 | does not exist" echo "可用的脚本有 | Available scripts:" if [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then - find owl -name "*.py" | grep -v "__" | sed 's/\\/\//g' + find ../owl -name "*.py" | grep -v "__" | sed 's/\\/\//g' else - ls -1 owl/*.py | grep -v "__" + ls -1 ../owl/*.py | grep -v "__" fi exit 1 fi @@ -51,8 +51,8 @@ echo "使用脚本 | Using script: $SCRIPT_NAME" echo "查询内容 | Query content: $QUERY" # 从docker-compose.yml获取服务名称(如果文件存在) | Get service name from docker-compose.yml (if file exists) -if [ -f ".container/docker-compose.yml" ]; then - DETECTED_SERVICE=$(grep -E "^ [a-zA-Z0-9_-]*:" .container/docker-compose.yml | head -1 | sed 's/^ \(.*\):.*/\1/') +if [ -f "docker-compose.yml" ]; then + DETECTED_SERVICE=$(grep -E "^ [a-zA-Z0-9_-]*:" docker-compose.yml | head -1 | sed 's/^ \(.*\):.*/\1/') if [ ! -z "$DETECTED_SERVICE" ]; then SERVICE_NAME="$DETECTED_SERVICE" echo "从docker-compose.yml检测到服务名称 | Detected service name from docker-compose.yml: $SERVICE_NAME" @@ -119,11 +119,11 @@ echo "在Docker容器中使用 $PYTHON_CMD 运行脚本... | Running script in D # 根据操作系统类型执行不同的命令 | Execute different commands based on operating system type if [[ "$OS_TYPE" == MINGW* ]] || [[ "$OS_TYPE" == CYGWIN* ]] || [[ "$OS_TYPE" == MSYS* ]]; then # Windows可能需要特殊处理引号 | Windows may need special handling for quotes - winpty $COMPOSE_CMD exec -T $SERVICE_NAME $PYTHON_CMD $SCRIPT_NAME "$QUERY" + winpty $COMPOSE_CMD exec -T $SERVICE_NAME bash -c "cd .. && source .venv/bin/activate && cd owl && $PYTHON_CMD $SCRIPT_NAME \"$QUERY\"" RESULT=$? else # macOS 或 Linux | macOS or Linux - $COMPOSE_CMD exec -T $SERVICE_NAME $PYTHON_CMD $SCRIPT_NAME "$QUERY" + $COMPOSE_CMD exec -T $SERVICE_NAME bash -c "cd .. && source .venv/bin/activate && cd owl && $PYTHON_CMD $SCRIPT_NAME \"$QUERY\"" RESULT=$? fi diff --git a/README.md b/README.md index 6259e05..4dcab72 100644 --- a/README.md +++ b/README.md @@ -261,9 +261,14 @@ cp owl/.env_template owl/.env # Option 1: Using docker-compose directly cd .container + docker-compose up -d + # Run OWL inside the container -docker-compose exec owl bash -c "xvfb-python run.py" +docker-compose exec owl bash -c "cd .. && source .venv/bin/activate && cd owl" + +#run example demo script +xvfb-python run.py # Option 2: Build and run using the provided scripts cd .container diff --git a/README_zh.md b/README_zh.md index 58e45d9..c2a4d82 100644 --- a/README_zh.md +++ b/README_zh.md @@ -257,9 +257,13 @@ cp owl/.env_template owl/.env # 选项1:直接使用docker-compose cd .container + docker-compose up -d + # 在容器中运行OWL -docker-compose exec owl bash -c "xvfb-python run.py" +docker-compose exec owl bash -c "cd .. && source .venv/bin/activate && cd owl" + +xvfb-python run.py # 选项2:使用提供的脚本构建和运行 cd .container