简体   繁体   English

创建一个运行 Ubuntu 的 docker 容器,该容器使用 Python 3.6+ 和 Z608C2BFD1742AACEDA743

[英]Creating a docker container that runs Ubuntu with Python 3.6+ and Pip

I'm trying to install Python 3.6 or above with pip in an docker container that runs Ubuntu.我正在尝试将 Python 3.6 或更高版本与 pip 安装在运行 Z3D4645423F8CE69 的 docker 容器中I've tried quite a few things with no success我已经尝试了很多没有成功的事情

FROM ubuntu:18.04
RUN apt update
RUN apt install software-properties-common -y
RUN add-apt-repository ppa:deadsnakes/ppa
RUN ln -s /usr/bin/pip3 /usr/bin/pip
RUN ln -s /usr/bin/python3.8 /usr/bin/python
RUN apt install python3.8 -y
RUN apt install pip

RUN pip install auto-sklearn
RUN pip install pandas

ADD test.py /

CMD [ "python", "./test.py" ]

This returns "Unable to locate package pip."这将返回“无法找到 package pip”。 I tried removing "apt install pip" incase Python 3.8 comes with it, but it gives me the error: "pip: not found."我尝试删除“apt install pip”,以防 Python 3.8 附带它,但它给了我错误:“pip:未找到”。

FROM ubuntu:18.04
RUN apt install software-properties-common -y
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update
RUN install python3-pip
RUN pip install auto-sklearn
RUN pip install pandas

ADD test.py /

CMD [ "python", "./test.py" ]

This installs pip, but auto-sklearn requires Python version 3.6 or higher and this installs a lower version.这会安装 pip,但 auto-sklearn 需要 Python 版本 3.6 或更高版本,而这会安装较低版本。 Auto-sklearn requires Linux as well which is why I'm using "FROM ubuntu" rather than "FROM python" cause "FROM python" seems to build a container on whatever native OS is running on the computer building the container, which for me is Windows. Auto-sklearn 也需要 Linux 这就是为什么我使用“FROM ubuntu”而不是“FROM python”的原因,因为“FROM python”似乎是在构建容器的计算机上运行的任何本机操作系统上构建容器,这对我来说是 Windows。

I see two consecutive issues here, so let's address them accordingly:我在这里看到两个连续的问题,所以让我们相应地解决它们:

Issue 1: missing pip in the Ubuntu image问题 1:Ubuntu 映像中缺少 pip

This returns "Unable to locate package pip."这将返回“无法找到 package pip”。 I tried removing "apt install pip" incase Python 3.8 comes with it, but it gives me the error: "pip: not found."我尝试删除“apt install pip”,以防 Python 3.8 附带它,但它给了我错误:“pip:未找到”。

That's right.这是正确的。 If you inspect the contents of the /usr/bin directory of the pulled image, you will notice that there is no pip or pip3 there.如果您检查拉取的镜像的/usr/bin目录的内容,您会注意到那里没有pippip3 So RUN ln -s /usr/bin/pip3 /usr/bin/pip line in your Dockerfile does nothing.所以RUN ln -s /usr/bin/pip3 /usr/bin/pip行中的 Dockerfile 什么都不做。 Even when python3.6 gets installed in the container (after calling apt install software-properties-common -y ), you do not get pip with it.即使在容器中安装了python3.6 (在调用apt install software-properties-common -y之后),您也不会得到pip

Solution: install pip解决方法:安装pip

The following commands can be used to install python3.6 binary and the corresponding pip :以下命令可用于安装python3.6二进制文件和相应的pip

RUN apt-get update
RUN apt-get install python3-pip

This installs both python3.6 and pip3 in the /usr/bin directory of your ubuntu:18/04 container.这会将python3.6pip3安装在ubuntu:18/04容器的/usr/bin目录中。

Issue 2: auto-sklearn requires python >= 3.7问题 2:auto-sklearn 需要 python >= 3.7

Even if you manage to get both python3.6 and pip for python3.6 , installation of auto-sklearn might still fail with the following error:即使您设法同时获得python3.6pippython3.6 ,安装auto-sklearn仍可能会失败并出现以下错误:

    RuntimeError: Python version >= 3.7 required.

This is because some of the dependencies (eg ConfigSpace package) require python version >= 3.7.这是因为某些依赖项(例如ConfigSpace包)需要 python 版本 >= 3.7。

Solution:解决方案:

This answer explains how to install pip for python3.8 on Ubuntu: https://stackoverflow.com/a/63207387/15043192这个答案解释了如何在 Ubuntu 上为python3.8安装piphttps://stackoverflow.com/a/632092737

You can follow it or get both pip and python3.8 installed using the following sequence:您可以按照它或使用以下顺序安装pippython3.8

Install python3.8:安装python3.8:

RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt update
RUN apt install python3.8

Install python3.6 and pip for python3.6为python3.6安装python3.6和pip

RUN apt install python3-pip

Now if you execute python3.6 -m pip --version in the container, you would get something like (the version of pip might be different):现在,如果你在容器中执行python3.6 -m pip --version ,你会得到类似的东西( pip的版本可能不同):

pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Install pip for python3.8为python3.8安装pip

Note: here we use previously installed pip for python3.6 to install pip for python3.8 .注意:这里我们使用之前安装的pip for python3.6来安装pip for python3.8 Do not ask me why:-)别问我为什么:-)

RUN python3.8 -m pip install pip --upgrade

Install auto-sklearn安装自动sklearn

RUN python3.8 -m pip install auto-sklearn

Note: the command above might also install pandas package among other dependencies of auto-sklearn .注意:上面的命令可能还会安装pandas package 以及auto-sklearn的其他依赖项。

Create a symbolic link to python3.8创建指向python3.8的符号链接

This would change the default这将更改默认值

RUN ln -s /usr/bin/python3.8 /usr/bin/python

Now if you execute python -m pip --version in the container, you would get something like (the version of pip might be different):现在,如果你在容器中执行python -m pip --version ,你会得到类似的东西( pip的版本可能不同):

pip 21.2.4 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)

Grand finale:大结局:

In the end, your Dockerfile should look like this:最后,您的 Dockerfile 应该如下所示:

FROM ubuntu:18.04

RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt update
RUN apt install python3.8
RUN apt install python3-pip
RUN python3.8 -m pip install auto-sklearn
RUN python3.8 -m pip install pandas
RUN ln -s /usr/bin/python3.8 /usr/bin/python

ADD test.py /

CMD [ "python", "./test.py" ]

NB注意

To avoid messing around with different versions of python and pip , you might want to have a look into virtual environments .为避免混淆pythonpip的不同版本,您可能需要查看虚拟环境

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

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