欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker部署python(Dockerfile和docker-compose组合使用)

程序员文章站 2022-07-14 12:06:14
...

docker部署python(Dockerfile和docker-compose组合使用)

1. 开发python接口

from logging.handlers import TimedRotatingFileHandler
import tornado.ioloop
import tornado.web
import json
import logging


def init_log():
    log_file = 'demo.txt'
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    fh = TimedRotatingFileHandler(log_file, when='D', interval=24, backupCount=7)
    datefmt = '%Y-%m-%d %H:%M:%S'
    format_str = '%(message)s'
    formatter = logging.Formatter(format_str, datefmt)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    return logger


class MainHandler(tornado.web.RequestHandler):

    def post(self):
        print("111111")
        body = self.request.body
        bodyRequest = json.loads(body)
        print(bodyRequest)
        body = {"mess": "MainHandler success"}
        self.write(json.dumps(body, ensure_ascii=False))


class MainHandler2(tornado.web.RequestHandler):

    def post(self):
        print("2222222")
        body = self.request.body
        bodyRequest = json.loads(body)
        print(bodyRequest)
        body = {"mess": "MainHandler2 success"}
        self.write(json.dumps(body, ensure_ascii=False))


class MainHandler3(tornado.web.RequestHandler):

    def post(self):
        print("3333333")
        body = self.request.body
        bodyRequest = json.loads(body)
        print(bodyRequest)
        body = {"mess": "MainHandler3 success"}
        self.write(json.dumps(body, ensure_ascii=False))


if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/yicall/api/119423976/sessionCallBack", MainHandler),
        (r"/yicall/api/119423976/taskCallBack", MainHandler2),
        (r"/yicall/api/119423976/sessionQcCallBack", MainHandler3),
    ])
    application.listen(8888)
    tornado.ioloop.IOLoop.current().start()

2.生成requirements.txt文件

	pip freeze > requirements.txt

3. 创建Dockerfile文件

FROM python:3

ADD . /

RUN pip install -r requirements.txt

CMD [ "python", "./demo.py" ]

4. 创建docker-compose.yml文件

version: '3'
services:
  demo:
    image: python-interface:latest
    build: .
    ports:
      - 8888:8888
    volumes:
      - .:/code
    restart: always

5. image介绍

image: python-interface:latest 是 docker build -t python-interface . 生成的

image :可以通过配置build . 生成镜像

build . : 加载的是当前目录的Dockerfile文件

相关标签: docker