Skip to content

Commit 7bc6ec5

Browse files
committed
Fix testcontainers#141 - getting ip addresses on custom networks
Improved tests
1 parent 6801e14 commit 7bc6ec5

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎core/testcontainers/core/docker_client.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,25 @@ def bridge_ip(self, container_id: str) -> str:
7979
Get the bridge ip address for a container.
8080
"""
8181
container = self.get_container(container_id)
82-
return container['NetworkSettings']['Networks']['bridge']['IPAddress']
82+
network_name = self.network_name(container_id)
83+
return container['NetworkSettings']['Networks'][network_name]['IPAddress']
84+
85+
def network_name(self, container_id: str) -> str:
86+
"""
87+
Get the name of the network this container runs on
88+
"""
89+
container = self.get_container(container_id)
90+
name = container['HostConfig']['NetworkMode']
91+
if name == 'default':
92+
return 'bridge'
93+
return name
8394

8495
def gateway_ip(self, container_id: str) -> str:
8596
"""
8697
Get the gateway ip address for a container.
8798
"""
8899
container = self.get_container(container_id)
89-
return container['NetworkSettings']['Networks']['bridge']['Gateway']
100+
return container['NetworkSettings']['Networks'][network_name]['Gateway']
90101

91102
def host(self) -> str:
92103
"""

‎core/tests/test_docker_in_docker.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pytest
21
import time
32
import socket
43
from testcontainers.core.container import DockerContainer
@@ -9,8 +8,7 @@
98
def _wait_for_dind_return_ip(client, dind):
109
# get ip address for DOCKER_HOST
1110
# avoiding DockerContainer class here to prevent code changes affecting the test
12-
specs = client.get_container(dind.id)
13-
docker_host_ip = specs['NetworkSettings']['Networks']['bridge']['IPAddress']
11+
docker_host_ip = client.bridge_ip(dind.id)
1412
# Wait for startup
1513
timeout = 10
1614
start_wait = time.perf_counter()
@@ -57,7 +55,10 @@ def test_wait_for_logs_docker_in_docker():
5755

5856
def test_dind_inherits_network():
5957
client = DockerClient()
60-
custom_network = client.client.networks.create("custom_network", driver="bridge")
58+
try:
59+
custom_network = client.client.networks.create("custom_network", driver="bridge", check_duplicate=True)
60+
except:
61+
custom_network = client.client.networks.list(names=["custom_network"])[0]
6162
dind = client.run(
6263
image="docker:dind",
6364
command="dockerd -H tcp://0.0.0.0:2375 --tls=false",
@@ -82,7 +83,7 @@ def test_dind_inherits_network():
8283
}) as container:
8384
assert container.get_container_host_ip() == docker_host_ip
8485
# Check the gateways are the same, so they can talk to each other
85-
assert client.gateway_ip(container.id) == client.gateway_ip(dind.id)
86+
assert client.gateway_ip(container.get_wrapped_container().id) == client.gateway_ip(dind.id)
8687
wait_for_logs(container, "Hello from Docker!")
8788
stdout, stderr = container.get_logs()
8889
assert stdout, 'There should be something on stdout'

0 commit comments

Comments
 (0)