masterdany88
masterdany88

Reputation: 5341

How to expose port from container to container on testcontainers?

I want to integration test few services running on docker environment. They have to talk each other as they are connected. So I need to somehow expose port between each other. Is this setup possible using test containers library? I did not find any docks on it.

Tested app running locally, which will connect to dockerised servers:

To make it even harder backend one server needs to talk to mysql service. Is this possible using testcontainers?

Upvotes: 2

Views: 3361

Answers (2)

bsideup
bsideup

Reputation: 3063

Testcontainers supports networks, there are a few examples in the tests.

You need to first create a network (via Network.newNetwork()), and then use withNetwork/withNetworkAliases to "connect" your containers into the same network.

Upvotes: 6

leopal
leopal

Reputation: 4959

Generally, when network communication between containers is required, a network should be created at docker level and containers should share it.

Things can be simplified by using docker-compose, which by default creates a network for all services declared in docker-compose file. Then containers can communicate via their service name.

I have never used Testcontainers project but I see in docs that has a docker-compose module.

Quoting from above url:

Testcontainers will spin up a small 'ambassador' container, which will proxy between the Compose-managed containers and ports that are accessible to your tests. This is done using a separate, minimal container that runs socat as a TCP proxy.

There is also an example that you can base on, on docs url.

Upvotes: -1

Related Questions