简体   繁体   中英

Docker Image with uWSGI/Gunicorn + Nginx for Flask apps in CentOS

I've been searching the internet to find any example of Docker image with uWSGI or Gunicorn and Nginx to serve a Flask app in CentOS 7 environment. The closest I found is this and its based on Ubuntu. How can I re-write this Dockerfile to use CentOS 7 instead of 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"]

Here is a variant with latest centos base, nginx and gunicorn. Note that this configuration is merely a sketch. There are several security issues with a setup like this (the flask app runs as root, for example) but i think it outlines the major differences to setup based on 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 (this is largely the default repo-version with some of centos' wackynesses removed):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM