一 背景
随着国内操作系统市场的不断发展,AnolisOS作为一款优秀的国产操作系统,逐渐受到了广大开发者的关注。为了满足Python开发者的需求,本文将介绍如何基于AnolisOS打造Python3.11.0容器基础开发环境,为开发者提供更高效、更稳定的开发体验。
二 Dockerfile编写
Dockerfile全部内容如下:
代码语言:javascript复制FROM
openanolis/anolisos:8.6
ARG PYTHON_VERSION="3.11.0"
# curl -L -C - -o Python.tar.xz https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tar.xz
# curl -L -C - -O https://www.openssl.org/source/openssl-1.1.1s.tar.gz
ADD [ "Python-${PYTHON_VERSION}.tar.xz", "/" ]
ENV LC_ALL="C"
# Deprecated since version 3.11, will be removed in version 3.13: The nis module is deprecated (see PEP 594 for details). dnf install libnsl2-devel
RUN set -eu;
dnf install -y gcc
make
zlib-devel
bzip2-devel
openssl-devel
ncurses-devel
sqlite-devel
readline-devel
tk-devel
gdbm-devel
xz-devel
libffi-devel
uuid-devel
libnsl2-devel
RUN set -eu;
cd Python-${PYTHON_VERSION};
./configure
--enable-optimizations
--with-lto
--enable-shared
--enable-loadable-sqlite-extensions
--enable-option-checking=fatal
--with-system-expat
--with-openssl=/usr
--prefix=/usr/local/python;
make;
make install;
make clean;
dnf clean all
FROM
openanolis/anolisos:8.6
COPY
--from=0 /usr/local/python /usr/local/
ENV TZ="Asia/Shanghai"
LANG="zh_CN.UTF-8"
LC_ALL="C"
RUN set -eu;
dnf install -y glibc-langpack-zh
zlib-devel
bzip2-devel
openssl-devel
ncurses-devel
sqlite-devel
readline-devel
tk-devel
gdbm-devel
xz-devel
libffi-devel
uuid-devel
libnsl2-devel;
dnf clean all
RUN set -eu;
ln -s /usr/local/bin/python3 /usr/bin/python;
ln -s /usr/local/bin/python3.11 /usr/bin/python3;
ln -s /usr/local/bin/pip /usr/bin/pip;
echo "/usr/local/lib">>/etc/ld.so.conf.d/python.conf;
ldconfig;
python -m pip install --upgrade pip;
python -c "import sysconfig; print(sysconfig.get_config_var('CONFIG_ARGS'))"
FROM
openanolis/anolisos:8.6
LABEL
AUTHOR="IVAN DU"
DATE="2022-12-05"
VERSION="1.0.0"
PYTHON_VERSION="3.11.0"
ENV TZ="Asia/Shanghai"
LANG="zh_CN.UTF-8"
LC_ALL="C"
COPY
--from=1 /usr /usr
COPY
--from=1 /etc /etc
CMD [ "/usr/local/bin/python3" ]
本次使用多阶段镜像构建,构建完成后镜像大小763MB。
三 构建镜像
代码语言:javascript复制docker build -t python:anolisos-8.6-3.11.0 .
四 使用镜像
代码语言:javascript复制docker run -it --name python --rm python:anolisos-8.6-3.11.0 bash
五 参考资料
1、Docker镜像构建最佳实践