Programming dog programming bull technology sharing platform
Recently, there was a price war between cloud service providers. I spent a small amount of money to rent a cloud server on a certain cloud for a month: the public IP address is 116.85.42.182. I use the IP address 116.85.42.182 as the demonstration below.
After you buy a server, you will get a public IP address that you can use to connect to your server via SSH.
Copy the code
By the way, one cloud creates an account for you called “dc2-user”, and you need to set your own password for root.
Then install docker:
Copy the code
sudo apt-get install http://docker.io
Demonstrate a minimal SanIC-app to deploy. This is the project tree.
Copy the code
.
├ ─ ─ app. Py
├ ─ ─ Dockerfile
└ ─ ─ templates
└ ─ ─ index. HTML
- ` `
1 directory, 3 files
app.py
Copy the code
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'))
The Python code here uses the SANic framework and the Jinja2 board engine, so the tape will need to have both dependencies installed.
Dockerfile
Copy the code
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
In the first line, “FROM Taoliu /gunicorn3”, I couldn’t find a suitable gunicorn base image for Python3, so I made one myself for everyone to use.
RUN PIP install sanic \ && PIP install jinja2 here to install the two dependencies.
CMD gunicorn app: app — bind 0.0.0.0:8080 – worker – class sanic. Worker. GunicornWorker this line, had been so executed commands mirror operation.
templates/index.html
Copy the code
<! 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>
Then upload these files to the server:
Copy the code
SCP - r * [email protected]: ~
Then SSH connected to our server to build our Docker image (this process is a bit long, depending on the network speed).
Copy the code
docker build -t sanic-demo .
Copy the code
docker images
Take a look at the images you currently have
Then run the docker image in the background:
Copy the code
docker run -d --restart=always -p 5000:8080 sanic-demo:latest
Open your browser and type: 116.85.42.182:5000 to see what happens.
Finally, go to the firewall rule of a cloud and add the rule for port 5000.
❈
Alice, The Computer Science actually has a lot in common with Magic.
❈
Recent Hot articles
** How to be a slutty programmer in Python **
** New discovery of Using Python to crawl 100,000 reviews of Eason Chan’s new song “We” **
Introduction and implementation of machine learning algorithm KNN \
Interesting ways to unpack Python \
Using Python to analyze Apple stock price data \
Nginx+ UWSGi to deploy Django applications \
Python natural Language Processing analysis: How to kill dragons \
Python 3.6 enables individual bloggers to crawl their tweets, images, and comments
Click **** to read the original article and become a free member of **** community