def get_container_host_ip(self) -> str: # if testcontainers itself runs in docker, get the newly spawned # container's IP address from the dockder "bridge" network if inside_container(): return self.get_docker_client().bridge_ip(self._container.id) elif is_windows(): return "localhost" else: return "0.0.0.0"
def get_exposed_port(self, port) -> str: mapped_port = self.get_docker_client().port(self._container.id, port) if inside_container(): gateway_ip = self.get_docker_client().gateway_ip( self._container.id) host = self.get_docker_client().host() if gateway_ip == host: return port return mapped_port
def test_docker_env_variables(): reload(mysql) db = mysql.MySqlContainer() db.with_bind_ports(3306, 32785) with db: url = db.get_connection_url() if inside_container(): assert re.match(r'mysql\+pymysql://demo:test@(\d+\.){3}\d+:3306/custom_db', url) else: assert url == 'mysql+pymysql://demo:test@localhost:32785/custom_db'
def host(self): # https://github.com/testcontainers/testcontainers-go/blob/dd76d1e39c654433a3d80429690d07abcec04424/docker.go#L644 # if os env TC_HOST is set, use it host = os.environ.get('TC_HOST') if host: return host try: url = urllib.parse.urlparse(self.client.api.base_url) except ValueError: return None if 'http' in url.scheme or 'tcp' in url.scheme: return url.hostname if 'unix' in url.scheme or 'npipe' in url.scheme: if inside_container(): ip_address = default_gateway_ip() if ip_address: return ip_address return "localhost"
def get_container_host_ip(self) -> str: # infer from docker host host = self.get_docker_client().host() if not host: return "localhost" # check testcontainers itself runs inside docker container if inside_container(): # If newly spawned container's gateway IP address from the docker # "bridge" network is equal to detected host address, we should use # container IP address, otherwise fall back to detected host # address. Even it's inside container, we need to double check, # because docker host might be set to docker:dind, usually in CI/CD environment gateway_ip = self.get_docker_client().gateway_ip( self._container.id) if gateway_ip == host: return self.get_docker_client().bridge_ip(self._container.id) return host
def test_compose_wait_for_container_ready(): with DockerCompose(TESTS_DIR) as compose: host = "host.docker.internal" if inside_container() else "localhost" compose.wait_for("http://%s:4444/wd/hub" % host)
import json from testcontainers.core import utils from testcontainers.core.container import DockerContainer result = { 'is_linux': utils.is_linux(), 'is_mac': utils.is_mac(), 'is_windows': utils.is_windows(), 'inside_container': utils.inside_container(), 'default_gateway_ip': utils.default_gateway_ip(), } with DockerContainer('alpine:latest') as container: client = container.get_docker_client() result.update({ 'container_host_ip': container.get_container_host_ip(), 'docker_client_gateway_ip': client.gateway_ip(container._container.id), 'docker_client_bridge_ip': client.bridge_ip(container._container.id), 'docker_client_host': client.host(), }) print(json.dumps(result, indent=2))
def get_exposed_port(self, port) -> str: if inside_container(): return port else: return self.get_docker_client().port(self._container.id, port)