【问题标题】:Unable to run Flask App using Docker in Windows-10无法在 Windows-10 中使用 Docker 运行 Flask App
【发布时间】:2021-06-20 19:09:26
【问题描述】:

我已经安装并配置了 docker(根据文档),我正在尝试使用 tiangolo/uwsgi-nginx-flask:python3.8 构建一个烧瓶应用程序。我已经构建了一个 hello-world 应用程序,并通过运行 python manage.py 在本地对其进行了测试,并且该应用程序运行成功。 Link to full Code-File.

我的docker版本和安装如下:

Dockerfile:

FROM tiangolo/uwsgi-nginx-flask:python3.8

ENV INSTALL_PATH /usr/src/helloworld
RUN mkdir -p $INSTALL_PATH

# install net-tools
RUN apt-get update \
  && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    net-tools \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# set working directory
WORKDIR $INSTALL_PATH

# setup flask environment
# install all requirements
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

# copy all files and folder to docker
COPY . .

# run the application in docker environment
CMD [ "python", "./manage.py" ]

我使用docker build --tag hello-world:test . 构建了应用程序,并将应用程序运行为:docker run -d -p 5000:5000 hello-world:test 成功。

但是,我无法在 localhost:50000.0.0.0:5000 或任何其他端口打开应用程序。应用程序正在运行,我可以从 CLI 中看到它:

但是,从浏览器无法访问该页面:

question 提到要检查 IP 地址:

docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" hungry_engelbart
>> <no value>

this link 找到了另一个解决方案,但docker-machine 目前已被弃用。

我是 docker 新手,但我尝试在 this tutorial 之后运行相同的东西,但遇到了类似的问题。

【问题讨论】:

  • 您是否尝试过将地址显式设置为0.0.0.0,就像this answer 中的一样?您将进行此更改 here 并将 localhost 替换为 0.0.0.0
  • 是的,localhost:50000.0.0.0:5000 都试过了,得到了相同的响应。

标签: python windows docker flask


【解决方案1】:

最后,我能够解决这个问题。我必须在 Windows 防火墙 > 高级设置 > 入站规则 > 新入站规则下配置一个新的inbound rules。创建一个允许本地 IP 地址范围的新规则,在我的例子中是 198.168.0.1:198.168.0.100。最后,您需要在0.0.0.0 上运行应用程序,正如cmets 中@tentative 所指出的那样。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2017-06-25
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多