host property

String get host

Returns the host address used to reach containers.

Resolution order:

  1. TestcontainersConfiguration.tcHostOverride (explicit override).
  2. SSH Docker host hostname extracted by dockerHostHostname.
  3. Hostname from a tcp://, http://, or https:// DOCKER_HOST URL. On Windows, Docker's named-pipe host localnpipe is normalised to 'localhost'.
  4. Default gateway IP when running inside a container (defaultGatewayIp).
  5. 'localhost' as the final fallback.

Implementation

String get host {
  final override = testcontainersConfig.tcHostOverride;
  if (override != null) {
    return override;
  }

  final sshHost = dockerHostHostname();
  if (sshHost != null) {
    return sshHost;
  }

  final rawHost = dockerHost();
  if (rawHost != null) {
    if (rawHost.startsWith('tcp://') ||
        rawHost.startsWith('http://') ||
        rawHost.startsWith('https://')) {
      final uri = Uri.parse(rawHost);
      final h = uri.host;
      if (h.isEmpty || (h == 'localnpipe' && isWindows())) {
        return 'localhost';
      }
      return h;
    }
  }

  if (insideContainer()) {
    final gw = defaultGatewayIp();
    if (gw != null) {
      return gw;
    }
  }
  return 'localhost';
}