testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

Make the getContainer(String jdbcUrl) in ContainerDatabaseDriver public

Open Reza-Senacor opened this issue 8 years ago • 6 comments

Hi There,

I see java doc of the following method says, it is supposed to be "get an instance of a database container given its JDBC URL" static JdbcDatabaseContainer getContainer(String jdbcUrl) However it is pacakge private one.

In an IT we appoint the ContainerDatabaseDriver as jdbc driver class and use flyway to migrate the test data into a postgres db running as docker container. However I need to call my deployed rest services under a jboss instance, which in turn runs as an additional docker container. I have to pass/replace the database port in the JNDI connection for jboss datasource.

However, the ContainerDatabaseDriver is not subclassing the GenericContainer and there is no way to get the mapped ports for internally created postgres container. The only option I can imagine is to extract the the corresponding JdbcDatabaseContainer and then find out the mapped ports.

Please advice Thanks in advance Reza

Reza-Senacor avatar Jul 09 '18 09:07 Reza-Senacor

Hi @Reza-Senacor,

We could make it public, yes :)

Another option (or as a workaround until we change & release) would be to start a container manually and use .getJdbcUrl() method of it. Something like:

static PostgreSQLContainer postgres = new PostgreSQLContainer();

static {
    postgres.start();
}

// ...

postgres.getJdbcUrl();

bsideup avatar Jul 09 '18 09:07 bsideup

Hi @bsideup,

Thank you for considering my CR. Meanwhile I have assembled a workaround which manually starts the container and works with class instances of the container.

Best Reza

Reza-Senacor avatar Jul 09 '18 14:07 Reza-Senacor

+1

jason-lindholm avatar Jul 03 '23 11:07 jason-lindholm

Is this proposal still considered as an option? In my use case I need this method accessible to retrieve the already running database container. Then I want to connect to this database container from another testcontainer. This approach is to be used for local development, not running unit-tests.

almostvoidcrusade avatar Nov 12 '24 16:11 almostvoidcrusade

In my use case I need this method accessible to retrieve the already running database container.

Started by Testcontainers? are you using Testcontainers JDBC URL? I think the answer for both question are yes but want to confirm.

Btw, for this scenarios you can create a network and connect both containers in the same network. Take a look at this example

eddumelendez avatar Nov 12 '24 17:11 eddumelendez

Hi, @eddumelendez ! Yes, you are correct. Both containers are started and managed by Tescontainers. The only complication compared to provided example is that PostgreSQL container is not created programmatically, rather by using this great feature of JDBC URL support. Since it doesn't support setting of fixed exposed port or configuring networkAliases, I encounter issues trying to configure the second GenericContainer with my custom app. That's why I thought that connecting to the running cached container and retrieving it's mapped port and/or alias is handy.

almostvoidcrusade avatar Nov 12 '24 17:11 almostvoidcrusade