繁体   English   中英

如何在 Dockerfile 中设置持久性 Python 虚拟环境?

[英]How can I setup a persistence Python virtual environment in a Dockerfile?

我正在使用 Dockerfile 在基本 Ubuntu 20.04 图像上构建 Python 3.7.4(这是其他软件的硬性要求)。我正在遵循本指南

如果我运行图像并按照指南操作,一切正常,但我想在 Dockerfile 中设置我的虚拟环境,并在运行图像时保持 pip 要求。

这是我的 Dockerfile 的相关部分:

...
RUN echo =============== Building and Install Python =============== \
    && cd /tmp \
    && wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz \
    && tar xvf ./Python-3.7.4.tgz \
    && cd Python-3.7.4 \
    && ./configure --enable-optimizations --with-ensurepip=install \
    && make -j 8 \
    && sudo make install

ENV VIRTUAL_ENV=/opt/python-3.7.4
ENV PATH="$VIRTUAL_ENV:$PATH"
COPY  "./hourequirements.txt" /usr/local/
RUN echo =============== Setting up Python Virtual Environment =============== \
    && python3 -m venv $VIRTUAL_ENV \
    && source $VIRTUAL_ENV/bin/activate \
    && pip install --upgrade pip \
    && pip install --no-input -r /usr/local/hourequirements.txt
...

Dockerfile 构建没有错误,但是当我运行图像时,环境不存在并且 python 3.7.4 没有显示任何已安装的要求。

如何在 Dockerfile 中使用 PIP 在虚拟环境中安装 Python 模块,并在 docker 图像运行时让它们持续存在?

通常在发布后立即找到答案。

我变了:

ENV PATH="$VIRTUAL_ENV:$PATH"

到:

ENV PATH="$VIRTUAL_ENV/bin:$PATH"

在 Dockerfile 并开始正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM