制作Python环境Docker镜像

编译生成

1
docker build -t 生成的镜像名:版本 .

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
FROM centos:7.3.1611

MAINTAINER runsha.yan <rex_yan@126.com>

# change aliyun yum source
ADD ./CentOS-Base-aliyun.repo /etc/yum.repos.d/CentOS-Base.repo

# add epel source
ADD ./epel-7.repo /etc/yum.repos.d/epel-7.repo

# make cache
RUN yum clean all && yum makecache

# set python version
ENV PYTHON_VERSION 3.6.2
ENV PYTHON_VERSION_SHORT 362

# create directory
RUN mkdir /root/app /root/source /root/www

# set config
ADD ./vimrc /root/.vimrc

# net-tools=ifconfig netstat
RUN yum install -y wget net-tools telnet bzip2 which gcc make zlib zlib-devel openssl openssl-devel

# install pip
RUN wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py && rm -rf get-pip.py

# set pip.conf
RUN mkdir -p /root/.pip
ADD ./pip.conf /root/.pip/pip.conf

# set sys_time
RUN rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# set utf-8
ENV LANG zh_CN.UTF-8
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8

# set default path
WORKDIR /root

# to import bz2
RUN yum install -y bzip2-devel vim

# install python3
ADD ["Python-$PYTHON_VERSION.tgz","."]
WORKDIR /root/Python-$PYTHON_VERSION
#RUN ./configure --prefix=/root/app/python362 --enable-optimizations && \
RUN ./configure --enable-shared --prefix=/root/app/python$PYTHON_VERSION_SHORT --enable-optimizations && \
make && make install

# need cp libpython3.6m.so.1.0 to /usr/lib64
#RUN cp /root/app/python$PYTHON_VERSION_SHORT/lib/libpython3.5m.so.1.0 /usr/lib64
RUN cp /root/app/python$PYTHON_VERSION_SHORT/lib/libpython*.1.0 /usr/lib64

WORKDIR /root
RUN rm -rf /root/Python-$PYTHON_VERSION

# link python to python3, call python3 with python
ADD yum /usr/bin/
RUN chmod +x /usr/bin/yum
ADD urlgrabber-ext-down /usr/libexec/
RUN chmod +x /usr/libexec/urlgrabber-ext-down
WORKDIR /usr/bin/
RUN mv python python.bak && \
mv pip pip.bak
RUN ln -s /root/app/python362/bin/python3 /usr/bin/python
RUN ln -s /root/app/python362/bin/pip3 /usr/bin/pip

WORKDIR /root

RUN pip install --upgrade pip
# install python package
ADD ["requirements.txt", "."]
RUN pip install -r requirements.txt

# install local package
# COPY ["package name", "."]
# RUN pip install -U package name

WORKDIR /root/www
CMD ["/bin/bash"]

示例

https://github.com/rexyan/DockerFile/tree/master/Python