Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions testcontainers/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,23 @@ def __del__(self):

def get_container_host_ip(self) -> str:
# infer from docker host
host = self.get_docker_client().host()
host = self.get_docker_client().host(self._container.id)
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 gateway_ip
return host

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()
bridge_ip = self.get_docker_client().bridge_ip(self._container.id)
host = self.get_docker_client().host(self._container.id)

if gateway_ip == host:
# use exposed port if host detection returns bridge ip
if bridge_ip == host:
return port

return mapped_port

def with_command(self, command: str) -> 'DockerContainer':
Expand Down
36 changes: 28 additions & 8 deletions testcontainers/core/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import docker
from docker.models.containers import Container
from testcontainers.core.utils import inside_container
from testcontainers.core.utils import docker_internal_host
from testcontainers.core.utils import default_gateway_ip


Expand Down Expand Up @@ -48,25 +49,44 @@ def bridge_ip(self, container_id):
return container['NetworkSettings']['Networks']['bridge']['IPAddress']

def gateway_ip(self, container_id):
container = self.client.api.containers(filters={'id': container_id})[0]
return container['NetworkSettings']['Networks']['bridge']['Gateway']
try:
container = self.client.api.containers(filters={'id': container_id})[0]
return container['NetworkSettings']['Networks']['bridge']['Gateway']
except Exception:
return default_gateway_ip()

def resolve_host(self, container_id, default="localhost"):
# get address for host.docker.internal if supports
host = docker_internal_host()

def host(self):
try:
if not host:
return self.gateway_ip(container_id)
if host == self.gateway_ip(container_id):
return self.bridge_ip(container_id)
return host
except Exception:
return default

def host(self, container_id):
# 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:
# check testcontainers itself runs inside docker container
if url.hostname == "localhost" and inside_container():
return self.resolve_host(container_id)
return url.hostname
if 'unix' in url.scheme or 'npipe' in url.scheme:
# check testcontainers itself runs inside docker container
if inside_container():
ip_address = default_gateway_ip()
if ip_address:
return ip_address
return "localhost"
return self.resolve_host(container_id)
return "localhost"
14 changes: 14 additions & 0 deletions testcontainers/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import subprocess
import logging
import socket

LINUX = "linux"
MAC = "mac"
Expand Down Expand Up @@ -48,6 +49,19 @@ def inside_container():
return os.path.exists('/.dockerenv')


def docker_internal_host():
"""
Returns resolved ip address if docker host supports host.docker.internal

https://github.com/moby/moby/pull/40007
https://github.com/moby/libnetwork/pull/2348
"""
try:
return socket.gethostbyname('host.docker.internal')
except socket.gaierror:
return None


def default_gateway_ip():
"""
Returns gateway IP address of the host that testcontainer process is
Expand Down