【发布时间】:2018-03-01 22:43:41
【问题描述】:
我想要两个 Docker containers,它们在同一个 docker-compose.yaml 文件中定义,以便能够共享 network 并与彼此的暴露端口进行交互。我在 Docker for Mac 上运行所有这些。
为了做到这一点,我设置了几个 docker 容器,它们运行一个小型 Flask 服务器,它可以返回“Hello”或向另一台服务器发出请求(详见下文)。到目前为止,我一直无法让这两个应用相互通信。
到目前为止我已经尝试过:
-
expose相关端口 -
publishing 端口并将它们与主机 1:1 映射 - 对于
flask,同时使用localhost和0.0.0.0作为--host arg -
curl从一个容器到另一个容器(同时使用localhost:<other_container_port>和0.0.0.0:<other_container_port> - 根据文档使用隐式
network - 显式
network定义
以上所有示例都给我一个Connection Refused 错误,所以我觉得我缺少一些关于 Docker 网络的基本知识。
Networking in Compose 文档提到以下内容:
当你运行 docker-compose up 时,会发生以下情况:
...
- 使用 db 的配置创建容器。它 加入名为 db 的网络 myapp_default。
他们的例子似乎让所有单独的服务能够在没有任何网络定义的情况下进行通信,这让我相信我可能也不应该需要定义网络。
下面是我的 docker-compose.yaml 文件——所有文件都可以在this gist找到:
version: '3'
services:
receiver:
build: ./app
# Tried with/without expose
expose:
- 3000
# Tried with/without ports
ports:
- 3000:3000
# Tried with/without 0.0.0.0
command: "--host 0.0.0.0 --port 3000"
# Tried with/without explicit network
networks:
- mine
requester:
build: ./app
expose:
- 4000
ports:
- 4000:4000
# This one's ip is 0.0.0.0, so we can access from host
command: "--host 0.0.0.0 --port 4000"
networks:
- mine
networks:
mine: {}
app.py 文件:
@app.route("/")
def hello():
return "Hello from {}".format(request.host)
@app.route("/request/<int:port>")
def doPing(port):
location = "http://localhost:{}/".format(port)
return requests.get(location, timeout=5).content
【问题讨论】:
标签: python macos docker networking docker-compose