docker-撰写成功但请求时服务器没有响应

docker-compose succeed but server does not response when request

我已经使用 Flask 框架构建了一个 RESTful API 网络服务,Redis 作为主数据库, MongoDB 作为备份存储,Celery 作为任务队列在后台将数据存储到 MongoDB

然后我 docker 使用 docker-compose 调整我的应用程序。这是我的 docker-compose.yml:

version: '3'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
  redis:
    image: "redis:alpine"
    ports:
      - "6379:6379"
  mongo:
    image: "mongo:3.6.5"
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_DATABASE: syncapp

这是我的 Dockerfile:

# base image
FROM python:3.5-alpine
MAINTAINER xhoix <145giakhang@gmail.com>

# copy just the requirements.txt first to leverage Docker cache
# install all dependencies for Python app
COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

# install dependencies in requirements.txt
RUN pip install -r requirements.txt

# copy all content to work directory /app
COPY . /app

# specify the port number the container should expose
EXPOSE 5000

# run the application
CMD ["python", "/app/app.py"]

在 运行 命令 docker-compose up 之后,应用程序服务器、Redis 和 Mongo 服务器就 运行 好了。但是当我使用 Postmancurl 调用 API 时,例如 http://127.0.0.1:5000/sync/api/v1.0/users,应该 return JSON所有用户的格式,但结果是Could not get any response: There was an error connecting to http://127.0.0.1:5000/sync/api/v1.0/users.

我不知道为什么会这样。 感谢您的帮助和建议!

如果操作系统 Linux 则使用:

ifconfig -a

如果操作系统 Windows 则使用:

ipconfig /all

然后检查像docker之类的接口或虚拟化的东西,并使用ipv4或inet

或者只需使用 docker 命令:

docker network inspect bridge

然后在IPAM上使用网关ip

我找到了问题的原因:

调试了一个小时,发现只要把app host改成0.0.0.0就可以了。也许在映射端口时,docker 默认为 0.0.0.0,因为当我 运行 命令 docker-compose ps 时,每个容器的 PORTS 列的格式为 0.0.0.0:<port> -> <port>。我不知道这是不是问题的原因,但我这样做了,问题就解决了