Public Repository

Last pushed: 7 months ago
Short Description
Flask + Gunicorn WSGI + NGINX: all in one!
Full Description

Creating Container

$ docker create --net=host --name=flagunix -it ivanlmj/flask_wsgi_nginx bash

Starting Container

$ docker start flagunix

Attaching STDIN (press ENTER after running the command)

$ docker attach flagunix
root@ilg40:/#

Starting NGINX

$ /etc/init.d/nginx start

Running FLASK with Gunicorn (WSGI)

$ cd /etc/flask/myproject/

/etc/flask/myproject $ gunicorn --bind unix:myproject.sock --workers 4 wsgi &
[1] 42
/etc/flask/myproject $
[2017-05-29 17:37:59 +0000] [42] [INFO] Starting gunicorn 19.7.1
[2017-05-29 17:37:59 +0000] [42] [INFO] Listening at: unix:myproject.sock (42)
[2017-05-29 17:37:59 +0000] [42] [INFO] Using worker: sync
[2017-05-29 17:37:59 +0000] [47] [INFO] Booting worker with pid: 47
[2017-05-29 17:37:59 +0000] [48] [INFO] Booting worker with pid: 48
[2017-05-29 17:37:59 +0000] [49] [INFO] Booting worker with pid: 49
[2017-05-29 17:37:59 +0000] [50] [INFO] Booting worker with pid: 50
^C

Watching HTTP Requests on NGINX access.log

tail -f /var/log/nginx/access.log
127.0.0.1 - - [26/May/2017:04:54:37 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.35.0"
127.0.0.1 - - [26/May/2017:04:58:03 +0000] "GET / HTTP/1.1" 502 181 "-" "curl/7.35.0"
127.0.0.1 - - [26/May/2017:04:58:41 +0000] "GET / HTTP/1.1" 502 181 "-" "curl/7.35.0"
127.0.0.1 - - [26/May/2017:05:12:31 +0000] "GET / HTTP/1.1" 200 19 "-" "curl/7.35.0"
127.0.0.1 - - [29/May/2017:17:38:52 +0000] "GET / HTTP/1.1" 200 19 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
127.0.0.1 - - [29/May/2017:17:38:55 +0000] "GET / HTTP/1.1" 200 19 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
127.0.0.1 - - [29/May/2017:17:39:12 +0000] "GET / HTTP/1.1" 200 19 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"

Testing...

Just have to perform a request on your Web Broser for "http://127.0.0.1".

NGINX is true Web Server which is listening to requests from one of his sites configured on /etc/nginx/sites-available, performing a proxy_pass
to the address configured for "/" location, which is a Socket File created by Gunicorn.

cat /etc/nginx/sites-available/myproject
server {

listen 80;
server_name 127.0.0.1;

    location / {
    include proxy_params;
    proxy_pass http://unix:/etc/flask/myproject/myproject.sock;
    }

}

The import cycle which operates here is a Python module called "wsgi.py" (called by gunicorn), which imports another module inside the same directory, that is the application itself written in Flask (Python).

++++++++++++++++++++++++++++
/etc/flask/myproject $ cat wsgi.py

from myproject import application

if name == "main":
application.run()

+++++++++++++++++++++++++++++
/etc/flask/myproject $ cat myproject.py

from flask import Flask
application = Flask(name)

@application.route("/")
def hello():
return "<h1> Hello!!! </h1>"

if name == "main":
application.run(host='0.0.0.0')

Important Files

  • /etc/nginx/nginx.conf
  • [included on /etc/nginx/nginx.conf] /etc/nginx/sites-enabled/myproject -> /etc/nginx/sites-available/myproject
  • /etc/nginx/sites-available/myproject
  • /etc/flask/myproject/wsgi.py
  • /etc/flask/myproject/myproject.py

OBS.: This container has already installed Virtualenv for your project purposes.

Docker Pull Command
Owner
ivanlmj