title: Wait till container starts in testcontainers with GenericContainer for custom PostgreSQL image
body: <p>I'm developing system/integration tests based on Testcontainers solution. It is required for me to use our own database PostgreSQL image with already applied database schema.</p>
  <p>For this reason I'm using Testcontainers GenericContainer.</p>
  <pre><code>private static final GenericContainer postgresDb = new GenericContainer(POSTGRES_IMAGE).withExposedPorts(5432);
  </code></pre>
  <p>I'm developing tests with Spring Boot, so for this reason I've created abstract class which will hold this config for all tests</p>
  <pre><code>@ActiveProfiles(&quot;test&quot;)
  @SpringBootTest
  public abstract class AbstractTests {
  
      private static final DockerImageName POSTGRES_IMAGE = DockerImageName.parse(&quot;docker-name:latest&quot;);
      private static final GenericContainer postgresDb;
  
      static {
          postgresDb = new GenericContainer(POSTGRES_IMAGE)
              .withExposedPorts(5432);
          postgresDb.withStartupTimeout(Duration.ofSeconds(30))
              .start();
      }
  
      @DynamicPropertySource
      static void properties(DynamicPropertyRegistry registry) throws InterruptedException {
          final String s = &quot;jdbc:postgresql://&quot;+ postgresDb.getHost() +&quot;:&quot;+ postgresDb.getMappedPort(5432) + &quot;/test&quot;;
          registry.add(&quot;spring.datasource.url&quot;, () -&gt;s);
      
      }
  
  }
  </code></pre>
  <p>However, the problem is that when when tests are running, container still is starting. This withStartupTimeout(Duration.ofSeconds(30)) doesn't for work some reason.</p>
  <p>When I stop in debug at properties method and give a couple of seconds to start a container all tests run fine.</p>
  <p>When tests fail I see next log:</p>
  <pre><code>org.postgresql.util.PSQLException: FATAL: the database system is starting up
  </code></pre>
  <p>If I put Thread.sleep(..) it also works, it is not preferable solution.</p>
  <p>What is the right solution to wait or right strategy to know that container is ready?</p>