等待容器端口打开超时(本地主机端口:[32773]应该正在侦听)

时间:2019-05-19 13:30:08

标签: java scala docker testcontainers

我正在尝试将https://github.com/testcontainers/testcontainers-scala所固有的https://www.testcontainers.org/用于以下用途:

final class MessageSpec extends BddSpec
  with ForAllTestContainer
  with BeforeAndAfterAll {


  override val container = GenericContainer("sweetsoft/sapmock").configure{ c =>
    c.addExposedPort(8080)
    c.withNetwork(Network.newNetwork())
  }

  override def beforeAll() {
  }


  feature("Process incoming messages") {  

使用命令sbt test运行测试时,遇到以下异常:

15:22:23.171 [pool-7-thread-2] ERROR  [sweetsoft/sapmock:latest] - Could not start container
org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [32775] should be listening)
        at org.testcontainers.containers.wait.strategy.HostPortWaitStrategy.waitUntilReady(HostPortWaitStrategy.java:47)
        at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:35)
        at org.testcontainers.containers.wait.HostPortWaitStrategy.waitUntilReady(HostPortWaitStrategy.java:23)
        at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:35)
        at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:582)

该图像是本地图像:

docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
sweetsoft/sapmock             latest              f02be90356e7        3 hours ago         664MB
openjdk                       8                   bec43387959a        11 days ago         625MB
quay.io/testcontainers/ryuk   0.2.3               64849fd2d464        3 months ago        10.7MB

问题是,为什么要等待32775端口?对于港口有什么好处?

更新

也许该日志会有所帮助:

15:47:47.274 [pool-7-thread-4] INFO org.testcontainers.dockerclient.DockerClientProviderStrategy - Found Docker environment with Environment variables, system properties and defaults. Resolved: 
    dockerHost=unix:///var/run/docker.sock
    apiVersion='{UNKNOWN_VERSION}'
    registryUrl='https://index.docker.io/v1/'
    registryUsername='developer'
    registryPassword='null'
    registryEmail='null'
    dockerConfig='DefaultDockerClientConfig[dockerHost=unix:///var/run/docker.sock,registryUsername=developer,registryPassword=<null>,registryEmail=<null>,registryUrl=https://index.docker.io/v1/,dockerConfigPath=/home/developer/.docker,sslConfig=<null>,apiVersion={UNKNOWN_VERSION},dockerConfig=<null>]'

15:47:47.275 [pool-7-thread-4] INFO org.testcontainers.DockerClientFactory - Docker host IP address is localhost
15:47:47.277 [pool-7-thread-4] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: com.github.dockerjava.core.exec.InfoCmdExec@51a07bb5
15:47:47.389 [pool-7-thread-4] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: com.github.dockerjava.core.exec.VersionCmdExec@70fc9b37
15:47:47.392 [pool-7-thread-4] INFO org.testcontainers.DockerClientFactory - Connected to docker: 
  Server Version: 18.09.6
  API Version: 1.39
  Operating System: Ubuntu 18.04.2 LTS
  Total Memory: 7976 MB
15:47:47.395 [pool-7-thread-4] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: ListImagesCmdImpl[imageNameFilter=quay.io/testcontainers/ryuk:0.2.3,showAll=false,filters=com.github.dockerjava.core.util.FiltersBuilder@0,execution=com.github.dockerjava.core.exec.ListImagesCmdExec@562a343]
15:47:47.417 [pool-7-thread-4] DEBUG org.testcontainers.utility.RegistryAuthLocator - Looking up auth config for image: quay.io/testcontainers/ryuk:0.2.3
15:47:47.417 [pool-7-thread-4] DEBUG org.testcontainers.utility.RegistryAuthLocator - RegistryAuthLocator has configFile: /home/developer/.docker/config.json (does not exist) and commandPathPrefix: 
15:47:47.418 [pool-7-thread-4] WARN org.testcontainers.utility.RegistryAuthLocator - Failure when attempting to lookup auth config (dockerImageName: quay.io/testcontainers/ryuk:0.2.3, configFile: /home/developer/.docker/config.json. Falling back to docker-java default behaviour. Exception message: /home/developer/.docker/config.json (No such file or directory)
15:47:47.418 [pool-7-thread-4] DEBUG org.testcontainers.dockerclient.auth.AuthDelegatingDockerClientConfig - Effective auth config [null]

1 个答案:

答案 0 :(得分:2)

原始的Java库可以回答您的端口问题。
https://www.testcontainers.org/features/networking/

  

请注意,此公开的端口号是从   容器。

     

从主持人的角度来看,Testcontainers实际上将此内容公开在   随机的免费端口。这是设计使然,以避免可能发生的端口冲突。   在本地运行的软件中或在并行测试运行之间出现。

     

由于存在此间接层,因此有必要询问   在运行时用于实际映射端口的测试容器。可以做到   使用getMappedPort方法,该方法采用原始(容器)   端口作为参数

在Scala库中,您可以通过调用

获取此映射端口。
container.mappedPort(yourExposedPort) 

错误很可能与此概念有关,您需要在Docker映像内预先公开该端口。确保您在dockerfile中的某处具有expose 8080命令,或者用于构建您的镜像的任何图像都有