美文网首页
docker 构建基于 centos7 的 python3.6

docker 构建基于 centos7 的 python3.6

作者: momo1023 | 来源:发表于2019-11-06 14:10 被阅读0次

编写 Dockerfile

# 基于 centos7 构建 python3.6.5 运行环境
FROM centos
MAINTAINER momo # 指定作者信息
RUN set -ex \
    # 预安装所需组件
    && yum install -y wget tar libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gcc-c++ initscripts \
    && wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz \
    && tar -zxvf Python-3.6.5.tgz \
    && cd Python-3.6.5 \
    && ./configure prefix=/usr/local/python3 \
    && make \
    && make install \
    && make clean \
    && rm -rf /Python-3.6.5* \
    && yum install -y epel-release \
    && yum install -y python-pip
# 设置默认为python3
RUN set -ex \
    # 备份旧版本python
    && mv /usr/bin/python /usr/bin/python27 \
    && mv /usr/bin/pip /usr/bin/pip-python27 \
    # 配置默认为python3
    && ln -s /usr/local/python3/bin/python3.6 /usr/bin/python \
    && ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
# 修复因修改python版本导致yum失效问题
RUN set -ex \
    && sed -i "s#/usr/bin/python#/usr/bin/python27#" /usr/bin/yum \
    && sed -i "s#/usr/bin/python#/usr/bin/python27#" /usr/libexec/urlgrabber-ext-down \
    && yum install -y deltarpm
# 基础环境配置
RUN set -ex \
    # 修改系统时区为东八区
    && rm -rf /etc/localtime \
    && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && yum install -y vim \
    # 安装定时任务组件
    && yum -y install cronie
# 支持中文
RUN yum install kde-l10n-Chinese -y
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
# 更新 pip 版本 安装 python 包
RUN pip install --upgrade pip \
    && pip install pyltp -i http://mirrors.aliyun.com/pypi/simple/   --trusted-host mirrors.aliyun.com
ENV LC_ALL zh_CN.UTF-8

update 之后的 dockerfile

# 基于 centos7 构建 python3.6.5 运行环境
FROM centos
MAINTAINER momo # 指定作者信息
RUN set -ex \
    # 预安装所需组件
    && yum install -y wget tar libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gcc-c++ initscripts \
    && wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz \
    && tar -zxvf Python-3.6.5.tgz \
    && cd Python-3.6.5 \
    && ./configure prefix=/usr/local/python3 \
    && make \
    && make install \
    && make clean \
    && rm -rf /Python-3.6.5* \
    && yum install -y epel-release \
    && yum install -y python-pip \
# 设置默认为python3
    # 备份旧版本python
    && mv /usr/bin/python /usr/bin/python27 \
    && mv /usr/bin/pip /usr/bin/pip-python27 \
    # 配置默认为python3
    && ln -s /usr/local/python3/bin/python3.6 /usr/bin/python \
    && ln -s /usr/local/python3/bin/pip3 /usr/bin/pip \
# 修复因修改python版本导致yum失效问题
    && sed -i "s#/usr/bin/python#/usr/bin/python27#" /usr/bin/yum \
    && sed -i "s#/usr/bin/python#/usr/bin/python27#" /usr/libexec/urlgrabber-ext-down \
    && yum install -y deltarpm \
# 基础环境配置
    # 修改系统时区为东八区
    && rm -rf /etc/localtime \
    && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && yum install -y vim \
    # 安装定时任务组件
    && yum -y install cronie \
# 支持中文
    && yum install kde-l10n-Chinese -y \
    && localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 \
# 更新pip版本 安装pip包
    && pip install --upgrade pip \
    && pip install pyltp -i http://mirrors.aliyun.com/pypi/simple/   --trusted-host mirrors.aliyun.com
ENV LC_ALL zh_CN.UTF-8

使用 dockerfile 构建

# dockerfile构建命令
$ docker build -t centos7-python3.6.5 -f dockerfile_path

其他问题

可以手动先下载下来需要的 tar 包,然后修改命令,将 tar 包添加到镜像中,就可以免去上面配置的 wget 了。这样会提高镜像制作速度,修改如下,其实就增加了一个ADD,然后把 wget 和解压去掉了,因为 ADD 命令会自动解压 tar 包:

FROM centos
MAINTAINER momo

#添加文件到镜像
ADD Python-3.6.1.tgz /root/
#安装
RUN set -ex \
    # 预安装所需组件
    && yum install -y wget tar libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gcc-c++ initscripts \
    && cd /root/Python-3.6.1 \
    && ./configure prefix=/usr/local/python3 \
    && make \
    && make install \
    && make clean \
    && rm -rf /root/Python-3.6.1* \
    && yum install -y epel-release \
    && yum install -y python-pip
# 设置默认为python3
RUN set -ex \
    # 备份旧版本python
    && mv /usr/bin/python /usr/bin/python27 \
    && mv /usr/bin/pip /usr/bin/pip-python27 \
    # 配置默认为python3
    && ln -s /usr/local/python3/bin/python3.6 /usr/bin/python \
    && ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
# 修复因修改python版本导致yum失效问题
RUN set -ex \
    && sed -i "s#/usr/bin/python#/usr/bin/python27#" /usr/bin/yum \
    && sed -i "s#/usr/bin/python#/usr/bin/python27#" /usr/libexec/urlgrabber-ext-down \
    && yum install -y deltarpm
# 基础环境配置
RUN set -ex \
    # 修改系统时区为东八区
    && rm -rf /etc/localtime \
    && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && yum install -y vim \
    # 安装定时任务组件
    && yum -y install cronie
# 支持中文
RUN yum install kde-l10n-Chinese -y
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
# 更新pip版本
RUN pip install --upgrade pip
ENV LC_ALL zh_CN.UTF-8

新增一版更新

# 基于 centos7 构建 python3.6.5 运行环境
FROM centos:7
MAINTAINER momo # 指定作者信息
RUN set -ex \
    # 预安装所需组件
    && yum install -y wget tar libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gcc-c++ initscripts \
# 基础环境配置
    # 修改系统时区为东八区
    && rm -rf /etc/localtime \
    && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && yum install -y vim \
# 支持中文
    && yum install kde-l10n-Chinese -y \
    && localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 \
    && yum clean all
RUN set -ex \
    && wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz \
    && tar -zxvf Python-3.6.5.tgz \
    && cd Python-3.6.5 \
    && ./configure prefix=/usr/local/python3 \
    && make \
    && make install \
    && make clean \
    && ln -s /usr/local/python3/bin/python3.6 /usr/local/bin/python3 \
    && ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3 \
    && cd .. \
    && rm -rf /Python-3.6.5* \
    && yum install -y epel-release \
    && yum install -y python-pip \
    && yum clean all
COPY requirements.txt ./
RUN set -ex \
    # 更新 pip 版本 安装 python 包
    && pip3 install --upgrade pip \
    && pip3 install --default-timeout=100 -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt \
    && find / -name "*.py[co]" -exec rm '{}' ';'

ENV LC_ALL zh_CN.UTF-8
CMD ["bin","bash"]

参考:
https://blog.csdn.net/Aeve_imp/article/details/101461488

相关文章

网友评论

      本文标题:docker 构建基于 centos7 的 python3.6

      本文链接:https://www.haomeiwen.com/subject/dmdubctx.html