Skip to content

Commit be687d2

Browse files
container: added 'ip_address' property to container (#141)
1 parent 47e9b79 commit be687d2

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

bugzoo/container.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import time
99

1010
from tempfile import NamedTemporaryFile
11-
from typing import List, Iterator, Dict, Optional
11+
from typing import List, Iterator, Dict, Optional, Union
1212
from timeit import default_timer as timer
13+
from ipaddress import IPv4Address, IPv6Address
1314

1415
from bugzoo.core import Language
1516
from bugzoo.cmd import ExecResponse, PendingExecResponse
@@ -118,6 +119,27 @@ def container(self):
118119
"""
119120
return self.__container
120121

122+
@property
123+
def ip_address(self,
124+
raise_error: bool = False
125+
) -> Optional[Union[IPv4Address, IPv6Address]]:
126+
"""
127+
The IP address used by this container, or None if no IP address.
128+
"""
129+
# TODO: refactor!
130+
api_client = docker.APIClient(base_url='unix://var/run/docker.sock')
131+
container_info = api_client.inspect_container(container.id)
132+
address = container_info['NetworkSettings']['IPAddress']
133+
try:
134+
return IPv4Address(address)
135+
except ipaddress.AddressValueError:
136+
try:
137+
return IPv6Address(address)
138+
except ipaddress.AddressValueError:
139+
if raise_error:
140+
raise
141+
return None
142+
121143
@property
122144
def alive(self) -> bool:
123145
"""

0 commit comments

Comments
 (0)