当前位置: 首页 > 面试题库 >

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

汪志业
2023-03-14
问题内容

容器内部的应用程序无法从外部访问,即如果我执行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的开发服务器上进行了以下更改

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

它没有 与开发服务器一起工作的容器的终端映像

我会很感激一个解决方案,该解决方案不涉及此处提到的内容,例如nginx,supervisor等。SOmeone请helllppp meeeeee
......


问题答案:

127.0.0.1表示容器内部与外部容器不同。使用0.0.0.0:5000代替。



 类似资料:
  • 程序一直工作得很棒,但我不知道我的代码哪里出了问题,因为我的类都没有出现在堆栈跟踪中。

  • 我尝试按照stackoverflow上的这个过程集成引导程序(我的rails版本是3.2.17,这是一个区别),所以没有gem,只是将引导文件包含在相关的项目目录中。 然后创建了一个HTML页面,并将其放在project public目录中。 这是代码: 但是,当我加载页面并检查它(它加载)时,我可以看到GETs产生以下错误:

  • 遵循此官方教程,创建一个干净的项目https://developers.google.com/ar/develop/unity/quickstart-android对于在Android中构建和运行ARCore应用程序,示例场景HelloAR尚未启动。 游戏视图中的即时预览也不起作用,显示的是白色屏幕而不是相机渲染。 我的Unity版本是2019.1.10f1,ARCore SDK版本是1.10.0

  • 预先列入登记表工作完善。注册后,它甚至显示我已登录。但当我尝试使用我在注册表中输入的相同凭证登录时。它不允许我登录。我认为问题出在中间件上,因为当我输入错误的用户名/密码时,会显示错误,即无效凭据。但不是有效的,它只是重定向到登录页面没有任何错误。我使用的是laravel 5.0,所有文件都是由laravel提供的

  • 我是maven和Struts2的新手。当我尝试运行我的程序时,会出现以下错误:

  • 我正在尝试运行一个简单的Spring Boot应用程序。它只有web作为依赖,当我运行命令clean install,它成功构建,但是在spring-boot运行时它失败了,我不知道它是否是依赖冲突。我还运行了mvn dependency:tree命令,但是我没有发现任何冲突。这是mvn dependency的输出:tree