You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器中Entrypoint脚本无法找到已复制文件的问题排查

问题根源与修复方案

核心问题1:Dockerfile中虚拟环境激活无效

每个RUN指令都是独立的shell进程,你用RUN . ./fn_env/bin/activate激活虚拟环境后,该shell进程直接退出,后续的pip install依然使用系统全局Python环境,完全没用到你创建的fn_env虚拟环境。

核心问题2:entrypoint.sh路径错误

Dockerfile里已经设置WORKDIR /project/dir/,容器启动后当前目录就是/project/dir/,所以entrypoint脚本里的路径不需要再加./project/dir/前缀,直接用当前目录的相对路径即可。


修改后的Dockerfile

FROM --platform=arm64 python:3.8.13-buster
LABEL maintainer=kirklimushin@gmail.com 

WORKDIR /project/dir/

ENV PYTHONUNBUFFERED=1

# 复制项目文件到工作目录
COPY ./src ./src
COPY ./__init__.py ./
COPY ./unittests ./unittests
COPY ./environment.env ./
COPY ./module_requirements.txt ./
COPY ./module_constraints.txt ./
COPY ./entrypoint.sh ./
COPY ./rest_controllers.py ./
COPY ./settings.py ./

# 创建虚拟环境
RUN python -m venv fn_env

# 直接调用虚拟环境内的pip执行安装,无需激活(激活本质是设置环境变量,直接指定路径更可靠)
RUN ./fn_env/bin/pip install --upgrade pip setuptools wheel
RUN ./fn_env/bin/pip install -r module_requirements.txt -c module_constraints.txt

RUN chmod +x entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]

修改后的entrypoint.sh

#!/bin/bash

echo "Starting Entrypoint pipeline...."

echo "Activating Virtual Environment"
# 当前目录已为/project/dir/,直接用相对路径激活虚拟环境
source ./fn_env/bin/activate

pip list

echo "Running Unittests..."
# 直接使用当前目录下的测试文件路径
pytest ./unittests/ml/test_models.py
pytest ./unittests/web/test_rest_controllers.py

echo "Starting ASGI Server..."
python ./settings.py

额外优化建议

  • 如果项目根目录没有多余文件,可直接用COPY . .替代多个COPY指令,更简洁(若有不需要复制的文件,需编写.dockerignore排除)
  • 使用ENTRYPOINT ["./entrypoint.sh"]的exec格式,避免shell进程成为PID 1,方便信号传递(比如停止容器时能正确终止Python进程)

内容的提问来源于stack exchange,提问作者CraZyCoDer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 05:02:21