docker - 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 :
- a few other tutorials to catch keys differencies
- changing the server_name ( IP, mywebsite.com; www.mywebsite.com) and the proxy passes (proxy_pass http://127.0.0.1:8000; http://0.0.0.0:8000 , IP, mywebsite.com; www.mywebsite.com and so on)
- using several configuration template files from the web, changing what I though must be changed.
My understanding of how the above should work is :
- NGINX listens to my_IP:80
- redirects to internal 0.0.0.0:8000
- which is gunicorn, running as a wsgi serveur serving the flask app (instead of the flask server, not prod-proof) all within a container (should I use the container's IP for the redirection ?)
- NGINX and the container seem to be running smoothly :
- for NGINX nginx reload , nginx -t etc. showning no errors,
- for the webserver : and docker-compose logs -f
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.
解决方案
推荐阅读
- java - 带有 Maven 的 Oracle JDBC 项目:在 IDE 中运行良好,但在命令行中运行 ClassNotFoundException
- android - Align Alignment.bottomCenter 在 Row 中没有帮助
- arduino - arduino,使用 SD 配置文件中的数据设置以太网和网络
- python - 如何使用 matplotlib ArtistAnimation 绘制直方图或条形动画?
- haskell - haskell中类型的函数应用程序操作数($)?
- matlab - 在 MATLAB 上使用持久变量
- python - 使用 pdfrw 处理后无法移动/删除 PDF
- javascript - JavaScript:每列生成表列
- npm - JFrog 私有注册表无法 npm 安装
- discord - webhook 和角色/权限概念的问题