首页 > 解决方案 > No NGINX redirection towards a gunicorn dockerized flask app - Nginx + Docker + Flask + Gunicorn

问题描述

I'm trying to understand how to use a Nginx + Docker + Flask + Gunicorn package using several tutorials, an I have difficulties running a minimal product.

I made managed to run a 'hello world' display using two services in the docker-compose file by following this tutorial https://towardsdatascience.com/how-to-deploy-ml-models-using-flask-gunicorn-nginx-docker-9b32055b3d0 (i.e. a flask_app and a nginx service)

My problem is the following : I did not manage to set my HTTPS configuration (I'm trying to use Certbot/Letsencrypt) inside the dockerized nginx service. Therefore I'm trying a not dockerized install+config for nginx but when I remove the dockerized nginx and use a normal installation of nginx directly on my server I get stuck at 'Welcome to nginx'.

Can anyone help me to connect a 'normal' NGINX to a containerized flask app (using gunicorn) ? It's a first try for all of this (webhosting, docker, SSL certificates, NGINX etc.) so after a lot of tries I'm a bit lost :)

My simple app :

Dockerfile

FROM python:3.6.7

WORKDIR usr/src/flask_app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .

app.py

from flask import Flask

server = Flask(__name__)

@server.route('/')
def hello_world():
    return 'hello world!'

wsgi.py

from app import server

if __name__ == "__main__":
    server.run(host='0.0.0.0', port=8000)

requirements.txt

flask
gunicorn

the simplified docker-compose.yml, having the nginx service removed (this is the key difference with the working result from the above tuto)

version: '3'

services:
  flask_app:
    container_name: flask_app
    restart: always
    build: ./flask_app
    ports:
      - "8000:8000"
    command: gunicorn -w 1 -b 0.0.0.0:8000 wsgi:server
  

And finally the nginx configuration, that was previously containerized and used by the nginx service of docker-compose which is now direclty on my serveur

/etc/nginx/conf.d/project.conf

server {

    listen 80;
    server_name my_IP_address;

    location / {
        proxy_pass http://0.0.0.0:8000;

        # Do not change this
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /static {
        rewrite ^/static(.*) /$1 break;
        root /static;
    }
}

The main idea being able to use certbot easily in that nginx conf afterwards since I did not managed to do it within the container.

I tried several things :

My understanding of how the above should work is :

flask_app    | [2021-05-19 10:51:19 +0000] [1] [INFO] Starting gunicorn 20.1.0
flask_app    | [2021-05-19 10:51:19 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
flask_app    | [2021-05-19 10:51:19 +0000] [1] [INFO] Using worker: sync
flask_app    | [2021-05-19 10:51:19 +0000] [8] [INFO] Booting worker with pid: 8

It's also my first call here, so I hope I did it well. Thanks for reading :)

PS : In case this is useful : since I'm trying to setup a 'production' environment, this experimentation is not made on my computer but on a distant server (ubuntu) I rented for the test. That may have an impact on the network aspect.

标签: dockernginxflaskdocker-compose

解决方案


推荐阅读