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

huangapple go评论58阅读模式
英文:

How to set up Flask+Gunicorn+Nginx using docker-compose?

问题

我有一个带有几个API端点的Flask应用程序:/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 flaskapp /app
WORKDIR /app

# 设置入口点
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;
    }
}

我使用docker-compose up运行它。一切都正常启动。
然后,我使用Postman发送POST请求到以下URL进行测试:

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。为什么它不起作用?

英文:

I have Flask app with couple API endpoints: /api1 and /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;
    }
}

I run it with Gunicorn and Nginx using docker-compose up. Everything starts properly.
Then I test it using Postman sending POST requests on this urls:

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

but Postman returns 500 error every time.

Request to http://localhost:80/api1 also writes this message to Nginx console:

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

I expected that POST requests will go to Flask API. Why it doesn't?

答案1

得分: 0

500错误代码表示在API执行期间发生了错误。

为了进行调试,可以使用app.run(debug=True)或Python中的logging库。

英文:

500 error code meant error was happening on API side during code execution.

For debugging could be used app.run(debug=True) or logging library in python.

huangapple
  • 本文由 发表于 2023年6月9日 02:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定