如何在 Dockerfile 中的 venv 中升级 pip?
How can I upgrade pip inside a venv inside a Dockerfile?
同时 运行
$ sudo docker build -t myproj:tag .
我收到消息了
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
并且考虑到最近偶尔出现的微妙之处并显示错误:
"/usr/bin/pip" "from pip import main" "ImportError: cannot import .."
我宁愿屈服,也确实升级。
所以我在 DockerFile
中添加 pip upgrade
命令, 在 构建 venv 之后,因为重要的 pip
是venv 中的那个(我做对了吗?)。所以我的 Dockerfile 现在有这个:
...
RUN python -m venv venv
RUN pip install --upgrade pip
...
但这样做并不能避免 "You are using pip 10.x" 消息。我错过了什么?
更新
虽然是一个有前途的建议,但都不是
RUN source venv/bin/activate
RUN pip install --upgrade pip
也不
RUN source venv/bin/activate
RUN python -m pip install --upgrade pip
删除 "You are using pip version 10.0.1, ..." 消息。
在你可以使用你的虚拟环境之前venv
你需要用
激活它
在 Windows 上:
venv\Scripts\activate.bat
在 Unix 或 MacOS 上,运行:
source venv/bin/activate
请注意,venv 是您的环境名称。您使用 RUN python -m venv venv
创建了这个环境。我强烈建议使用其他名称。
然后你可以用python -m pip install --upgrade pip
升级
通过
在Docker容器中创建虚拟环境后
RUN python -m venv venv
然后 运行 或者
RUN venv/bin/pip install --upgrade pip
或
RUN venv/bin/python -m pip install --upgrade pip
但都没有
RUN pip install --upgrade pip
也不
RUN python -m pip install --upgrade pip
对此最简单的答案就是不要在 Docker 图像中使用虚拟环境。虚拟环境为您提供了一个独立的文件系统 space,其中包含一组私有的 Python 包,这些包与系统安装不冲突,但 Docker 映像也是如此。您可以在 Docker 图像中使用系统 pip 就可以了。
FROM python:3.7
RUN pip install --upgrade pip
WORKDIR /usr/src/app
COPY . .
RUN pip install .
CMD ["myscript"]
如果你真的想要一个虚拟环境,你要么需要专门运行虚拟环境路径中的包装脚本
RUN python -m venv venv
RUN venv/bin/pip install --upgrade pip
或运行虚拟环境"activate"脚本每个运行命令;它设置的环境变量不会从一个步骤转移到另一个步骤。 (每个 运行 命令实际上在幕后执行自己的 docker run; docker commit
序列,并将在新容器中启动一个新的 shell;Dockerfile reference 稍微描述了这一点。)
RUN python -m venv venv
RUN . venv/bin/activate \
&& pip install --upgrade pip
COPY . .
RUN . venv/bin/activate \
&& pip install .
CMD ["venv/bin/myscript"]
尝试在它自己的 运行 指令中激活虚拟环境除了生成一个空操作层之外没有任何作用。
# This step does nothing
RUN . venv/bin/activate
# And therefore this upgrades the system pip
RUN pip install --upgrade pip
同时 运行
$ sudo docker build -t myproj:tag .
我收到消息了
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
并且考虑到最近偶尔出现的微妙之处并显示错误:
"/usr/bin/pip" "from pip import main" "ImportError: cannot import .."
我宁愿屈服,也确实升级。
所以我在 DockerFile
中添加 pip upgrade
命令, 在 构建 venv 之后,因为重要的 pip
是venv 中的那个(我做对了吗?)。所以我的 Dockerfile 现在有这个:
...
RUN python -m venv venv
RUN pip install --upgrade pip
...
但这样做并不能避免 "You are using pip 10.x" 消息。我错过了什么?
更新
虽然是一个有前途的建议,但都不是
RUN source venv/bin/activate
RUN pip install --upgrade pip
也不
RUN source venv/bin/activate
RUN python -m pip install --upgrade pip
删除 "You are using pip version 10.0.1, ..." 消息。
在你可以使用你的虚拟环境之前venv
你需要用
在 Windows 上:
venv\Scripts\activate.bat
在 Unix 或 MacOS 上,运行:
source venv/bin/activate
请注意,venv 是您的环境名称。您使用 RUN python -m venv venv
创建了这个环境。我强烈建议使用其他名称。
然后你可以用python -m pip install --upgrade pip
通过
在Docker容器中创建虚拟环境后RUN python -m venv venv
然后 运行 或者
RUN venv/bin/pip install --upgrade pip
或
RUN venv/bin/python -m pip install --upgrade pip
但都没有
RUN pip install --upgrade pip
也不
RUN python -m pip install --upgrade pip
对此最简单的答案就是不要在 Docker 图像中使用虚拟环境。虚拟环境为您提供了一个独立的文件系统 space,其中包含一组私有的 Python 包,这些包与系统安装不冲突,但 Docker 映像也是如此。您可以在 Docker 图像中使用系统 pip 就可以了。
FROM python:3.7
RUN pip install --upgrade pip
WORKDIR /usr/src/app
COPY . .
RUN pip install .
CMD ["myscript"]
如果你真的想要一个虚拟环境,你要么需要专门运行虚拟环境路径中的包装脚本
RUN python -m venv venv
RUN venv/bin/pip install --upgrade pip
或运行虚拟环境"activate"脚本每个运行命令;它设置的环境变量不会从一个步骤转移到另一个步骤。 (每个 运行 命令实际上在幕后执行自己的 docker run; docker commit
序列,并将在新容器中启动一个新的 shell;Dockerfile reference 稍微描述了这一点。)
RUN python -m venv venv
RUN . venv/bin/activate \
&& pip install --upgrade pip
COPY . .
RUN . venv/bin/activate \
&& pip install .
CMD ["venv/bin/myscript"]
尝试在它自己的 运行 指令中激活虚拟环境除了生成一个空操作层之外没有任何作用。
# This step does nothing
RUN . venv/bin/activate
# And therefore this upgrades the system pip
RUN pip install --upgrade pip