基于CentOS7.3基础镜像编译构建Python3.9.16可用于生产环境的最小基础镜像

2023-12-18 11:58:23 浏览数 (3)

本文介绍镜像构建解决所有报错,并设置时区、字符集,可直接应用于生产环境,不妥之处欢迎指正。centos-7.3.1611-x86_64-docker.tar.xz包下载参见:https://gitee.com/cxwn/images.git

代码语言:javascript复制
FROM scratch as PYTHON

ARG OS_XZ="centos-7.3.1611-x86_64-docker.tar.xz"
ARG PYTHON_VERSION="3.9.16"
ARG OPENSSL_VERSION="3.0.8"

ADD [ "${OS_XZ}", "/" ]
ADD [ "Python-${PYTHON_VERSION}.tar.xz", "/" ]
ADD [ "openssl-${OPENSSL_VERSION}.tar.gz", "/" ]

ENV LC_ALL="C"

RUN set -eu; 
    yum install -y gcc 
                   make 
                   perl 
                   zlib-devel 
                   bzip2-devel 
                   ncurses-devel 
                   sqlite-devel 
                   readline-devel 
                   tk-devel 
                   gdbm-devel 
                   xz-devel 
                   libffi-devel 
                   uuid-devel 
                   perl-IPC-Cmd

RUN set -eu; 
    cd /openssl-${OPENSSL_VERSION}; 
    ./config --prefix=/usr/local; 
    make -j `nproc`; 
    make install; 
    make clean; 
    echo "/usr/local/lib64">/etc/ld.so.conf.d/ld.local.conf; 
    echo "/usr/local/lib">>/etc/ld.so.conf.d/ld.local.conf; 
    ldconfig; 
    cd /Python-${PYTHON_VERSION}; 
    ./configure 
        --enable-optimizations 
        --with-lto 
        --enable-shared 
        --enable-ipv6 
        --enable-loadable-sqlite-extensions 
        --with-ssl-default-suites=openssl 
        --enable-option-checking=fatal 
        --with-system-expat 
        --with-openssl=/usr/local 
        --prefix=/usr/local; 
    make -j `nproc`; 
    make install; 
    make clean; 
    ldconfig; 
    yum clean all

FROM scratch AS OS

ARG OS_XZ="centos-7.3.1611-x86_64-docker.tar.xz"
ADD [ "${OS_XZ}", "/" ]

COPY 
    --from=PYTHON /usr/local /usr/local

ENV TZ="Asia/Shanghai" 
    LANG="zh_CN.UTF-8" 
    LC_ALL="C"

RUN set -eu; 
    yum install -y glibc-langpack-zh 
                   zlib-devel 
                   bzip2-devel 
                   ncurses-devel 
                   sqlite-devel 
                   readline-devel 
                   tk-devel 
                   gdbm-devel 
                   xz-devel 
                   libffi-devel 
                   uuid-devel; 
    yum clean all

RUN set -eu; 
    ln -sf /usr/local/bin/python3 /usr/bin/python3; 
    ln -sf /usr/local/bin/python3.10 /usr/bin/python3; 
    ln -sf /usr/local/bin/pip3.10 /usr/bin/pip3; 
    echo "/usr/local/lib">/etc/ld.so.conf.d/ld.local.conf; 
    echo "/usr/local/lib64">>/etc/ld.so.conf.d/ld.local.conf; 
    ldconfig; 
    python3 -m pip install --upgrade pip; 
    python3 -c "import sysconfig; print(sysconfig.get_config_var('CONFIG_ARGS'))"

FROM scratch

ARG OS_XZ="centos-7.3.1611-x86_64-docker.tar.xz"
ADD [ "${OS_XZ}", "/" ]

LABEL 
    AUTHOR="IVAN DU" 
    DATE="2023-03-07" 
    VERSION="1.0.0" 
    PYTHON_VERSION="3.9.16"

ENV TZ="Asia/Shanghai" 
    LANG="zh_CN.UTF-8" 
    LC_ALL="C"

COPY 
    --from=OS /usr /usr
COPY 
    --from=OS /etc /etc

CMD [ "/usr/local/bin/python3" ]

官方镜像915MB,使用本文Dockerfile构建镜像619MB,体积压缩32%。

0 人点赞