Flask + Docker => 无连接

问题描述 投票:0回答:1

我在 Flask 中实现了一个基本的 REST API。我想尝试使用 Docker 将其容器化。我对 Docker 完全陌生,但是根据我在各个论坛上了解到的内容,这就是我所设置的。

Dockerfile

FROM python:3.11
WORKDIR     /app
COPY ./requirements.txt /app/requirements.txt
COPY .env /app/.env
COPY . /app
RUN python3 -m pip install -r /app/requirements.txt

EXPOSE 5000
ENTRYPOINT ["python3"]
CMD ["app.py", "--host=0.0.0.0"]

需求.txt

aiohttp==3.8.6
aiohttp-retry==2.8.3
aiosignal==1.3.1
async-timeout==4.0.3
attrs==23.1.0
blinker==1.6.3
certifi==2023.7.22
charset-normalizer==3.3.1
click==8.1.7
distlib==0.3.7
filelock==3.12.4
Flask==2.3.0
Flask-Cors==4.0.0
flask-marshmallow==0.14.0
Flask-MySQL==1.5.2
Flask-MySQLdb==2.0.0
Flask-SQLAlchemy==3.1.1
frozenlist==1.4.0
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
marshmallow-sqlalchemy==0.29.0
multidict==6.0.4
mysqlclient==2.2.0
packaging==23.2
platformdirs==3.11.0
PyJWT==2.8.0
PyMySQL==1.1.0
python-dotenv==1.0.0
requests==2.31.0
six==1.16.0
SQLAlchemy==2.0.22
twilio==8.10.0
typing_extensions==4.8.0
urllib3==2.0.7
virtualenv==20.24.5
Werkzeug==3.0.0
yarl==1.9.2

然后我转到终端,然后运行

$ docker build -t myapp:latest
.

构建成功,我可以看到我的应用程序列在 Docker 桌面应用程序中

然后我就跑了

$ docker run --rm -it -p 8080:5000 myapp:latest
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
/usr/local/lib/python3.11/site-packages/flask_sqlalchemy/model.py:144: SAWarning: This declarative base already contains a class with the same class name and module name as __main__.User, and will be replaced in the string-lookup table.
  super().__init__(name, bases, d, **kwargs)
 * Debugger is active!
 * Debugger PIN: 581-248-767

Docker Desktop 显示 myapp 正在使用中。

到目前为止,一切都很好。但这就是我开始遇到问题的地方。

从主机(通过 Postman)我无法通过以下方式访问端口 8080 上的应用程序:

127.0.0.1:8080/data 

邮递员抛出错误:

Error: read ECONNRESET

我真的不知道该做什么或从这里去哪里,我尝试过的每一个来源都给我带来了与我已经尝试过的略有不同的变化,而且我离让它发挥作用还很远。请帮忙,谢谢。

python docker flask flask-restful
1个回答
0
投票

使用gunicorn或其他WSGI服务器来启动flask应用程序。 https://flask.palletsprojects.com/en/3.0.x/deploying/gunicorn/

命令应该是这样的: /path/to/python/install/or/venv/python -m Gunicorn -b :5000 --access-logfile - --error-logfile - wsgi:app

© www.soinside.com 2019 - 2024. All rights reserved.