本文中的命令,均可在非docker镜像中执行,亲测可以直接使用在centos7中运行;
如:写入Dockerfile
前的命令、写入Dockerfile
后RUN
后面的命令,可以直接在shell中运行;
感觉可能有问题,可以把&&
连接的多条命令分开逐行执行,以便确认结果。
v=python:3.9.16-centos7.6
tmp_dir=/tmp/${v//:/_} # 临时文件夹
mkdir -p $tmp_dir && cd $tmp_dir
# 资源准备
docker pull centos:7.6.1810
curl -O http://mirrors.cloud.tencent.com/openssl/source/old/1.1.1/openssl-1.1.1p.tar.gz
curl -O https://mirrors.huaweicloud.com/python/3.9.16/Python-3.9.16.tgz
# 写入Dockerfile
cat > Dockerfile << EOF
from centos:7.6.1810
COPY openssl-1.1.1p.tar.gz Python-3.9.16.tgz .
# 源
RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo \
&& yum clean all \
&& yum makecache
RUN yum -y install gcc \
zlib zlib-devel \
bzip2 bzip2-devel \
ncurses ncurses-devel \
readline readline-devel \
openssl openssl-devel openssl-static \
xz lzma xz-devel \
sqlite sqlite-devel \
gdbm gdbm-devel \
tk tk-devel \
libffi libffi-devel \
&& yum install -y perl
RUN cd ~ \
&& tar zxf openssl-1.1.1p.tar.gz \
&& cd openssl-1.1.1p \
&& ./config --prefix=/usr/local/openssl shared zlib \
&& make -j4 && make install \
&& rm -rf /usr/bin/openssl \
&& rm -rf /usr/lib64/openssl \
&& ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl \
&& ln -sf /usr/local/openssl/include/openssl /usr/include/openssl \
&& ln -sf /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1 \
&& ln -sf /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 \
&& echo /usr/local/openssl/lib >> /etc/ld.so.conf
RUN cd ~ \
&& tar zxf Python-3.9.16.tgz \
&& cd Python-3.9.16 \
&& echo -e "\n_socket socketmodule.c\nSSL=/usr/local/openssl\n_ssl _ssl.c -DUSE_SSL -I\$(SSL)/include -I\$(SSL)/include/openssl -L\$(SSL)/lib -lssl -lcrypto">> Modules/Setup \
&& ./configure --prefix=/usr/local/python39 --enable-shared --with-ssl=/usr/local/openssl \
&& make -j4 && make install \
&& ln -sf /usr/local/python39/lib/libpython3.9.so.1.0 /usr/lib64/libpython3.9.so.1.0\
&& ln -sf /usr/local/python39/bin/python3 /usr/bin/python3
RUN python3 -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
EOF
docker build ./ -t $v
cd .. && rm -rf $tmp_dir