I'm writing some integration tests with Junit5 and Testcontainer for my spring-boot app. I'm experiencing an issue due to random port exposition performed by testcontainer.

Here is the abstract class I use for all my integration tests:

@Slf4j
public class AbstractContainerBaseTest {

    static final ComposeContainer ORACLE_OPIOWNER_CONTANINER;
    static final ComposeContainer ORACLE_FIRMAOWNER_CONTANINER;

    static {
        ORACLE_OPIOWNER_CONTANINER = new ComposeContainer(new File("src/test/resources/docker-compose1.yml"))
                .withExposedService("opiowner", 1521, Wait.forListeningPort())
                .withLocalCompose(true);


        ORACLE_FIRMAOWNER_CONTANINER = new ComposeContainer(new File("src/test/resources/docker-compose2.yml"))
                .withExposedService("firmaowner", 1523, Wait.forListeningPort())
                .withLocalCompose(true);

        Startables.deepStart(ORACLE_OPIOWNER_CONTANINER, ORACLE_FIRMAOWNER_CONTANINER).join();


        System.setProperty("spring.datasource.driver-class-name", "oracle.jdbc.driver.OracleDriver");
        System.setProperty("spring.datasource.url", "jdbc:oracle:thin:" + ORACLE_OPIOWNER_CONTANINER.getServiceHost("opiowner", 1521) + ":" + ORACLE_OPIOWNER_CONTANINER.getServicePort("opiowner", 1521));
        System.setProperty("spring.datasource.username", "OPIOWNER");
        System.setProperty("spring.datasource.password", "password");

        System.setProperty("spring.second-datasource.url", "jdbc:oracle:thin:" + ORACLE_FIRMAOWNER_CONTANINER.getServiceHost("firmaowner", 1523) + ":" + ORACLE_FIRMAOWNER_CONTANINER.getServicePort("firmaowner", 1523));
        System.setProperty("spring.second-datasource.username", "FIRMAOWNER");
        System.setProperty("spring.second-datasource.password", "password");

    }

}

I start 2 containers from docker-compose.yml files. Here are the docker compose files:

version: "3.8"
services:
  opiowner:
    image: gvenzl/oracle-xe:21-slim
    expose: 
      - "1521"
    environment:
      ORACLE_PASSWORD: password
      ORACLE_DATABASE: opiowner
      APP_USER: opiowner
      APP_USER_PASSWORD: password

    command: printenv

and

version: "3.8"
services:
  firmaowner:
    image: gvenzl/oracle-xe:21-slim
    expose:
      - "1523"
    environment:
      ORACLE_PASSWORD: password
      ORACLE_DATABASE: firmaowner
      APP_USER: firmaowner
      APP_USER_PASSWORD: password

    command: printenv

I found this related question and aswer here on StackOverflow: Timed out waiting for container port to open (localhost ports: [32773] should be listening), but although I have added ' expose: -"1521(1523)" ' in my docker files, I am still getting the same error.

Does anyone knows ho to fix this?

Thanks, Saverio

0

There are 0 best solutions below