V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ltoddy
V2EX  ›  Python

基于 docker+gunicorn 部署 sanic 项目

  •  
  •   ltoddy ·
    ltoddy · 2018-04-15 09:25:58 +08:00 · 3082 次点击
    这是一个创建于 2196 天前的主题,其中的信息可能已经有所发展或是发生改变。

    基于 docker+gunicorn 部署 sanic 项目

    源代码: https://github.com/ltoddy/Python-useful/tree/master/sanic-app

    最近云服务提供商在打价格战,我在滴滴云上花了很少的钱租了一个月的云服务器: 公网 ip 是:116.85.42.182, 以下我以 116.85.42.182 这个 ip 为演示,当你自己在部署的时候请换乘自己的 ip 地址.

    买完服务器之后,你会得到一个公网 ip,你可以通过 ssh 命令连接上你的服务器.

    ssh [email protected]

    顺便提一句,滴滴云给你创建的账户叫"dc2-user",你需要自己设置 root 的密码.

    然后安装 docker:

    sudo apt-get install docker.io

    演示一个最小的 sanic-app,来部署一下.

    这是项目树(目录).

    .
    ├── app.py
    ├── Dockerfile
    └── templates
        └── index.html
    
    1 directory, 3 files
    

    app.py

    import os
    
    from sanic import Sanic
    from sanic.response import html
    from sanic.response import HTTPResponse
    from jinja2 import Environment, FileSystemLoader
    
    app = Sanic(__name__)
    base_dir = os.path.abspath(os.path.dirname(__file__))
    templates_dir = os.path.join(base_dir, 'templates')
    jinja_env = Environment(loader=FileSystemLoader(templates_dir), autoescape=True)
    
    
    def render_template(template_name: str, **context) -> str:
        template = jinja_env.get_template(template_name)
        return template.render(**context)
    
    
    @app.route('/')
    async def index(request) -> HTTPResponse:
        return html(render_template('index.html'))
    

    这里的 python 代码,用到了 sanic 框架和 jinja2 木板引擎,所以带会需要安装这两个依赖.

    Dockerfile

    FROM taoliu/gunicorn3
    
    WORKDIR /code
    
    ADD . /code
    
    RUN pip install sanic \
        && pip install jinja2
    
    EXPOSE 8080
    
    CMD gunicorn app:app --bind 0.0.0.0:8080 --worker-class sanic.worker.GunicornWorker
    

    第一行那里"FROM taoliu/gunicorn3",由于没找到合适的 Python3 的 gunicorn 的基础镜像,所以我自己做了一个,方便所有人使用.

    RUN pip install sanic \ && pip install jinja2 这里,来安装那两个依赖.

    CMD gunicorn app:app --bind 0.0.0.0:8080 --worker-class sanic.worker.GunicornWorker 这行,是镜像运行他所以执行的命令.

    templates/index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ltoddy's home</title>
        <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.css">
    </head>
    <body>
    <div class="container">
        <div class="page-header">
            <h1>Welcome</h1>
        </div>
    </div>
    </body>
    </html>
    

    然后把这些文件传到服务器上:

    scp -r * [email protected]:~

    然后 ssh 连上我们的服务器,去构建我们的 docker 镜像(这个过程有些漫长,具体看网速.)

    docker build -t sanic-demo .

    docker images

    来查看一下当前拥有的镜像

    然后后台运行 docker 镜像:

    docker run -d --restart=always -p 5000:8080 sanic-demo:latest

    这时候打开浏览器输入: 116.85.42.182:5000 来看看效果吧.

    最后说明一点,去滴滴云那里的防火墙规则那里,添加 5000 端口的规则.

    https://img.vim-cn.com/00/b211cca5695de1c1c71ddf48faa4916cf441c6.png

    3 条回复    2018-04-16 10:08:42 +08:00
    Lycnir
        1
    Lycnir  
       2018-04-15 15:15:39 +08:00
    感谢分享!
    1. sanic 中加载 jinja2 模板不用函数速度可以更快。毕竟启动的时候就加载好了
    2. Dockfile 中,能用 COPY 就不要用 ADD。再者因为分层,RUN 命令可以前置于第二行
    ltoddy
        2
    ltoddy  
    OP
       2018-04-15 15:40:03 +08:00
    @Lycnir 你说的第二条我清楚,第一条不是很清楚。
    xuyl
        3
    xuyl  
       2018-04-16 10:08:42 +08:00
    把你自行构建的镜像 taoliu/gunicorn3 相关 Dockerfile 也放到 github 上给人看看吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5626 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 02:48 · PVG 10:48 · LAX 19:48 · JFK 22:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.