docker django 应用程序中的 virtualenv 的目的是什么? Python等依赖都已经安装好了,但是同时需要用pip安装很多包,看来冲突还不清楚。
你能解释一下这个概念吗?
编辑:另外,例如。我在 docker django 应用程序中创建了 virtualenv,最近安装了 pip freeze djangorestframework
并将其添加到 settings.py 中,但 docker-compose up
引发了错误 。没有名为 rest_framework 的模块。
已检查,一切正确。
Docker/virtualenv 冲突?可能是吗?
最佳答案
Docker 和容器化可能会激发您不需要虚拟环境的错觉。 distutil
的 Glpyh 对这种误解提出了非常有说服力的论据 in this pycon talk .
virtualenv
优势的相同基本方面适用于容器,因为它们适用于非容器化应用程序,因为从根本上说,您仍在运行 linux 发行版。
Debian and Red Hat are fantastically complex engineering projects. Integrating billions of lines of C code.For example, you can just
apt install libavcodec
. Oryum install ffmpeg
.Writing a working build system for one of those things is a PhD thesis. They integrate thousands of Python packages simultaneously into one working environment. They don't always tell you whether their tools use Python or not.
And so, you might want to docker exec some tools inside a container, they might be written in Python, if you
sudo pip install
your application in there, now it's all broken.So even in containers, isolate your application code from the system's
无论您是否使用 docker,您都应该始终在虚拟环境中运行您的应用程序。
现在在 docker 中,特别是使用 virtualenv 比它应该的要复杂一些。在 docker 内部,每个 RUN
命令都是独立运行的,除了文件系统更改之外,没有其他状态逐行保留。要安装到 virutalenv
,您必须预先激活 command on every line :
RUN apt-get install -y python-virtualenv
RUN virtualenv /appenv
RUN . /appenv/bin/activate; \
pip install -r requirements.txt
ENTRYPOINT . /appenv/bin/activate; \
run-the-app
关于python - 在 docker 容器内的 virtualenv 中运行 django 应用程序的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42457474/