User-Profile-Image
hankin
  • 5
  • centos7
  • docker
  • mysql
  • PostgreSQL
  • git/gitlab
  • ELK
  • python
    • python-Tornado
    • python-django
  • redis
  • nginx
  • kvm
  • proxmox
  • mongo
  • kubernetes
  • prometheus
  • GlusterFs
  • nfs
  • freeswitch
  • httpd
  • shell脚本
  • linux
  • fastdfs
  • nextcloud
  • openssl
  • openvpn
  • rabbitmq
  • sqlite
  • svn
  • java
  • ubuntu
  • vue2
  • wordpress
  • php
  • IOT物联网
  • 项目
  • 故障处理
  • 树莓派
  • 博客存档
  • 未分类
  • 杂项
  • #1742(无标题)
  • 新视野
  • 分类
    • 项目
    • 树莓派
    • 杂项
    • 未分类
    • 新视野
    • 故障处理
    • 博客存档
    • wordpress
    • vue2
    • ubuntu
    • svn
    • sqlite
    • shell脚本
    • redis
    • rabbitmq
    • python-django
    • python
    • proxmox
    • prometheus
    • PostgreSQL
    • php
    • openvpn
    • openssl
    • nginx
    • nfs
    • nextcloud
    • mysql
    • mongo
    • linux
    • kvm
    • kubernetes
    • java
    • IOT物联网
    • httpd
    • GlusterFs
    • git/gitlab
    • freeswitch
    • fastdfs
    • ELK
    • docker
    • centos7
  • 页面
    • #1742(无标题)
  • 友链
      请到[后台->主题设置->友情链接]中设置。
Help?

Please contact us on our email for need any support

Support
    首页   ›   docker   ›   正文
docker

dokcer-python环境基础镜像制作

2022-10-28 23:33:25
750  0 0

定制自己的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
评论 (0)

点击这里取消回复。

欢迎您 游客  

Copyright © 2025 网站备案号 : 蜀ICP备2022017747号
smarty_hankin 主题. Designed by hankin
主页
页面
  • #1742(无标题)
博主
tang.show
tang.show 管理员
linux、centos、docker 、k8s、mysql等技术相关的总结文档
210 文章 2 评论 200971 浏览
测试
测试