dokcer-python环境基础镜像制作
定制自己的python环境基础镜像,便于后续python项目快速打成镜像
参考 https://blog.csdn.net/imiyuer/article/details/106003736
环境
已部署好docker镜像仓库(registry)
centos7主机上已安装 python3、dokcer
制作原始镜像
docker build -f ./Dockerfile -t "centos7-py:v1" .
Dockerfile内容如下(参考https://blog.csdn.net/imiyuer/article/details/106003736):
# 完整功能python3 imiyuer/python:3.6.4-centos
FROM centos:7.5.1804
MAINTAINER imiyuer <imiyuer@qq.com>
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 zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make wget \
&& yum clean all \
&& rm -rf /var/cache/yum \
# 下载安装python3
&& wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz \
&& mkdir -p /usr/local/python3 \
&& tar -zxvf Python-3.6.4.tgz \
&& cd Python-3.6.4 \
&& ./configure --prefix=/usr/local/python3 \
&& 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
可能网络不好时会制作失败,重试几次就好
修改标签并上传到内网仓库
docker tag centos7-python3:v1 192.168.2.132:5000/centos7-python3:v1
docker push 仓库(registry)
#192.168.2.132:5000为内网仓库(registry)地址
在其他主机上使用python环境基础镜像
拉取基础镜像
docker pull 192.168.2.132:5000/centos7-python3:v1
创建python代码test.py(以最简单webpy为例)
import web
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
name = 'World'
return 'Hello, ' + name + '!'
if __name__ == "__main__":
app.run()
创建Dockerfile:
FROM 192.168.2.132:5000/centos7-python:v1
COPY ./test.py ./
RUN pip install web.py
EXPOSE 8080
CMD python3 test.py
制作镜像
docker build -f ./Dockerfile -t "centos7-python-test:v1" .
创建运行容器
docker run -d -p 8080:8080 --name webpy centos7-python-test:v1