testcontainers / testcontainers-java

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

Allow customisation of host.testcontainers.internal #3933

Closed corinapurcarea closed 3 years ago

corinapurcarea commented 3 years ago

I'm testing a docker setup with 2 Redis sentinels, one Redis master and 2 replicas.

The Redis sentinel container is configured with an environment variable that specifies the Redis master host that the sentinel discovers. The sentinel checks if the Redis host can be resolved(and it is cause i've set it to the env var host.testcontainers.internal) but it also returns the Redis master host to whatever client asks for it. The issue is that returned host cannot be resolved outside the docker network.

It would be great if host.testcontainers.internal could be customised. In my case, INTERNAL_HOST_HOSTNAME would be set to "localhost", thus making it accessible from within the container as well as outside the container.

        PortForwardingContainer.INSTANCE.getNetwork().ifPresent(it -> {
            withExtraHost(INTERNAL_HOST_HOSTNAME, it.getIpAddress());
        });
bsideup commented 3 years ago

Hi @corinapurcarea,

host.testcontainers.internal may not be the right API to use here.

Have you considered doing something similar to how KafkaContainer is working? It delays starting the process before the host/port are available, so that it can pass them to the process and it will return the correct values to the consumer: https://github.com/testcontainers/testcontainers-java/blob/8965712f239ae3c449cec579264af5e79265f314/modules/kafka/src/main/java/org/testcontainers/containers/KafkaContainer.java#L103

corinapurcarea commented 3 years ago

Thanks for the quick answer!

Unfortunately, the Kafka setup you mentioned doesn't help because the Redis master host must be resolvable from inside docker network as well from outside it. The application(in my case, the tests) asks sentinel process for the master, but also the sentinel periodically checks if master is available. The application is outside Docker network, the sentinel and master are started with test-containers.

I will leave here 2 solutions for this complicated setup: