以ubuntu为基础镜像, 再里面安装python和pip, Dockfile:
# 使用Ubuntu作为基础镜像
FROM ubuntu:20.04
# 安装依赖
RUN apt-get update && apt-get install -y \
build-essential \
libssl-dev \
libncurses5-dev \
libncursesw5-dev \
libreadline-dev \
libsqlite3-dev \
libgdbm-dev \
libbz2-dev \
libexpat1-dev \
liblzma-dev \
zlib1g-dev \
libffi-dev \
wget
# 安装Python 3.11.5
ARG PYTHON_VERSION=3.11.5
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz
RUN tar xzf Python-$PYTHON_VERSION.tgz
RUN cd Python-$PYTHON_VERSION && ./configure && make -j$(nproc) && make altinstall
# 清理不需要的包和文件
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
strip /usr/local/bin/python3.11
# 设置Python 3.11.5为默认Python版本
RUN update-alternatives --install /usr/bin/python python /usr/local/bin/python3.11 1 && \
update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.11 1 && \
update-alternatives --install /usr/bin/python3.11 python3.11 /usr/local/bin/python3.11 1
# 设置环境变量
ENV PYTHON_VERSION 3.11.5
ENV PYTHON_PIP_VERSION 22.3.1
ENV PATH="/usr/local/bin:${PATH}"
# 安装pip
RUN wget -q https://bootstrap.pypa.io/get-pip.py && \
python3.11 get-pip.py && \
rm get-pip.py
# 清理不必要的文件
RUN rm -rf /root/Python-*
# 设置工作目录
WORKDIR /code
构建镜像: "docker build -f Dockerfile -t ubuntu-python:u20.04-p3.11 ."(双引号中一点不能少)
启动一个容器:docker run -it 镜像名
这时,你就,然后执行: python --version 可以查看版本