testcontainers / testcontainers-node

Testcontainers is a NodeJS library that supports tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.com
MIT License
1.91k stars 190 forks source link

Error: No host port found for host IP #818

Open theogravity opened 2 months ago

theogravity commented 2 months ago

Expected Behaviour Container should start up.

Actual Behaviour


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: No host port found for host IP
 ❯ resolveHostPortBinding ../../node_modules/.pnpm/testcontainers@10.11.0/node_modules/testcontainers/src/utils/bound-ports.ts:74:9
 ❯ ../../node_modules/.pnpm/testcontainers@10.11.0/node_modules/testcontainers/src/utils/bound-ports.ts:55:46
 ❯ Function.fromInspectResult ../../node_modules/.pnpm/testcontainers@10.11.0/node_modules/testcontainers/src/utils/bound-ports.ts:54:41
 ❯ GenericContainer.startContainer ../../node_modules/.pnpm/testcontainers@10.11.0/node_modules/testcontainers/src/generic-container/generic-container.ts:177:35
 ❯ processTicksAndRejections node:internal/process/task_queues:95:5
 ❯ createNewReaper ../../node_modules/.pnpm/testcontainers@10.11.0/node_modules/testcontainers/src/reaper/reaper.ts:84:28
 ❯ ../../node_modules/.pnpm/testcontainers@10.11.0/node_modules/testcontainers/src/reaper/reaper.ts:38:14
 ❯ withFileLock ../../node_modules/.pnpm/testcontainers@10.11.0/node_modules/testcontainers/src/common/file-lock.ts:14:12
 ❯ getReaper ../../node_modules/.pnpm/testcontainers@10.11.0/node_modules/testcontainers/src/reaper/reaper.ts:29:12

Testcontainer Logs

  testcontainers [TRACE] Container runtime info:
  testcontainers {
  testcontainers   "node": {
  testcontainers     "version": "v20.16.0",
  testcontainers     "architecture": "arm64",
  testcontainers     "platform": "darwin"
  testcontainers   },
  testcontainers   "containerRuntime": {
  testcontainers     "host": "localhost",
  testcontainers     "hostIps": [
  testcontainers       {
  testcontainers         "address": "::1",
  testcontainers         "family": 6
  testcontainers       }
  testcontainers     ],
  testcontainers     "remoteSocketPath": "/var/run/docker.sock",
  testcontainers     "indexServerAddress": "https://index.docker.io/v1/",
  testcontainers     "serverVersion": "27.1.1",
  testcontainers     "operatingSystem": "Docker Desktop",
  testcontainers     "operatingSystemType": "linux",
  testcontainers     "architecture": "aarch64",
  testcontainers     "cpus": 12,
  testcontainers     "memory": 8219254784,
  testcontainers     "runtimes": [
  testcontainers       "io.containerd.runc.v2",
  testcontainers       "runc"
  testcontainers     ]
  testcontainers   },
  testcontainers   "compose": {
  testcontainers     "version": "2.29.1-desktop.1",
  testcontainers     "compatability": "v2"
  testcontainers   }
  testcontainers }

Steps to Reproduce

  const postgresContainer = await new PostgreSqlContainer().start();

  process.env.DB_PORT = postgresContainer.getPort().toString();
  process.env.DB_USER = postgresContainer.getUsername();
  process.env.DB_PASS = postgresContainer.getPassword();
  process.env.DB_NAME = postgresContainer.getDatabase();

Environment Information

This is a co-worker's environment. I've checked their MacOS network settings - nothing seems out of the ordinary. They have an IPv4 setup with IPv6 set to automatic (my own working environment is more extensive, with custom IPv6). Their docker configuration settings are the default for a docker desktop installation. We also tried stopping all existing containers before re-running without any luck.

One thing I noticed between my own working environment and theirs, is the following:

  testcontainers   "containerRuntime": {
  testcontainers     "host": "localhost",
  testcontainers     "hostIps": [
  testcontainers       {
  testcontainers         "address": "::1",
  testcontainers         "family": 6
  testcontainers       },
  testcontainers       {
  testcontainers         "address": "127.0.0.1",
  testcontainers         "family": 4
  testcontainers       }
  testcontainers     ],

Mine has an IPv4 recognized while theirs do not:

  testcontainers   "containerRuntime": {
  testcontainers     "host": "localhost",
  testcontainers     "hostIps": [
  testcontainers       {
  testcontainers         "address": "::1",
  testcontainers         "family": 6
  testcontainers       }
  testcontainers     ],
theogravity commented 2 months ago

We found a workaround:

TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1

kiview commented 2 months ago

Testcontainers for Node should support IPv6 port mappings since a long time.

Interestingly, we just closed this IPv6 related PR in tc-go yesterday (https://github.com/testcontainers/testcontainers-go/pull/2403), because Docker/Moby now supports consistent IPv4/IPv6 port mappings (since Docker 27, see https://github.com/moby/moby/pull/47871).

Do you have an idea what is happening here @cristianrgreco? Given the upstream change in Moby, does it maybe make sense to remove the conditional IPv4/IPv6 logic from tc-node, to simplify the code?