python - 在 docker 容器内的 virtualenv 中运行 django 应用程序的目的是什么?

标签 python django docker virtualenv

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. Or yum 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/

相关文章:

python - 在 Cartopy、Robinson 投影中绘制直线

python - 从 python 运行 linux 命令

javascript - 对 django View 的 Ajax 请求未返回响应

python - 如何重命名 django rest_framework 的 search_fields 中的字段?

virtual-machine - Docker/LXC 容器是正在运行的应用程序还是内存中的东西?

django - 在哪里存储 Django 应用程序的媒体文件以便在 docker 容器更新后保存它们?

python - 类型错误 : must be str or None, 不是框架

python - 从 Windows 迁移到 Linux 时 WXPython 接口(interface)无法正常工作

Django get_or_create 方法不返回确切的对象

docker - 如何让nginx日志显示在prometheus中?