Skip to content

[Bug]: BAZEL project - "Could not connect to Ryuk at localhost" in run mode but works in debug mode #9998

@mathieuridet

Description

@mathieuridet

Module

Core

Testcontainers version

1.19.8

Using the latest Testcontainers version?

No

Host OS

Ubuntu 24.04 LTS

Host Arch

x86_64

Docker version

Client: Docker Engine - Community
 Version:           27.3.1
 API version:       1.47
 Go version:        go1.22.7
 Git commit:        ce12230
 Built:             Fri Sep 20 11:40:59 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.3.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.7
  Git commit:       41ca978
  Built:            Fri Sep 20 11:40:59 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.22
  GitCommit:        7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc:
  Version:          1.1.14
  GitCommit:        v1.1.14-0-g2c9f560
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

What happened?

Trying to use a testcontainers postgresql database for a spring batch integration testing.
It works well when I run my test in debug mode but when I run in regular run mode, I get the following error : "Exception in thread "Thread-1" java.lang.IllegalStateException: Could not connect to Ryuk at localhost:32768".
It breaks my build as well.

Does someone know how to handle this ?

Relevant log output

11:19:28.979 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.datahouse.redx.etl.jobs.exportdata.configuration.ExportDataJobConfigurationTest]: ExportDataJobConfigurationTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
11:19:29.037 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.datahouse.redx.etl.RedxEtlApplication for test class com.datahouse.redx.etl.jobs.exportdata.configuration.ExportDataJobConfigurationTest
Sleeping...
11:19:34.105 [main] INFO org.testcontainers.images.PullPolicy -- Image pull policy will be performed by: DefaultPullPolicy()
11:19:34.107 [main] INFO org.testcontainers.utility.ImageNameSubstitutor -- Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
11:19:34.383 [main] INFO org.testcontainers.dockerclient.DockerClientProviderStrategy -- Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
11:19:34.385 [main] INFO org.testcontainers.DockerClientFactory -- Docker host IP address is localhost
11:19:34.398 [main] INFO org.testcontainers.DockerClientFactory -- Connected to docker: 
  Server Version: 27.3.1
  API Version: 1.47
  Operating System: Ubuntu 24.04 LTS
  Total Memory: 31601 MB
11:19:34.486 [main] INFO tc.testcontainers/ryuk:0.7.0 -- Creating container for image: testcontainers/ryuk:0.7.0
11:19:34.608 [main] INFO tc.testcontainers/ryuk:0.7.0 -- Container testcontainers/ryuk:0.7.0 is starting: 4095c2c48399b4d81908337f4021746201ca5215467bd520f14db8f47c7f5ebe
11:19:34.838 [main] INFO tc.testcontainers/ryuk:0.7.0 -- Container testcontainers/ryuk:0.7.0 started in PT0.352276914S
11:19:34.840 [testcontainers-ryuk] WARN org.testcontainers.utility.RyukResourceReaper -- Can not connect to Ryuk at localhost:32768
java.net.ConnectException: Connection refused
	at java.base/sun.nio.ch.Net.pollConnect(Native Method)
	at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682)
	at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542)
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
	at java.base/java.net.Socket.connect(Socket.java:757)
	at org.testcontainers.utility.RyukResourceReaper.lambda$null$1(RyukResourceReaper.java:105)
	at org.rnorth.ducttape.ratelimits.RateLimiter.doWhenReady(RateLimiter.java:27)
	at org.testcontainers.utility.RyukResourceReaper.lambda$maybeStart$2(RyukResourceReaper.java:101)
	at java.base/java.lang.Thread.run(Thread.java:1583)
11:19:35.093 [testcontainers-ryuk] WARN org.testcontainers.utility.RyukResourceReaper -- Can not connect to Ryuk at localhost:32768
// ... (repeated hundreds of time)
11:20:04.843 [main] ERROR org.testcontainers.utility.RyukResourceReaper -- Timed out waiting for Ryuk container to start. Ryuk's logs:
2025/02/20 11:19:34 Pinging Docker...
2025/02/20 11:19:34 Docker daemon is available!
2025/02/20 11:19:34 Starting on port 8080...
2025/02/20 11:19:34 Started!

