|
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 | 10 | from testcontainers.core.config import testcontainers_config as c |
11 | 11 | from testcontainers.core.docker_client import DockerClient |
12 | 12 | from testcontainers.core.exceptions import ContainerStartException |
13 | 13 | from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID |
14 | 14 | from testcontainers.core.network import Network |
15 | | -from testcontainers.core.utils import inside_container, is_arm, setup_logger |
| 15 | +from testcontainers.core.utils import is_arm, setup_logger |
16 | 16 | from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs |
17 | 17 |
|
18 | 18 | if TYPE_CHECKING: |
@@ -128,33 +128,23 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None: |
128 | 128 | self.stop() |
129 | 129 |
|
130 | 130 | 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 |
| 131 | + connection_mode: c.ConnectionMode |
| 132 | + connection_mode = self.get_docker_client().get_connection_mode() |
| 133 | + if connection_mode == c.ConnectionMode.docker_host: |
| 134 | + return self.get_docker_client().host() |
| 135 | + elif connection_mode == c.ConnectionMode.gateway_ip: |
| 136 | + return self.get_docker_client().gateway_ip(self._container.id) |
| 137 | + elif connection_mode == c.ConnectionMode.bridge_ip: |
| 138 | + return self.get_docker_client().bridge_ip(self._container.id) |
| 139 | + else: |
| 140 | + # ensure that we covered all possible connection_modes |
| 141 | + assert_never(connection_mode) |
147 | 142 |
|
148 | 143 | @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 |
| 144 | + def get_exposed_port(self, port: int) -> int: |
| 145 | + if self.get_docker_client().get_connection_mode().use_mapped_port: |
| 146 | + return self.get_docker_client().port(self._container.id, port) |
| 147 | + return port |
158 | 148 |
|
159 | 149 | def with_command(self, command: str) -> Self: |
160 | 150 | self._command = command |
|
0 commit comments