在 Docker 中安装和使用 pip 和 virtualenv [英] Install and using pip and virtualenv in Docker

查看:71
本文介绍了在 Docker 中安装和使用 pip 和 virtualenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Ubuntu 的 Docker 容器中安装 Python、Pip 和 virtualenv,我通过 Dockerfile 创建镜像:

I want to install Python, Pip and virtualenv in a Docker container which is Ubuntu, I create the image through Dockerfile:

FROM ubuntu:16.04

RUN apt-get update -y

RUN apt-get

RUN apt-get python3 -y

RUN apt-get install python3-pip -y

RUN pip install virtualenv
...

到这里的时候出现了/bin/sh: 1: pip: not found",但是在安装过程中没有出现异常,是不是我没有安装pip对吧?或者我应该在使用 pip order 之前做其他事情吗?

When it went here, it came out "/bin/sh: 1: pip: not found", but there's no exception showed in the installation process, is this mean I didn't install pip right? Or should I do anything else before I use pip order?

然后我像这样更改了 Dockerfile:

Then I changed the Dockerfile like this:

...
RUN apt-get python3 -y

RUN apt-get install python3-pip -y

RUN apt-get install python3-virtualenv -y 

RUN virtualenv --no-stie-packages -p /path/python3 py3env
...

但它仍然说/bin/sh: 1: virtualenv: not found, 就是这样

but it still saying /bin/sh: 1: virtualenv: not found, here is it

我也安装了git,git clone命令运行正确,可以使用.我哪里错了,或者我该怎么办?

I also installed git, git clone order ran correctly, can be used. Where am I wrong, or how should I do?

推荐答案

在基于 Debian 的平台上,包括 Ubuntu,python3-pip 安装的命令称为 pip3 以便它与任何系统安装的 Python 2 和平共处,并且它的 pip.

On Debian-based platforms, including Ubuntu, the command installed by python3-pip is called pip3 in order for it to peacefully coexist with any system-installed Python 2 and its pip.

有点类似,virtualenv 命令不是由 python3-virtualenv 包安装的;要做到这一点,你需要 apt-get install -y virtualenv.

Somewhat similarly, the virtualenv command is not installed by the package python3-virtualenv; to get that, you need apt-get install -y virtualenv.

请注意,venv 包含在 Python 3 标准库中,因此您根本不需要安装任何东西.

Note that venv is included in the Python 3 standard library, so you don't really need to install anything at all.

python3 -m venv newenv

不过,你为什么要在 Docker 中使用 virtualenv?(在 情况下这是有意义的,但在绝大多数情况下,您希望 Docker 容器尽可能简单,这意味着,以 root 身份安装所有内容,并在出现问题时重建整个容器需要更新.)

Why would you want a virtualenv inside Docker anyway, though? (There are situations where it makes sense but in the vast majority of cases, you want the Docker container to be as simple as possible, which means, install everything as root, and rebuild the whole container if something needs to be updated.)

顺便说一句,您通常希望尽量减少 RUN 语句的数量.在调试时创建多个层也许是有道理的,但是什么都不做的层绝对是浪费.或许还会发现apt-get一次可以安装多个包.

As an aside, you generally want to minimize the number of RUN statements. Making many layers while debugging is perhaps defensible, but layers which do nothing are definitely just wasteful. Perhaps also discover that apt-get can install more than one package at a time.

RUN apt-get update -y && \
    apt-get install -y python3 python3-pip && \
    ...

只要其中一个命令失败,&& 就会导致整个 RUN 序列失败.

The && causes the entire RUN sequence to fail as soon as one of the commands fails.

这篇关于在 Docker 中安装和使用 pip 和 virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