?[36m╷?[0m
?[36m└─?[0m ?[36mJUnit Jupiter?[0m ?[32m✔?[0m
?[36m   └─?[0m ?[31mExportDataJobConfigurationTest?[0m ?[36m35931 ms?[0m ?[31m✘?[0m ?[31mCould not connect to Ryuk at localhost:32768?[0m

Failures (1):
  JUnit Jupiter:ExportDataJobConfigurationTest
    ClassSource [className = 'com.datahouse.redx.etl.jobs.exportdata.configuration.ExportDataJobConfigurationTest', filePosition = null]
    => java.lang.IllegalStateException: Could not connect to Ryuk at localhost:32768
       org.testcontainers.utility.RyukResourceReaper.maybeStart(RyukResourceReaper.java:145)
       org.testcontainers.utility.RyukResourceReaper.init(RyukResourceReaper.java:42)
       org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:232)
       org.testcontainers.DockerClientFactory$1.getDockerClient(DockerClientFactory.java:106)
       com.github.dockerjava.api.DockerClientDelegate.authConfig(DockerClientDelegate.java:109)
       org.testcontainers.containers.GenericContainer.start(GenericContainer.java:329)
       org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.start(TestcontainersExtension.java:280)
       org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.access$200(TestcontainersExtension.java:267)
       org.testcontainers.junit.jupiter.TestcontainersExtension.lambda$null$4(TestcontainersExtension.java:82)
       org.testcontainers.junit.jupiter.TestcontainersExtension.lambda$startContainers$5(TestcontainersExtension.java:82)
       [...]

Test run finished after 35966 ms
[         2 containers found      ]
[         0 containers skipped    ]
[         2 containers started    ]
[         0 containers aborted    ]
[         1 containers successful ]
[         1 containers failed     ]
[         2 tests found           ]
[         0 tests skipped         ]
[         0 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         0 tests failed          ]


WARNING: Delegated to the 'execute' command.
         This behaviour has been deprecated and will be removed in a future release.
         Please use the 'execute' command directly.
Exception in thread "Thread-1" java.lang.IllegalStateException: Could not connect to Ryuk at localhost:32768
	at org.testcontainers.utility.RyukResourceReaper.maybeStart(RyukResourceReaper.java:145)
	at org.testcontainers.utility.RyukResourceReaper.init(RyukResourceReaper.java:42)
	at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:232)
	at org.testcontainers.DockerClientFactory$1.getDockerClient(DockerClientFactory.java:106)
	at com.github.dockerjava.api.DockerClientDelegate.authConfig(DockerClientDelegate.java:109)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:329)
	at org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.start(TestcontainersExtension.java:280)
	at org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.access$200(TestcontainersExtension.java:267)
	at org.testcontainers.junit.jupiter.TestcontainersExtension.lambda$null$4(TestcontainersExtension.java:82)
	at org.testcontainers.junit.jupiter.TestcontainersExtension.lambda$startContainers$5(TestcontainersExtension.java:82)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at org.testcontainers.junit.jupiter.TestcontainersExtension.startContainers(TestcontainersExtension.java:82)
	at org.testcontainers.junit.jupiter.TestcontainersExtension.beforeAll(TestcontainersExtension.java:56)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Additional Information

Here is the content of the simple test service I use :

package com.datahouse.redx.etl.jobs.exportdata.configuration;

import lombok.Getter;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@ExtendWith(SpringExtension.class)
@SpringBootTest // Encapsulate @DataJpaTest
@testcontainers
@AutoConfigureTestDatabase(
replace = AutoConfigureTestDatabase.Replace.NONE,
connection = EmbeddedDatabaseConnection.NONE)
@EnableBatchProcessing // Otherwise Spring Batch beans are not found at container launch
public class BaseTestContainerRegistry {

@container @Getter
private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:latest");

/** Start postgres container. */
@BeforeAll
static void init() {
postgres.start();
}

/** Closes postgres container. */
@afterall
static void terminate() {
postgres.close();
}

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
registry.add("spring.datasource.driver-class-name", postgres::getDriverClassName);
registry.add("spring.flyway.enabled", () -> "true");
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    resolution/waiting-for-infoWaiting for more information of the issue author or another 3rd party.type/bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions