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
  1. SSH [email protected]

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
  1. sudo apt-get install http://docker.io

Demonstrate a minimal SanIC-app to deploy. This is the project tree.

Copy the code
  1. .
  2. ├ ─ ─ app. Py
  3. ├ ─ ─ Dockerfile
  4. └ ─ ─ templates
  5. └ ─ ─ index. HTML
  6. ` `
  7. 1 directory, 3 files

app.py

Copy the code
  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'))

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
  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

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
  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>

Then upload these files to the server:

Copy the code
  1. 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
  1. docker build -t sanic-demo .

Copy the code
  1. docker images

Take a look at the images you currently have

Then run the docker image in the background:

Copy the code
  1. 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