在 Dockerfile 中激活 python virtualenv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48561981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:45:24  来源:igfitidea点击:

Activate python virtualenv in Dockerfile

pythondockervirtualenvdockerfile

提问by igsm

I have a Dockerfile where I try to activate python virtualenv after what, it should install all dependencies within this env. However, everything still gets installed globally. I used different approaches and non of them worked. I also do not get any errors. Where is a problem?

我有一个 Dockerfile,我尝试在其中激活 python virtualenv 之后,它应该在这个 env 中安装所有依赖项。但是,所有内容仍然会在全球范围内安装。我使用了不同的方法,但没有一个有效。我也没有收到任何错误。哪里有问题?

1. ENV PATH $PATH:env/bin

1. ENV PATH $PATH:env/bin

2. ENV PATH $PATH:env/bin/activate

2. ENV PATH $PATH:env/bin/activate

3. RUN . env/bin/activate

3. RUN . env/bin/activate

I also followed an example of a Dockerfile config for the python-runtime image on Google Cloud, which is basically the same stuff as above.

我还遵循了 Google Cloud 上 python-runtime 映像的 Dockerfile 配置示例,这与上面的内容基本相同。

Setting these environment variables are the same as running source /env/bin/activate.

设置这些环境变量与运行 source /env/bin/activate 相同。

ENV VIRTUAL_ENV /env

ENV VIRTUAL_ENV /env

ENV PATH /env/bin:$PATH

ENV PATH /env/bin:$PATH

Additionally, what does ENV VIRTUAL_ENV /envmean and how it is used?

此外,ENV VIRTUAL_ENV /env它是什么意思以及如何使用?

回答by Marcus Lind

You don't need to use virtualenv inside a Docker Container.

您不需要在 Docker 容器中使用 virtualenv。

virtualenvis used for dependency isolation. You want to prevent any dependencies or packages installed from leaking between applications. Dockerachieves the same thing, it isolates your dependencies within your container and prevent leaks between containers and between applications.

virtualenv用于依赖隔离。您希望防止安装的任何依赖项或包在应用程序之间泄漏。Docker实现了同样的事情,它隔离了容器内的依赖项,并防止容器之间和应用程序之间的泄漏。

Therefor, there is no point in using virtualenv inside a Docker Container unless you are running multiple apps in the same container, if that's the case I'd say that you're doing something wrong and the solution would be to architect your app in a better way and split them up in multiple containers.

因此,除非您在同一个容器中运行多个应用程序,否则在 Docker 容器中使用 virtualenv 毫无意义,如果是这种情况,我会说您做错了什么,解决方案是将您的应用程序构建在一个更好的方法并将它们拆分到多个容器中。

回答by Ellis Percival

There are perfectly valid reasons for using a virtualenv within a container.

在容器中使用 virtualenv 有完全正当的理由。

You don't necessarily need to activate the virtualenv to install software or use it. Try invoking the executables directly from the virtualenv's bindirectory instead:

您不一定需要激活 virtualenv 来安装或使用它。尝试直接从 virtualenv 的bin目录中调用可执行文件:

FROM python:2.7

RUN virtualenv /ve
RUN /ve/bin/pip install somepackage

CMD ["/ve/bin/python", "yourcode.py"]

回答by pinty

Although I agree with Marcus that this is not the way of doing with Docker, you can do what you want.

虽然我同意 Marcus 的观点,这不是使用 Docker 的方式,但您可以随心所欲。

Using the RUN command of Docker directly will not give you the answer as it will not execute your instructions from within the virtual environment. Instead squeeze the instructions executed in a single line using /bin/bash. The following Dockerfile worked for me:

直接使用 Docker 的 RUN 命令不会给你答案,因为它不会在虚拟环境中执行你的指令。而是使用 /bin/bash 将执行的指令压缩在一行中。以下 Dockerfile 对我有用:

FROM python:2.7

RUN virtualenv virtual
RUN /bin/bash -c "source /virtual/bin/activate && pip install pyserial && deactivate"
...

This should install the pyserial module only on the virtual environment.

这应该只在虚拟环境中安装 pyserial 模块。

回答by monitorius

Setting this variables

设置这个变量

ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

is not exactly the same as just running

并不完全等同于跑步

RUN . env/bin/activate

because activation inside single RUNwill not affect any lines below that RUNin Dockerfile. But setting environment variables through ENVwill activate your virtual environment for all RUNcommands.

因为在 single 中激活RUN不会影响RUNDockerfile 中低于该行的任何行。但是通过设置环境变量ENV将激活所有RUN命令的虚拟环境。

Look at this example:

看这个例子:

RUN virtualenv env                       # setup env
RUN which python                         # -> /usr/bin/python
RUN . /env/bin/activate && which python  # -> /env/bin/python
RUN which python                         # -> /usr/bin/python

So if you really need to activate virtualenv for the whole Dockerfile you need to do something like this:

因此,如果您确实需要为整个 Dockerfile 激活 virtualenv,则需要执行以下操作:

RUN virtualenv env
ENV VIRTUAL_ENV /env                     # activating environment
ENV PATH /env/bin:$PATH                  # activating environment
RUN which python                         # -> /env/bin/python

回答by Chirag Maliwal

If you your using python 3.x :

如果您使用 python 3.x :

RUN pip install virtualenv
RUN virtualenv -p python3.5 virtual
RUN /bin/bash -c "source /virtual/bin/activate"

If you are using python 2.x :

如果您使用的是 python 2.x :

RUN pip install virtualenv
RUN virtualenv virtual
RUN /bin/bash -c "source /virtual/bin/activate"

回答by Sergey Nevmerzhitsky

Consider a migration to pipenv- a tool which will automate virtualenv and pip interactions for you. It's recommended by PyPA.

考虑迁移到pipenv- 一个工具,它将为您自动化 virtualenv 和 pip 交互。它是由PyPA推荐的。

Reproduce environment via pipenvin a docker image is very simple:

通过pipenvdocker 镜像重现环境非常简单:

FROM python:3.7

RUN pip install pipenv

COPY src/Pipfile* ./

RUN pipenv install --deploy

...