|
5 | 5 | import docker.errors
|
6 | 6 | from docker import version
|
7 | 7 | from docker.types import EndpointConfig
|
8 |
| -from typing_extensions import Self |
| 8 | +from typing_extensions import Self, assert_never |
9 | 9 |
|
| 10 | +from testcontainers.core.config import ConnectionMode |
10 | 11 | from testcontainers.core.config import testcontainers_config as c
|
11 | 12 | from testcontainers.core.docker_client import DockerClient
|
12 | 13 | from testcontainers.core.exceptions import ContainerStartException
|
13 | 14 | from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID
|
14 | 15 | from testcontainers.core.network import Network
|
15 |
| -from testcontainers.core.utils import inside_container, is_arm, setup_logger |
| 16 | +from testcontainers.core.utils import is_arm, setup_logger |
16 | 17 | from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
|
17 | 18 |
|
18 | 19 | if TYPE_CHECKING:
|
@@ -128,33 +129,23 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
128 | 129 | self.stop()
|
129 | 130 |
|
130 | 131 | def get_container_host_ip(self) -> str:
|
131 |
| - # infer from docker host |
132 |
| - host = self.get_docker_client().host() |
133 |
| - |
134 |
| - # # check testcontainers itself runs inside docker container |
135 |
| - # if inside_container() and not os.getenv("DOCKER_HOST") and not host.startswith("http://"): |
136 |
| - # # If newly spawned container's gateway IP address from the docker |
137 |
| - # # "bridge" network is equal to detected host address, we should use |
138 |
| - # # container IP address, otherwise fall back to detected host |
139 |
| - # # address. Even it's inside container, we need to double check, |
140 |
| - # # because docker host might be set to docker:dind, usually in CI/CD environment |
141 |
| - # gateway_ip = self.get_docker_client().gateway_ip(self._container.id) |
142 |
| - |
143 |
| - # if gateway_ip == host: |
144 |
| - # return self.get_docker_client().bridge_ip(self._container.id) |
145 |
| - # return gateway_ip |
146 |
| - return host |
| 132 | + connection_mode: ConnectionMode |
| 133 | + connection_mode = self.get_docker_client().get_connection_mode() |
| 134 | + if connection_mode == ConnectionMode.docker_host: |
| 135 | + return self.get_docker_client().host() |
| 136 | + elif connection_mode == ConnectionMode.gateway_ip: |
| 137 | + return self.get_docker_client().gateway_ip(self._container.id) |
| 138 | + elif connection_mode == ConnectionMode.bridge_ip: |
| 139 | + return self.get_docker_client().bridge_ip(self._container.id) |
| 140 | + else: |
| 141 | + # ensure that we covered all possible connection_modes |
| 142 | + assert_never(connection_mode) |
147 | 143 |
|
148 | 144 | @wait_container_is_ready()
|
149 |
| - def get_exposed_port(self, port: int) -> str: |
150 |
| - mapped_port = self.get_docker_client().port(self._container.id, port) |
151 |
| - if inside_container(): |
152 |
| - gateway_ip = self.get_docker_client().gateway_ip(self._container.id) |
153 |
| - host = self.get_docker_client().host() |
154 |
| - |
155 |
| - if gateway_ip == host: |
156 |
| - return port |
157 |
| - return mapped_port |
| 145 | + def get_exposed_port(self, port: int) -> int: |
| 146 | + if self.get_docker_client().get_connection_mode().use_mapped_port: |
| 147 | + return self.get_docker_client().port(self._container.id, port) |
| 148 | + return port |
158 | 149 |
|
159 | 150 | def with_command(self, command: str) -> Self:
|
160 | 151 | self._command = command
|
|
0 commit comments