最近云服务提供商在打价格战,我在某云上花了很少的钱租了一个月的云服务器: 公网ip是: 116.85.42.182, 以下我以116.85.42.182这个ip为演示,当你自己在部署的时候请换乘自己的ip地址.
买完服务器之后,你会得到一个公网ip,你可以通过ssh命令连接上你的服务器.
ssh dc2-[email protected].85.42.182
顺便提一句,某云给你创建的账户叫"dc2-user",你需要自己设置root的密码.
然后安装docker:
sudo apt-get install http://docker.io
演示一个最小的sanic-app,来部署一下. 这是项目树(目录).
.├── app.py├── Dockerfile└── templates└── index.html1 directory, 3 files
app.py
import osfrom sanic import Sanicfrom sanic.response import htmlfrom sanic.response import HTTPResponsefrom jinja2 import Environment, FileSystemLoaderapp = 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/gunicorn3WORKDIR /codeADD . /codeRUN pip install sanic \&& pip install jinja2EXPOSE 8080CMD 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 * dc2-[email protected].85.42.182:~
然后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端口的规则.
作者:Alice,The computer science actually has a lot in common with magic.
❈最近热门文章
▼ 点击下方阅读原文,免费成为社区会员