hello云胜

技术与生活

0%

定制python3.11.5镜像

之前制作了python3.6.5的Dockerfile。

现在业务需要3.11的python镜像,本以为只需要升级下python版本即可。

但是在实际解决的过程中需要很多问题。3.7以上的python有一些升级问题需要处理。

image-20231013182859157

1
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available

没有ssl模块。

经过查询得知,是openssl版本太低,需要升级到1.1.x

实际上解决openssl之后,在部署应用时又遇到了各种依赖缺失的问题

只能一一解决。(你可能会遇到自己应用的特定依赖缺失,需要你自己修改了)

最后成功的dockerfile如下。

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
FROM centos:7.5.1804
MAINTAINER uncley

ENV PATH $PATH:/usr/local/python3/bin/
ENV PYTHONIOENCODING utf-8

RUN set -ex \
# 替换yum源
&& mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup \
&& curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo \
&& sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo \
# 安装python依赖库
&& yum makecache \
&& yum -y install perl libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make wget \
&& yum clean all \
# 安装openssl1.1.1d
&& wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1d.tar.gz
&& tar -zxvf OpenSSL_1_1_1d.tar.gz && cd openssl-OpenSSL_1_1_1d && ./config --prefix=/usr/local/openssl
&& make && make install
&& mv /usr/lib64/libssl.so /usr/lib64/libssl.so.old && ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl && ln -s /usr/local/openssl/include/openssl /usr/include/openssl && ln -s /usr/local/openssl/lib/libssl.so /usr/lib64/libssl.so && echo "/usr/local/openssl/lib" >> /etc/ld.so.conf && ldconfig -v
&& rm -rf /var/cache/yum \
# 下载安装python3
&& wget https://www.python.org/ftp/python/3.11.5/Python-3.11.5.tgz \
&& mkdir -p /usr/local/python3 \
&& tar -zxvf Python-3.11.5.tgz \
&& cd Python-3.11.5 \
&& ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl \
&& make && make install && make clean \
# 修改pip默认镜像源
&& mkdir -p ~/.pip \
&& echo '[global]' > ~/.pip/pip.conf \
&& echo 'index-url = https://pypi.tuna.tsinghua.edu.cn/simple' >> ~/.pip/pip.conf \
&& echo 'trusted-host = pypi.tuna.tsinghua.edu.cn' >> ~/.pip/pip.conf \
&& echo 'timeout = 120' >> ~/.pip/pip.conf \
# 更新pip
&& pip3 install --upgrade pip \
# 安装wheel
&& pip3 install wheel \
# 删除安装包
&& cd .. \
&& rm -rf /Python* \
&& find / -name "*.py[co]" -exec rm '{}' ';' \
# 设置系统时区
&& rm -rf /etc/localtime \
&& ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime