如何使用docker-compose设置Flask+Gunicorn+Nginx?

wydwbb8l  于 2023-06-21  发布在  Nginx
关注(0)|答案(1)|浏览(152)

我有一个Flask应用程序,有几个API端点:/api1/api2
Flask(flask_app.py):

app = Flask(__name__)
...
@app.route('/api1', methods=['POST'])
def api1():
...
@app.route('/api2', methods=['POST'])
def api2():
...
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Dockerfile:

...
# Copy application code
COPY flaskapp /app
WORKDIR /app

# Set entrypoint
ENTRYPOINT ["gunicorn", "-b", "0.0.0.0:5000", "flask_app:app"]

docker-compose:

services:

  nginx:
    image: nginx:latest
    container_name: nginx_container
    restart: always
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - app

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: flask_container
    restart: always
    ports:
      - "5000:5000"
    volumes:
      - ./flaskapp:/app

nginx.conf:

events { 
    worker_connections 1024; 
}
http {
    client_max_body_size 0;
    server {
        listen 80;
            location / {
                proxy_pass http://flask_container:5000/;
            }
        client_max_body_size 0;
    }
}

我用Gunicorn和Nginx运行它,使用docker-compose up。一切都开始正常。然后我使用Postman在这个URL上发送POST请求来测试它:

http://localhost:5000/api1
http://localhost:80/api1

但是Postman每次都返回500个错误。
http://localhost:80/api1的请求也将此消息写入Nginx控制台:

172.18.0.1 - - [08/Jun/2023:18:26:41 +0000] "POST /api1 HTTP/1.1" 500 265 "-" "PostmanRuntime/7.32.2"

我希望POST请求会转到Flask API。为什么没有?

m4pnthwp

m4pnthwp1#

500错误代码表示在代码执行期间API端发生错误。
对于调试,可以使用python中的app.run(debug=True)logging库。

相关问题