python - Docker Container中的Gunicorn Flask应用程序未暴露

标签 python docker flask kubernetes gunicorn

容器内部的应用程序无法从外部访问,即如果我执行docker容器并执行

curl localhost:5000 

它可以正常工作,但不能在我的计算机上的浏览器上出现错误:无法访问此站点

我的Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set the working directory to /app
WORKDIR /web-engine

# Copy the current directory contents into the container at /app
COPY . /web-engine

# Install Gunicorn3
RUN apt-get update && apt-get install default-libmysqlclient-dev gcc -y

# Install any needed packages specified in requirements.txt
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV username root

# Run app.py when the container launches
CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1


UPON以这种方式执行docker:

sudo docker run -e password=$password -p 5000:5000 $reg/web-engine:ve0.0.2

我得到以下输出:
[2019-09-08 11:53:36 +0000] [6] [INFO] Starting gunicorn 19.9.0
[2019-09-08 11:53:36 +0000] [6] [INFO] Listening at: http://127.0.0.1:5000 (6)
[2019-09-08 11:53:36 +0000] [6] [INFO] Using worker: sync
[2019-09-08 11:53:36 +0000] [9] [INFO] Booting worker with pid: 9
[2019-09-08 11:53:36 +0000] [10] [INFO] Booting worker with pid: 10
[2019-09-08 11:53:36 +0000] [11] [INFO] Booting worker with pid: 11
[2019-09-08 11:53:36 +0000] [12] [INFO] Booting worker with pid: 12

如您所见,我正在将容器的端口5000映射到计算机的端口5000,但是localhost:5000无法正常工作

因此,我尝试了所有相同的事情,但是使用了Flask的开发服务器
在我的dockerfile中进行以下更改



CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1




CMD python3.7 application.py

和它的工作;我转到localhost:5000并看到应用程序正在运行

该应用程序没有任何问题。我想gunicorn服务器出现错误

requirements.txt文件:
Flask
Flask-SQLAlchemy
mysqlclient
gunicorn
bs4
html5lib

请帮帮我

我还尝试了不同形式的gunicorn和docker run命令组合,例如

CMD gunicorn -application:app && sudo docker run -e password=$password -p 5000:8000 $reg/web-engine:ve0.0.2

它没有用
terminal image of container working with development server

我希望能得到一个解决方案,该解决方案不涉及此处提到的内容,例如Nginx,Supervisor等
SOmeone请helllppp meeeeee ......

最佳答案

默认情况下,127.0.0.1表示容器内部与外部容器不同。请改用0.0.0.0:5000

关于python - Docker Container中的Gunicorn Flask应用程序未暴露,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60705576/

相关文章:

javascript - 在 Django View 中解码查询字符串参数

python - Pandas 中的日期范围

python - 将数据访问到字典 python 列表中

linux - Packer、Dockramp 与 Dockerfile

docker - 将当前目录复制到 docker 镜像中

amazon-web-services - 在 ec2 上从 docker 运行 elasticsearch

python - 使用 nosql 排名在 SQLalchemy 中排序和分页

python - Flask 和 SQLAlchemy,应用程序未在实例上注册

python - 如何定制 Flask-Admin 模块的内联模型 View /表单?

python - 为什么在 Pylint 认为不正确的条件值中使用 len(SEQUENCE)?