无法从另一个容器到Docker容器中运行Flask API

我很感激关于Flask和Docker的许多问题,还有更多关于如何在Docker容器中运行简单Flask API的博客文章。

然而,我有一些麻烦到达一个容器中运行的Flask API,我从另一个容器中设置了主机( 0.0.0.0 )和端口( 5000 )。 有趣的是,如果我启动托pipeAPI的容器,我可以从本地浏览器访问API。

我想要实现的是首先启动一个托pipePostgres数据库的容器,然后托pipe一个Flask API(它使用Postgres作为后端,最后启动一个容器,运行一些针对数据库和API的unit testing。

这是docker-compose.yml文件:

 version: "3" services: app: build: context: ../ dockerfile: docker/app.Dockerfile ports: - "5000:5000" depends_on: - db networks: - todo_backend tests: build: context: ../ dockerfile: docker/tests.Dockerfile ports: - "80:8000" depends_on: - db - app networks: - todo_backend db: image: postgres:9.5 restart: on-failure environment: POSTGRES_PASSWORD: changeme POSTGRES_USER: docker POSTGRES_DB: testdb ports: - "5432" networks: - todo_backend networks: todo_backend: driver: bridge 

该API的Dockerfile是:

 FROM python:3.5-slim COPY requirements.txt /app/requirements.txt RUN apt-get update && \ apt-get install -y wget RUN wget https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -P /usr/bin/ && \ chmod +x /usr/bin/wait-for-it.sh WORKDIR /app RUN pip install -r requirements.txt COPY . /app ENTRYPOINT ["/bin/bash"] CMD ["app/entrypoint.sh"] 

这里是试图访问API的容器的Dockerfile …

 FROM python:3.5-slim COPY requirements.txt /todo/requirements.txt RUN apt-get update && \ apt-get install -y wget RUN wget https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -P /usr/bin/ && \ chmod +x /usr/bin/wait-for-it.sh WORKDIR /todo RUN pip install -r requirements.txt COPY . /todo EXPOSE 80 ENTRYPOINT ["/bin/bash"] CMD ["tests/entrypoint.sh"] 

当我运行testing时,我首先使用docker-compose build图像,然后运行docker-compose up tests

这是我得到的错误消息:

 tests_1 | E requests.exceptions.ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=5000): Max retries exceeded with url: /todo/api/v1.0/tasks (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fc08e2489b0>: Failed to establish a new connection: [Errno 111] Connection refused',)) 

我已经缩短了,因为它的方式更长,但问题似乎是因为tests容器似乎无法到达地址http://0.0.0.0:5000的API。

有一件事我忘记了entrypoint.sh文件的内容。 他们看起来像下面的那个:

 set -e bash wait-for-it.sh --timeout=5 db:5432 python app/app.py 

正如我上面提到的问题是到达API。 我可以在没有问题的情况下对数据库运行所有自动化testing,但出于某种原因似乎无法连接到API。

这是容器和端口的列表:

 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1ea4d2b60d32 docker_app "/bin/bash app/ent..." 20 minutes ago Up 20 minutes 0.0.0.0:5000->5000/tcp docker_app_1 09c21e7354ba postgres:9.5 "docker-entrypoint..." 20 minutes ago Up 20 minutes 0.0.0.0:32816->5432/tcp docker_db_1