在docker容器上运行时无法连接到flask

时间:2019-02-19 23:40:10

标签: python docker flask

我正在尝试将一个简单的flask应用程序移至docker,但是我无法弄清楚为什么无法从浏览器访问该应用程序。

docker-compose文件

version: '3'
services:
  web:
    build: .
    ports:
     - "5000:5000"
    volumes:
      - .:/app
    environment:
      FLASK_APP: api
      FLASK_ENV: development

  redis:
    image: "redis:alpine"

dockerfile

FROM python:3.7

ADD . /app
WORKDIR /app

RUN pip install -r requirements.txt
CMD ["flask", "run"]

__ init__py:

def create_app(test_config=None):
  app = Flask(__name__, instance_relative_config=True)
  ...

  return app

docker-compose up --build

的结果
web_1    |  * Serving Flask app "api" (lazy loading)
web_1    |  * Environment: development
web_1    |  * Debug mode: on
web_1    |  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
web_1    |  * Restarting with stat
web_1    |  * Debugger is active!
web_1    |  * Debugger PIN: 277-205-758

但是当我进入浏览器时:

The connection was reset

enter image description here

任何想法为何?我看不到丢失的内容,因为服务器正在运行,但是它确实会立即重置连接...

编辑:

  • 我确实尝试使用容器IP地址通过localhost 0.0.0.0访问它

1 个答案:

答案 0 :(得分:1)

尝试

import subprocess
subprocess.run(
    ["netsh", "interface", "ip", "set", "dns", "Ethernet0", "static", "8.8.8.8"])