简体   繁体   English

使 Docker 容器使用安装的最新版本的 Python

[英]Make Docker container use newest version of Python installed

I have a couple of Python modules that I use inside my Docker container and they require a higher version of Python that what's being used.我在 Docker 容器中使用了几个 Python 模块,它们需要使用更高版本的 Python。 I install Python and install the modules using:我安装 Python 并使用以下方法安装模块:

RUN apt-get update || : && apt-get install python3 -y
RUN apt-get install -y python3-pip
COPY requirements.txt /project
RUN pip3 install -r requirements.txt

Expecting I would be using the latest version of Python in my Docker container but when I go into it's shell and run python3 --version is comes as 3.4.2 which is incredibly old for my program.期望我会在我的 Docker 容器中使用最新版本的 Python,但是当我进入它的 shell 并运行python3 --version ,它是3.4.2 ,这对我的程序来说太旧了。 How do I make the default Python to be the latest I installed above without messing over the System-level python?如何使默认 Python 成为我上面安装的最新版本,而不会弄乱系统级 Python?

The image runtime I'm using for the Docker container is: node:9-slim我用于 Docker 容器的图像运行时是: node:9-slim

I don't think you can find a prebuilt python3.9 package on a debian 8 distribution as your environment is pretty old.我认为您无法在 debian 8 发行版上找到预构建的 python3.9 包,因为您的环境很旧。

The only solution is you build the python3.9 out from source code in your base container.唯一的解决方案是从基础容器中的源代码构建 python3.9。 A full workable Dockerfile as next:一个完整的 Dockerfile 如下:

FROM node:9-slim

RUN apt update; \
apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev; \
wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz; \
tar -zxvf Python-3.9.7.tgz; \
cd Python-3.9.7; \
./configure --prefix=/usr/local/python3; \
make && make install; \
ln -sf /usr/local/python3/bin/python3.9 /usr/bin/python3; \
ln -sf /usr/local/python3/bin/pip3.9 /usr/bin/pip3

Verify it:验证一下:

$ docker build -t myimage:1 .
$ docker run --rm -it myimage:1 python3 --version
Python 3.9.7
$ docker run --rm -it myimage:1 pip3 --version
pip 21.2.3 from /usr/local/python3/lib/python3.9/site-packages/pip (python 3.9)

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

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