【发布时间】:2021-07-08 02:09:27
【问题描述】:
我有两个 Docker 容器。容器一被命名为5extraextranodes_plc1_1,并在名为test_net 的桥接网络上使用docker-compose 运行。
容器 2 在 docker-compose 之外构建和运行。在容器 2 中,有一个 Python3 脚本。在这个 Python3 脚本中,我希望能够查找 5extraextranodes_plc1_1 容器的 IP 地址。
我最初尝试在 Python 代码中使用 subprocess.checkout() 来运行 docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 5extraextranodes_plc1_1 命令。但这给了我一个错误 FileNotFoundError: [Errno 2] No such file or directory: 'docker'.
所以现在我尝试在我的 Python 3.6 脚本中使用 Docker SDK 来执行此操作。我正在运行以下代码:
client = docker.from_env()
container = client.containers.list(filters={"name": "5extraextranodes_plc1_1"})
ip_add = container.attrs['NetworkSettings']['IPAddress']
不幸的是,这段代码给了我错误:docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory')) em> 声明client = docker.from_env()。
如何从同一网络上的容器 2 中查找 5extraextranodes_plc1_1 容器的 IP 地址?
注意:我目前在网络 test_net(与容器 1 相同的网络)上运行容器 2。我也试过在网络host 模式下运行它。两者都失败了。
【问题讨论】:
-
获取
5extraextranodes_plc1_1ip 地址的最简单方法是将新容器附加到同一网络,然后使用DNS。
标签: python-3.x docker docker-compose subprocess docker-networking