Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add withAccessToHost option to access to host from containers #2212

Closed
JulienBreux opened this issue Feb 15, 2024 · 5 comments · Fixed by #2471
Closed

[Feature] Add withAccessToHost option to access to host from containers #2212

JulienBreux opened this issue Feb 15, 2024 · 5 comments · Fixed by #2471
Labels
feature New functionality or new behaviors on the existing one

Comments

@JulienBreux
Copy link
Contributor

JulienBreux commented Feb 15, 2024

Problem

In my case, I've a container that needs to access to host in order to function.

Solution

Add the withAccessToHost(bool: access) from Java implem to Go to do that.

Benefit

The main advantage is to allow containers to communicate with the host.
(e.g. Redis hosted on a host or a service under development, etc.)

Alternatives

IDK

Would you like to help contributing this feature?

Yes

@JulienBreux JulienBreux added the feature New functionality or new behaviors on the existing one label Feb 15, 2024
@JulienBreux JulienBreux changed the title [Feature]: Add withAccessToHost option to access to host from containers [Feature] Add withAccessToHost option to access to host from containers Feb 19, 2024
@lbroudoux
Copy link

lbroudoux commented Apr 3, 2024

+1 for this one that is crucial for a helper container (think of a dev service/tooling) that needs to access the application under development that runs locally.

We'll also probably need the equivalent of Testcontainers.exposeHostPorts(localServerPort); to expose a specific port to containers.

@mdelapenya
Copy link
Member

Hi, we have internally discussed this issue, and because it's already available in Java, and handy for testing, we are going to work on it.

It basically needs to start an SSHD container to forward the traffic to it, as seen in https://github.com/testcontainers/testcontainers-java/blob/main/core/src/main/java/org/testcontainers/containers/PortForwardingContainer.java

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Apr 4, 2024

Thanks, in my case, it is helpful as I use testcontainers-k3s in pod callback scenarios to external services.

There is some unreliability using host.docker.internal with certain colima settings. Even when solvable, I've heard DinD can be worse on this point. Looking forward to a bulletproof way to keep tests green.

@Eddie9922
Copy link

Hi, if I understand this correctly, is the goal to export a port from the container to host so that the container can access it? If so, is there a solution for the reverse scenario? Specifically, I've spun up a container and would like to expose ports from the host to the running container so that I can access them from the host.

The problem I'm attempting to address is related to spinning up a Redis cluster using the Testcontainers library. I'm concerned that if I start the Redis cluster with all nodes together and export the ports before the container starts, there might be issues. I'm unsure if Testcontainers will occupy the ports in advance, potentially preventing other processes from using them. My assumption is that there's a small chance the exposed ports might be utilized by other processes or containers, given that the container requires around 20 seconds to start up and we are using shared hosts for spinning up containers.

Therefore, I'm exploring the possibility of spinning up Redis nodes one by one and bootstrapping them into a cluster. However, to accomplish this, we need a method to dynamically expose ports for each newly created Redis node inside the running container.

This is the current constructor to spin up a Reids cluster with all nodes together. addFixedExposedPort doesn't work after the container is running.

public RedisClusterContainer() {
    super(image);

    // Expose the Redis ports, set up memory, wait policy and env variables.
    for (int i = 0; i < NUM_REDIS_PORTS; i++) {
      // Find free port for Redis cluster
      int freePort = TestUtils.getAvailablePort();
      this.withExposedPorts(freePort)
          .addFixedExposedPort(freePort, freePort);
    }
    this.withCreateContainerCmdModifier(it -> Constants.configureMemory(it, memory))
        .waitingFor(Wait.forLogMessage(".*OK\\. Meet duration.*", 1));
  }

Really appreciate if anyone could help here, thanks!

@mdelapenya
Copy link
Member

Hey folks, I'm adding #2471 as draft for feedback 🙏 I'm currently multitasking and would love to check with you if the implementation would be usable from your side. Please read the follow-up notes.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants