Docker Image与uWSGI / Gunicorn + Nginx for CentOS中的Flask应用程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker Image与uWSGI / Gunicorn + Nginx for CentOS中的Flask应用程序相关的知识,希望对你有一定的参考价值。

我一直在网上搜索任何与uWSGI或Gunicorn和nginx一起使用Docker图像的例子,以便在CentOS 7环境中为Flask应用程序提供服务。我找到的最接近的是this,它基于Ubuntu。如何重新编写此Dockerfile以使用CentOS 7而不是Ubuntu:

FROM ubuntu:14.04
MAINTAINER Phillip Bailey <phillip@bailey.st>

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y 
    python-pip python-dev uwsgi-plugin-python 
    nginx supervisor
COPY nginx/flask.conf /etc/nginx/sites-available/
COPY supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY app /var/www/app

RUN mkdir -p /var/log/nginx/app /var/log/uwsgi/app /var/log/supervisor 
    && rm /etc/nginx/sites-enabled/default 
    && ln -s /etc/nginx/sites-available/flask.conf /etc/nginx/sites-enabled/flask.conf 
    && echo "daemon off;" >> /etc/nginx/nginx.conf 
    &&  pip install -r /var/www/app/requirements.txt 
    && chown -R www-data:www-data /var/www/app 
    && chown -R www-data:www-data /var/log

CMD ["/usr/bin/supervisord"]
答案

这是最新的centos base,nginx和gunicorn的变种。请注意,此配置仅仅是草图。这样的设置有几个安全问题(例如,烧瓶应用程序以root身份运行),但我认为它概述了基于ubuntu的设置的主要差异。

Dockerfile:

FROM centos:latest
MAINTAINER Deine Mudda<deine@mudda.co.uk>

RUN yum -y update && yum -y install python-setuptools epel-release
RUN yum -y install nginx && 
    easy_install pip supervisor && 
    echo_supervisord_conf > /etc/supervisord.conf
COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY nginx/flask.conf /etc/nginx/conf.d/
COPY supervisor/supervisord.conf /tmp/supervisord.conf
RUN cat /tmp/supervisord.conf >> /etc/supervisord.conf && 
    rm /tmp/supervisord.conf
COPY app /app
RUN pip install -r /app/requirements.txt

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

nginx.conf(这主要是默认的repo版本,删除了一些centos'古怪):

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
daemon off;


events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;


}

flask.conf:

upstream flask {
    server 127.0.0.1:8080;
}

server {
    listen          80;
    location / {
            proxy_pass      http://flask;
    }

}

supervisord.conf:

[program:flask]
directory=/app
command=gunicorn --bind 0.0.0.0:8080 app:app
autostart=true
autorestart=true

[program:nginx]
command=/usr/sbin/nginx
autostart=true
autorestart=true

以上是关于Docker Image与uWSGI / Gunicorn + Nginx for CentOS中的Flask应用程序的主要内容,如果未能解决你的问题,请参考以下文章

为啥Flask logger在前面使用UWSGI时不登录docker?

用Docker部署Django+uWSGI+Nginx

django+uwsgi+docker+k8s

flask+uwsgi+nginx+docker-compose部署

在 Docker 上使用 Nginx、uWSGI 和 Postgres 配置 Django

text docker tensorflow flask keras uwsgi nginx process