Testcontainers-java: Docker Compose with Docker Desktop >=3.2.0: HostPortWaitStrategy: Timed out waiting for container port to open

Created on 23 Mar 2021  路  5Comments  路  Source: testcontainers/testcontainers-java

Upgrading to Docker Desktop 3.2.2 our tests using DockerComposeContainer started to fail when using docker-compose with dynamic ports. The error message is something like:

Timed out waiting for container port to open (localhost ports: [0, 55196] should be listening)

Debugging the code you can see that port 0 is reported and then ExternalPortListeningCheck fails because it tries to open a connection to localhost:0

As a workaround we are using this fix for HostPortWaitStrategy:

class HostPortWaitStrategyFixForDockerDesktop322 : HostPortWaitStrategy() {
  override fun getLivenessCheckPorts(): MutableSet<Int> {
    return super.getLivenessCheckPorts().stream()
      .filter { port -> port > 0 }
      .collect(Collectors.toSet())
  }
}

And we use it like this:

container.withExposedService(SERVICE, SERVICE_PORT, HostPortWaitStrategyFixForDockerDesktop322())
typbug

Most helpful comment

hey @rogervinas, this might solve your issue #3948

All 5 comments

hey @rogervinas, this might solve your issue #3948

@rogervinas, @wrover thanks - this bug actually causes Testcontainers' own test suite to fail with Docker for Mac >3.1.0. It looks like a pretty dramatic change was introduced in 3.2.0 - I _think_ this is the cause:

Fixed an issue when binding ports on specific IPs. Note: It may now take a bit of time before the docker inspect command shows the open ports. Fixes docker/for-mac#4541

Because ExternalPortListeningCheck receives the external ports at creation time, only the first inspect results are ever read.

I think #3948 is probably a good quick fix, but I think a general rethink of how and when ports are resolved is required - as hinted at by #3949.

Fixed in #3979

@rnorth I'm on Docker Desktop 3.3.3 and experience this on testcontainers 1.15.3. Should I open a new issue?

@dreis2211 yes please. FWIW again it seems there are numerous issues with Docker for Mac >3.

We're seeing our own test suite taking 30% longer between Docker for a Mac 2.5 and 3.3.3 - the networking performance issues could be affecting your build and causing timeouts.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ayedo picture ayedo  路  3Comments

McKratt picture McKratt  路  4Comments

aniketbhatnagar picture aniketbhatnagar  路  3Comments

lovepoem picture lovepoem  路  3Comments

vmassol picture vmassol  路  3Comments