英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论