testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
8.02k stars 1.65k forks source link

Timed out waiting for container port to open #4602

Closed memekas closed 3 years ago

memekas commented 3 years ago

https://github.com/testcontainers/testcontainers-java/issues/4125 is still reproducible. I can't downgrade my docker because of apple m1 chip. Earlier versions do not support it.

import org.testcontainers.containers.GenericContainer
import org.testcontainers.utility.DockerImageName

open class KGenericContainer(image: String) : GenericContainer<KGenericContainer>(DockerImageName.parse(image))

fun main() {
    val container = KGenericContainer("MY_CONTAINER")
        .withExposedPorts(8081)
    container.start()
}
org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [62362] should be listening)
    at org.testcontainers.containers.wait.strategy.HostPortWaitStrategy.waitUntilReady(HostPortWaitStrategy.java:49)
    at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:35)
    at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:923)
    at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:466)
    at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:329)
    at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:327)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:315)

macbook m1 chip macOS 11.6 docker desktop 4.1.1 docker engine 20.10.8 testcontainers 1.16.0 Kotlin 1.5 JVM 11

kiview commented 3 years ago

Can you please share with which Docker image this happens? Else it is impossible to understand if this is a Testcontainers related issue.

Also, can you please confirm whether this example also fails on your setup? https://github.com/testcontainers/testcontainers-java-repro

rnorth commented 3 years ago

For anybody reading this:

1.16.1 contained a bug which resulted in this same error message, for a different reason. 1.16.2 contains the fix.

@memekas, you've said you're using 1.16.0, so it's likely that you have another issue to the bug in 1.16.1. It would be great if you could upgrade to 1.16.2 anyway!

Anybody else seeing this error message, please ensure you're using 1.16.2 and not 1.16.1.

memekas commented 3 years ago

@kiview https://github.com/memekas/testcontainers/blob/master/src/test/java/org/testcontainers/repro/ReproExampleTest.java

18:39:27.357 [main] INFO  🐳 [openjdk:8-jre-slim] - Container openjdk:8-jre-slim is starting: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925
18:39:27.359 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925,false
18:39:27.359 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.exec.InspectContainerCmdExec - GET: DefaultWebTarget{path=[/containers/8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925/json], queryParams={}}
18:39:27.369 [ducttape-0] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925,false
18:39:27.369 [ducttape-0] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.exec.InspectContainerCmdExec - GET: DefaultWebTarget{path=[/containers/8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925/json], queryParams={}}
18:39:27.380 [testcontainers-wait-0] DEBUG org.testcontainers.containers.ExecInContainerPattern - /friendly_khorana: Running "exec" command: /bin/sh -c while true; do ( true  &&  (grep -i ':0*18eb' /proc/net/tcp* || nc -vz -w 1 localhost 6379 || /bin/bash -c '</dev/tcp/localhost/6379') ) && exit 0 || sleep 0.1; done
18:39:27.380 [testcontainers-wait-0] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925,<null>,true,true,<null>,<null>,<null>,{/bin/sh,-c,while true; do ( true  &&  (grep -i ':0*18eb' /proc/net/tcp* || nc -vz -w 1 localhost 6379 || /bin/bash -c '</dev/tcp/localhost/6379') ) && exit 0 || sleep 0.1; done},<null>,<null>
18:39:27.381 [testcontainers-wait-1] DEBUG org.testcontainers.containers.wait.strategy.HostPortWaitStrategy - External port check passed for [6379] mapped as [65176] in PT0.001785S
18:40:27.387 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925,false
18:40:27.389 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.exec.InspectContainerCmdExec - GET: DefaultWebTarget{path=[/containers/8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925/json], queryParams={}}
18:40:27.416 [main] ERROR 🐳 [openjdk:8-jre-slim] - Could not start container
org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [65176] should be listening)
    at org.testcontainers.containers.wait.strategy.HostPortWaitStrategy.waitUntilReady(HostPortWaitStrategy.java:90)
    at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:51)
    at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:929)
    at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:468)
    at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:331)
    at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:329)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:317)
    at org.testcontainers.repro.ReproExampleTest.demonstration(ReproExampleTest.java:29)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
18:40:27.425 [main] ERROR 🐳 [openjdk:8-jre-slim] - There are no stdout/stderr logs available for the failed container
18:40:27.426 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925,false
18:40:27.426 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.exec.InspectContainerCmdExec - GET: DefaultWebTarget{path=[/containers/8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925/json], queryParams={}}
18:40:27.434 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925,<null>
18:40:27.594 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925,false
18:40:27.594 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.exec.InspectContainerCmdExec - GET: DefaultWebTarget{path=[/containers/8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925/json], queryParams={}}
18:40:27.601 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 8dcade1a431406c014f7289333def056cf64627e8f330e66460ebe479a045925,true,true
18:40:27.630 [main] DEBUG org.testcontainers.utility.ResourceReaper - Removed container and associated volume(s): openjdk:8-jre-slim

I can't use my real container, but it is created from openjdk:8-jre-slim I have updated testcontainers version to 1.16.2

kiview commented 3 years ago

openjdk:8-jre-slim will not open a port on 6379, so it has to fail in this case. Since you are using withExposedPorts(6379) this will automatically configure a HostPortWaitStrategy for the mapped port, which you see fail.

Can you please try with the redis image, as originally contained in the example?

memekas commented 3 years ago

with redis image it works.

So the problem is I need to have image with the exposed ports by Dockerfile? On ubuntu i don't have such problem

kiview commented 3 years ago

Which problem do you not have on Ubuntu? I can assure you that the example you provided would not work like this on Ubuntu as well.

Please check the docs to see how WaitStrategies work in Testconatiners: https://www.testcontainers.org/features/startup_and_waits/

DJ-Glock commented 3 years ago

Hi all I had a similar issue and it looks like I've found the cause once I read this document: https://www.testcontainers.org/features/startup_and_waits/

In my case issue happened because I set .withExposedPorts(80), but container was not going to listen this port. And it gave me similar error with random port. Once I removed .withExposedPorts(80), container was able to start.

@memekas are you sure if port 8081 is correct for your container and it should listen to it?

fun main() {
    val container = KGenericContainer("MY_CONTAINER")
        .withExposedPorts(8081)
    container.start()
}
memekas commented 3 years ago

It looks like my problem is that the program inside the container takes a long time to start, so the check for ports availability does not pass.

I found that it is possible to increase the startup timeout. Is it possible to disable the port openness check? Docker writes that the ports are forwarded, and this is the main thing for me. And whether there is a program on them or not is a different case.

kiview commented 3 years ago

I struggle to understand your Testcontainers usage patterns in this case. However, you can configure Testcontainers' wait strategies according to your needs: https://www.testcontainers.org/features/startup_and_waits/

memekas commented 3 years ago

Yeah. I understood. There are no standard strategies that I need. I have implemented my own strategy. The question is, is it possible to disable checks? I don’t know why to do this. Maybe for quick testing it might be helpful... But yes it sounds useless

hemant-hpe commented 11 months ago

With 1.17.1 still getting the same error : 2023-11-27 15:45:40.239 PST [main] [ERROR] 🐳 [amazon/dynamodb-local:latest] - Could not start container org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [57847] should be listening) at org.testcontainers.containers.wait.strategy.HostPortWaitStrategy.waitUntilReady(HostPortWaitStrategy.java:90) at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:51) at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:929) at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:468) at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:331)