基于docker+gunicorn部署sanic项目

364 阅读5分钟

640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1

编程狗 编程大牛技术分享平台 640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1

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

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

  1. ssh dc2-user@116.85.42.182

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

640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1

然后安装docker:

  1. sudo apt-get install http://docker.io

演示一个最小的sanic-app,来部署一下. 这是项目树(目录).

  1. .
  2. ├── app.py
  3. ├── Dockerfile
  4. └── templates
  5.    └── index.html
  6. ``
  7. 1 directory, 3 files

app.py

  1. import os
  2. ``
  3. from sanic import Sanic
  4. from sanic.response import html
  5. from sanic.response import HTTPResponse
  6. from jinja2 import Environment, FileSystemLoader
  7. ``
  8. app = Sanic(__name__)
  9. base_dir = os.path.abspath(os.path.dirname(__file__))
  10. templates_dir = os.path.join(base_dir, 'templates')
  11. jinja_env = Environment(loader=FileSystemLoader(templates_dir), autoescape=True)
  12. ``
  13. ``
  14. def render_template(template_name: str, **context) -> str:
  15.    template = jinja_env.get_template(template_name)
  16.    return template.render(**context)
  17. ``
  18. ``
  19. @app.route('/')
  20. async def index(request) -> HTTPResponse:
  21.    return html(render_template('index.html'))

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

Dockerfile

  1. FROM taoliu/gunicorn3
  2. ``
  3. WORKDIR /code
  4. ``
  5. ADD . /code
  6. ``
  7. RUN pip install sanic \
  8.    && pip install jinja2
  9. ``
  10. EXPOSE 8080
  11. ``
  12. 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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.    <meta charset="UTF-8">
  5.    <title>ltoddy's home</title>
  6.    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10.    <div class="page-header">
  11.        <h1>Welcome</h1>
  12.    </div>
  13. </div>
  14. </body>
  15. </html>

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

  1. scp -r * dc2-user@116.85.42.182:~

640?wx_fmt=jpeg

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

  1. docker build -t sanic-demo .

640?wx_fmt=jpeg

  1. docker images

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

640?wx_fmt=jpeg

然后后台运行docker镜像:

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

640?wx_fmt=jpeg

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

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

640?wx_fmt=jpeg

作者 Alice, The computer science actually has a lot in common with magic.

最近热门文章

**如何用Python做一个骚气的程序员
**

**用Python爬取陈奕迅新歌《我们》10万条评论的新发现
**

机器学习算法KNN简介及实现\

Python有趣的解包用法\

用Python分析苹果公司股价数据\

Nginx+uwsgi部署Django应用\

Python自然语言处理分析倚天屠龙记\

Python 3.6实现单博主微博文本、图片及热评爬取


640?wx_fmt=jpeg

点击下方****阅读原文 免费成为****社区会员