Skip to content
Merged
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
30 changes: 23 additions & 7 deletions testcontainers/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,33 @@ def __del__(self):
pass

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
# 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():
return self.get_docker_client().bridge_ip(self._container.id)
return "localhost"
# 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 get_exposed_port(self, port) -> str:
mapped_port = self.get_docker_client().port(self._container.id, port)
if inside_container():
return port
else:
return self.get_docker_client().port(self._container.id, port)
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 with_command(self, command: str) -> 'DockerContainer':
self._command = command
Expand Down
28 changes: 28 additions & 0 deletions testcontainers/core/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import urllib
import docker
from docker.models.containers import Container
from testcontainers.core.utils import inside_container
from testcontainers.core.utils import default_gateway_ip


class DockerClient(object):
Expand Down Expand Up @@ -42,3 +46,27 @@ def port(self, container_id, port):
def bridge_ip(self, container_id):
container = self.client.api.containers(filters={'id': container_id})[0]
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']

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"
19 changes: 19 additions & 0 deletions testcontainers/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import subprocess

LINUX = "linux"
MAC = "mac"
Expand Down Expand Up @@ -35,3 +36,21 @@ def inside_container():
https://github.com/docker/docker/blob/a9fa38b1edf30b23cae3eade0be48b3d4b1de14b/daemon/initlayer/setup_unix.go#L25
"""
return os.path.exists('/.dockerenv')


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

https://github.com/testcontainers/testcontainers-java/blob/3ad8d80e2484864e554744a4800a81f6b7982168/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java#L27
"""
cmd = ["sh", "-c", "ip route|awk '/default/ { print $3 }'"]
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
ip_address = process.communicate()[0]
if ip_address and process.returncode == 0:
return ip_address.decode('utf-8').strip().strip('\n')
except subprocess.SubprocessError:
return None
6 changes: 3 additions & 3 deletions tests/test_docker_compose.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from testcontainers.compose import DockerCompose
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.exceptions import NoSuchPortExposed
from testcontainers.core.utils import inside_container


def test_can_spawn_service_via_compose():
Expand All @@ -29,5 +29,5 @@ def test_can_throw_exception_if_no_port_exposed():

def test_compose_wait_for_container_ready():
with DockerCompose("tests") as compose:
host = "host.docker.internal" if inside_container() else "localhost"
compose.wait_for("http://%s:4444/wd/hub" % host)
docker = DockerClient()
compose.wait_for("http://%s:4444/wd/hub" % docker.host())
7 changes: 2 additions & 5 deletions tests/test_new_docker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from testcontainers import mysql

from testcontainers.core.generic import GenericContainer
from testcontainers.core.utils import inside_container
from importlib import reload


Expand All @@ -31,10 +30,8 @@ def test_docker_env_variables():
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'
pattern = r'mysql\+pymysql:\/\/demo:test@[\w,.]+:(3306|32785)\/custom_db'
assert re.match(pattern, url)


def test_docker_kargs():
Expand Down