Expose host ports per container instead of globally

Open
#1,557 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
54/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
docker, node.js, typescript
Domain
backend, testing

Research direction

Read start and startContainer in packages/testcontainers/src/generic-container/generic-container.ts, then trace host access through packages/testcontainers/src/port-forwarder/port-forwarder.ts and packages/testcontainers/src/test-containers.ts. Check how the request affects reuse hashing and coordinate with #1520 and #1522. Done means a container can opt in with withExposedHostPorts(...ports), unrequested containers stay unaffected, and the existing global call continues to work.

Written by the indexing model from the issue text.

Description

enhancement minor

What happens

TestContainers.exposeHostPorts(...ports) is global and permanent. Once it has been called, every container started afterwards in the process:

  • is connected to the port forwarder's network, in addition to its own
  • gets a host.testcontainers.internal entry in its extra hosts

whether or not it needs to reach the host. A container cannot opt out, and exposure cannot be scoped to one test. The extra network shows up in getNetworkNames().

Expected

Host access is requested per container, for example withExposedHostPorts(...ports). Containers that did not ask are left alone. The global call keeps working.

Where

  • start and startContainer in packages/testcontainers/src/generic-container/generic-container.ts
  • packages/testcontainers/src/port-forwarder/port-forwarder.ts
  • packages/testcontainers/src/test-containers.ts

Notes

  • Java keeps the global Testcontainers.exposeHostPorts(...) and makes containers opt in with withAccessToHost(true). Go's WithHostPortAccess(ports...) is per container.
  • Related: #1188.
  • Changing the global call so that containers no longer get host access by default would be major. Adding the per-container form is minor.

Dependencies

  • Stack on #1520: the host-access wiring is in start(), where it is one of the things that accumulates.
  • Coordinate with #1522: host access changes the create request, so it affects the reuse hash.
Dominant language
TypeScript
Stars
2.7k
Forks
290
Avg merge
4d 1h
Merged PRs (30d)
22

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from testcontainers/testcontainers-node

All issues in testcontainers/testcontainers-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.