code: public final void waitForPort(final String host, final int port, final int timeout, final TimeUnit unit)
              throws IllegalArgumentException {
          Preconditions.checkArgument(host != null, "host must be non-null");
          Preconditions.checkArgument(port > 0, "port must be positive integer");
          Preconditions.checkArgument(unit != null, "unit must be non-null");
  
          final long startTime = System.currentTimeMillis();
          final long timeoutTime = startTime + TimeUnit.MILLISECONDS.convert(timeout, unit);
  
          LOGGER.info("Waiting for docker container at {}:{} for {} {}", host, port, timeout, unit);
          final SocketAddress address = new InetSocketAddress(host, port);
          for (;;) {
              if (isSocketAlive(address, 2000)) {
                  LOGGER.info(
                          "Successfully connected to container at {}:{} after {}ms",
                          host,
                          port,
                          System.currentTimeMillis() - startTime);
                  return;
              }
              try {
                  Thread.sleep(1000);
                  LOGGER.info("Waiting for container at {}:{}", host, port);
                  if (System.currentTimeMillis() > timeoutTime) {
                      LOGGER.error("Failed to connect with container at {}:{}", host, port);
                      throw new IllegalStateException(
                              "Timeout waiting for socket connection to " + host + ":" + port);
                  }
              } catch (final InterruptedException ie) {
                  throw new IllegalStateException(ie);
              }
          }
      }
docs: Wait for the specified port to accept socket connection within a given time frame.
  
  @param host
  target host name
  @param port
  target port number
  @param timeout
  timeout value
  @param unit
  timeout units
  @throws IllegalArgumentException
  if invalid arguments are passed to method