python flask+uwsgi+tengine 容器

本文介绍了如何利用Docker容器技术,结合Python的Flask框架,配合uWSGI和Nginx来搭建RESTful API服务。详细列举了包括Dockerfile在内的配置文件结构,如Nginx配置、uWSGI设置以及Supervisor的配置,旨在创建一个高效稳定的API服务环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近几日需要写resultful API接口并测试,框架用flask所以搭建一个flask app容器
目录结构如下

flask/
├── config
│   ├── nginx.conf
│   ├── pip.conf
│   ├── supervisord.conf
│   ├── supervisor_nginx.conf
│   ├── supervisor_uwsgi.conf
│   └── uwsgi.ini
├── Dockerfile
└── index.py

1.Dockerfile

FROM ubuntu:16.04
MAINTAINER  becivells <becivells@gmail.com>

#becivells/ubuntu:base
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list && \
    apt-get -y update && apt-get install -y  supervisor                       && \
    mkdir -p /var/log/supervisor                                              && \
    mkdir -p /etc/supervisord.d/                                              && \
    mkdir -p /var/run/supervisor/ 
COPY config/supervisord.conf  /etc/supervisord.conf 


#tengine
RUN apt-get install -y zlib1g-dev gcc make libpcre3 libpcre3-dev openssl  libssl-dev wget
#create dir
RUN mkdir -p /etc/supervisord.d/    &&\
    mkdir -p /var/run/supervisor/   &&\
    mkdir -p /var/log/supervisor/   &&\
    mkdir -p /var/tmp/nginx/client/ &&\
    mkdir -p /var/tmp/nginx/proxy/  &&\
    mkdir -p /var/tmp/nginx/fcgi/   &&\
    mkdir -p /var/tmp/nginx/uwsgi/  &&\
    mkdir -p /var/tmp/nginx/scgi/  
#env
ENV TENGINE_VERSION tengine-2.1.0
#download
RUN cd /tmp/ && wget  http://tengine.taobao.org/download/${TENGINE_VERSION}.tar.gz        &&\
    tar -zxvf ${TENGINE_VERSION}.tar.gz -C /tmp/ && chmod -R 777 /tmp/${TENGINE_VERSION}  
#./configure
RUN cd /tmp/${TENGINE_VERSION} && ./configure \
  --prefix=/opt/${TENGINE_VERSION}/ \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre  &&\
      cd /tmp/${TENGINE_VERSION} && make && make install && \
      rm -rf /tmp/* && apt-get autoclean && \
      ln -s /opt/${TENGINE_VERSION}/ /opt/tengine
#config
COPY config/nginx.conf /opt/tengine/conf/nginx.conf
COPY config/supervisor_nginx.conf  /etc/supervisord.d/supervisor_nginx.conf


#flask uwsgi
RUN apt-get install -y python-pip && \
    mkdir ~/.pip
COPY config/pip.conf ~/.pip/pip.conf
#run
RUN pip install flask  && \
    pip install uwsgi
#config
COPY config/uwsgi.ini /etc/uwsgi.ini
COPY config/supervisor_uwsgi.conf  /etc/supervisord.d/supervisor_uwsgi.conf


#web
RUN  mkdir -p /pyweb/   &&\
     pip install requests
COPY index.py /pyweb/index.py

EXPOSE 80 
VOLUME ["/pyweb/"]

CMD ["/usr/bin/supervisord","-c","/etc/supervisord.conf"]

2.config/nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    autoindex on;  
    autoindex_exact_size on;  
    autoindex_localtime on; 
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen 80;
        server_name localhost;
        access_log /var/log/nginx/flask/access.log;
        error_log /var/log/nginx/flask/error.log;
        location / {
                include uwsgi_params;
                uwsgi_pass 0.0.0.0:8090;
                # uwsgi_pass unix:/tmp/helper.sock;
        }
    }


}
daemon off;

3.config/pip.conf

[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple

4.config/supervisor_nginx.conf

[program:nginx]
command=/opt/tengine/sbin/nginx
autostart = true
autorestart=true
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0

5.config/supervisor_uwsgi.conf

[program:uwsgi]
command=uwsgi -i /etc/uwsgi.ini
autostart = true
autorestart=true
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0

6.config/supervisord.conf

[unix_http_server]
file=/var/run/supervisor/supervisor.sock   

[supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervisord.log 
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/var/run/supervisord.pid

[supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock 

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 

[include]
files = supervisord.d/*.conf

7.config/uwsgi.ini

[uwsgi]
socket = 0.0.0.0:8090
# website dir
chdir = /pyweb/
plugin = python
# python name
wsgi-file = index.py
callable = app
processes = 4 
threads = 2

8.index.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-10-24 17:56:11
# @Author  : Wenzhi Lee (becivells@gmail.com)
# @Version : V0.0.1

from flask import Flask

app = Flask(__name__)
@app.route('/')
def index():
    return "This is a flask app for docker"

if __name__ == '__main__':
        app.run()
docker run -d 80:80 -v /py/pyweb/:/pyweb/ flask
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值