docker容器内使用venv虚拟环境执行的python脚本,容器外如何实现自动化执行容器内的python脚本
问题描述:
问题背景:
1.容器内所使用的venv虚拟环境,python脚本强依赖此环境,执行命令行python test.py -m 1时ok
2.容器外使用shell命令:docker exec container -c "python test.py -m 1"报错。通过对比方式排查,执行此shell命令时python脚本并没有使用容器内的venv虚拟环境,导致执行shell命令时报错。
答
在生成镜像的时候,直接激活虚拟环境:
RUN apt-get install -y python-virtualenv
RUN virtualenv /appenv
RUN . /appenv/bin/activate; \n pip install -r requirements.txt
ENTRYPOINT . /appenv/bin/activate; \n run-the-app