I have 2 testcontainers, one which is a dynamodb instance, and the second one a spring-boot one that needs to connect with the first one. While running locally I was just connecting from spring-boot => dynamodb using localhost:8000 , which won't work given dynamic ports.
So I set up:
public static final GenericContainer dynamoDBLocal = new GenericContainer("amazon/dynamodb-local:latest")
.withImagePullPolicy(PullPolicy.defaultPolicy())
.withNetwork(network)
.withNetworkAliases("dynoDBContainer")
.withNetworkMode(network.getId())
.withExposedPorts(8000);
public static final GenericContainer appContainer = new GenericContainer(APP_IMAGE_FUTURE)
.withImagePullPolicy(PullPolicy.defaultPolicy())
.withExposedPorts(8080)
.withNetwork(network)
.withNetworkAliases("appContainer")
.withNetworkMode(network.getId())
.waitingFor(Wait.forHttp("/login").forStatusCode(200))
.dependsOn(dynamoDBLocal);
And in my @BeforeAll
I pass in my dyno port to my appContainer using withEnv
which correctly gets picked up.
However in my app container logs I still see: connection to localhost:something
refused, while when from my local machine I try to connect to the dyno by keeping the container running using a breakpoint I can connect just fine using localhost:something
?
I saw an akin question, but there the answer seemingly no longer used dynamic ports which seemed to go a bit against the dynamic port aspect of testcontainers.
appContainer
should have .withEnv("<dynamo-env-var>", "dynoDBContainer:8000")
When using containers in the same network there is no need to use the exposed random port.
You can find an example here