2025-09-16 21:25:24 +08:00

150 lines
3.8 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 使用 RockyLinux 8 最小化镜像作为基础
FROM rockylinux:8
# 设置元数据标签
LABEL maintainer="小蚂蚁云团队" \
version="1.0" \
description="基于CentOS 8的Python 3.9.13运行环境" \
python.version="3.9.13"
# 设置环境变量
ENV PYTHON_VERSION=3.9.13 \
PYTHON_HOME=/usr/local/python3.9.13 \
TZ=Asia/Shanghai \
LANG=en_US.UTF-8
# 设置工作目录
WORKDIR /tmp
# 第一步修复CentOS 8的软件源并安装编译工具
RUN set -eux; \
# 配置国内镜像源加速
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/*.repo && \
\
# 更新系统并安装必要的开发工具
dnf update -y && \
dnf install -y \
make \
gcc \
gcc-c++ \
kernel-devel \
openssl-devel \
bzip2-devel \
libffi-devel \
zlib-devel \
readline-devel \
sqlite-devel \
# 系统工具
curl \
tar \
xz \
gzip \
git \
glibc-langpack-en \
shadow-utils \
which \
&& \
\
# 清理缓存以减少镜像大小
dnf clean all && \
rm -rf /var/cache/dnf; \
\
# 配置时区
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
echo $TZ > /etc/timezone; \
\
# 设置语言环境
echo 'LANG="en_US.UTF-8"' > /etc/locale.conf && \
echo 'LC_ALL="en_US.UTF-8"' >> /etc/locale.conf;
# 第二步:验证编译器安装
RUN set -eux; \
\
# 检查编译器是否安装成功
echo "=== 检查编译工具 ==="; \
make --version; \
gcc --version; \
g++ --version;
# 添加Python安装包确保Python-3.9.13.tar.xz在构建上下文
ADD Python-3.9.13.tar.xz /tmp/
# 第三步编译和安装Python
RUN set -eux; \
\
# 进入解压后的Python目录
cd /tmp/Python-3.9.13 && \
\
# 配置编译选项
./configure \
--prefix=${PYTHON_HOME} \
--enable-optimizations \
--enable-shared \
--with-system-ffi \
--with-ensurepip=install \
&& \
\
# 编译和安装
make -j$(nproc) && \
make install && \
\
# 创建软链接
ln -sf ${PYTHON_HOME}/bin/python3.9 /usr/bin/python3 && \
ln -sf ${PYTHON_HOME}/bin/python3.9 /usr/bin/python && \
ln -sf ${PYTHON_HOME}/bin/pip3 /usr/bin/pip && \
\
# 配置动态链接库路径
echo "${PYTHON_HOME}/lib" > /etc/ld.so.conf.d/python3.conf && \
ldconfig && \
\
# 清理编译文件和源码
cd /tmp && \
rm -rf /tmp/Python-3.9.13;
# 设置全局PATH环境变量
ENV PATH=${PYTHON_HOME}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# 第四步验证Python安装
RUN set -eux; \
\
# 检查Python版本
echo "=== Python版本 ==="; \
python3 --version; \
python --version; \
\
# 检查pip版本
echo "=== Pip版本 ==="; \
pip --version;
# 第五步升级pip和安装常用工具
RUN set -eux; \
\
# 升级pip
pip install --upgrade pip setuptools wheel && \
\
# 安装常用Python工具
pip install virtualenv;
# 设置工作目录Python项目目录
WORKDIR /opt/apps
# 创建非root用户用于运行Python应用
RUN set -eux; \
\
# 创建用户和组
groupadd -r esxi && \
useradd -r -g esxi -d /opt/apps -s /bin/bash esxi && \
\
# 设置目录权限
chown -R esxi:esxi /opt/apps && \
chown -R esxi:esxi ${PYTHON_HOME};
# 设置健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python3 --version > /dev/null 2>&1 || exit 1
# 设置默认启动命令
CMD ["python3", "--version"]