44import time
55
66from .docker_command_failed_error import DockerCommandFailedError
7- from ..util .remove_whitespace import remove_whitespace
87
98
109@dataclass
@@ -26,7 +25,7 @@ def get_exposed_port(self, internal_port: int = 3_000) -> int:
2625 docker_command = [
2726 'docker' ,
2827 'inspect' ,
29- f '--format={{{{ json .NetworkSettings.Ports}} }}' ,
28+ '--format={{json .NetworkSettings.Ports}}' ,
3029 self .id
3130 ]
3231
@@ -43,18 +42,24 @@ def get_exposed_port(self, internal_port: int = 3_000) -> int:
4342 # Parse the JSON output to get the port mapping
4443 try :
4544 port_mappings = json .loads (stdout .decode ('utf-8' ).strip ())
46- port_key = f"{ internal_port } /tcp"
47- if port_key not in port_mappings or not port_mappings [port_key ]:
48- # Wait briefly and try to restart/refresh the container
49- time .sleep (1 )
50- self .restart ()
51- time .sleep (2 ) # Wait for container to be ready
52- # Try one more time after restart
53- return self .get_exposed_port (internal_port )
45+ except json .JSONDecodeError as e :
46+ raise DockerCommandFailedError (
47+ f'Failed to decode JSON: { stdout .decode ("utf-8" )} . Error: { str (e )} ' )
48+
49+ port_key = f"{ internal_port } /tcp"
50+ if port_key not in port_mappings or not port_mappings [port_key ]:
51+ # Wait briefly and try to restart/refresh the container
52+ time .sleep (1 )
53+ self .restart ()
54+ time .sleep (2 ) # Wait for container to be ready
55+ # Try one more time after restart
56+ return self .get_exposed_port (internal_port )
57+
58+ try :
5459 return int (port_mappings [port_key ][0 ]['HostPort' ])
55- except (json . JSONDecodeError , KeyError , IndexError , TypeError ) as e :
60+ except (KeyError , IndexError , TypeError ) as e :
5661 raise DockerCommandFailedError (
57- f'Failed to parse port mapping: { stdout .decode ("utf-8" )} . Error: { str (e )} ' )
62+ f'Failed to parse port mapping: { stdout .decode ("utf-8" )} . Error: { str (e )} ' ) from e
5863
5964 def restart (self ):
6065 """Restart the container"""
0 commit comments