# 020基础镜像重构之Python编译镜像
# 构建工具选型(来自章节d008)
# kenaito-python:3.13.7
镜像大小:451.56MB 构建耗时:首次 493.6s,再次 120.1s
# 下载源码
if [ ! -f "Python-3.13.7.tgz" ];then
curl -O https://mirrors.huaweicloud.com/python/3.13.7/Python-3.13.7.tgz
fi
if [ $? -ne 0 ]; then
echo "下载 Python-3.13.7.tgz 文件失败"
exit 1
fi
# Dockerfile
cat > Dockerfile << 'EOF'
# 阶段1:源码编译
FROM alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest AS builder
LABEL MAINTAINER="tianjun@odboy.cn"
ENV TimeZone="Asia/Shanghai"
WORKDIR /home/admin
ADD Python-3.13.7.tgz .
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone; \
sed -i 's/mirrors.cloud.aliyuncs.com/mirrors.aliyun.com/' /etc/yum.repos.d/*.repo; \
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y; \
yum install epel-release -y; \
yum install tar -y; \
yum install gcc automake autoconf libtool make -y; \
cd Python-3.13.7; \
./configure --with-ssl --prefix=/usr/local/python3; \
make && make install; \
cd /usr/local; \
tar -czvf python3.tgz python3; \
mv python3.tgz /tmp/;
# 阶段2:制作镜像
FROM alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest
LABEL MAINTAINER="tianjun@odboy.cn"
ENV TimeZone="Asia/Shanghai"
ENV PYTHON_HOME="/home/admin/python3"
ENV PATH="$PATH:${PYTHON_HOME}/bin"
WORKDIR /home/admin
COPY --from=builder /tmp/python3.tgz .
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone; \
sed -i 's/mirrors.cloud.aliyuncs.com/mirrors.aliyun.com/' /etc/yum.repos.d/*.repo; \
yum install tar -y; \
tar -xvf python3.tgz; \
rm -f python3.tgz;
CMD ["sleep", "365d"]
EOF
# 构建镜像(docker可换成buildah)
docker build --no-cache -f Dockerfile -t kenaito-python:3.13.7 .
if [ $? -ne 0 ]; then
echo "构建镜像失败"
exit 1
fi
# 运行测试
docker run kenaito-python:3.13.7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54