How to resolve org.testcontainers.containers.ContainerFetchException on Spring Boot?

Viewed 11147

I am working on developing a few integration tests for my spring boot application. I am using testcontainers in order to create a MongoDB docker image. My code so far:

ContainerListener.java class:

public class ContainerListener implements TestListener {

@ClassRule
public static Network sharedNetwork = Network.newNetwork();

@ClassRule
public static GenericContainer mongoDBContainer = new GenericContainer("mongo:3.2.4").withNetwork(sharedNetwork)
        .withNetworkAliases("mongo").withExposedPorts(27017);

public static MockServerContainer mockServerContainer = new MockServerContainer().withNetwork(sharedNetwork)
        .withNetworkAliases("mockserver").withExposedPorts(1080);

public static int getMockPort() {
    return mockServerContainer.getMappedPort(1080);
}

public static int getMongoPort() {
    return mongoDBContainer.getMappedPort(27017);
}

public static void runAll() {
    List.of(mongoDBContainer, mockServerContainer).forEach(e -> e.start());
}

public static void stopAll() {
    List.of(mongoDBContainer, mockServerContainer).forEach(e -> e.stop());
}

}

And SomeControllerIntegrationTest.java class:

import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

public class RatingControllerIntegrationTest {

    @BeforeAll
    public static void setuo() {
        ContainerListener.runAll();

    }

    @AfterAll
    public static void tearDown() {
        ContainerListener.stopAll();
    }

    @Test
    public void detailsTest() throws Exception {
        RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
        RestAssured.defaultParser = Parser.JSON;

        given().log().all().when().get("http://localhost:8080/actuator/info/").then().log().all()
                .statusCode(200);
    }

}

Despite that after running the tests I get the following error:

org.testcontainers.containers.ContainerLaunchException: Container startup failed

Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=mongo:3.2.4, imagePullPolicy=DefaultPullPolicy())
    at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1279)
    at org.testcontainers.containers.GenericContainer.logger(GenericContainer.java:613)
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:320)
    ... 58 more
Caused by: java.lang.IllegalStateException: Can not connect to Ryuk at localhost:32770
    at org.testcontainers.utility.ResourceReaper.start(ResourceReaper.java:176)
    at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:168)
    at org.testcontainers.LazyDockerClient.getDockerClient(LazyDockerClient.java:14)
    at org.testcontainers.LazyDockerClient.listImagesCmd(LazyDockerClient.java:12)
    at org.testcontainers.images.LocalImagesCache.maybeInitCache(LocalImagesCache.java:68)
    at org.testcontainers.images.LocalImagesCache.get(LocalImagesCache.java:32)
    at org.testcontainers.images.AbstractImagePullPolicy.shouldPull(AbstractImagePullPolicy.java:18)
    at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:59)
    at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:26)
    at org.testcontainers.utility.LazyFuture.getResolvedValue(LazyFuture.java:20)
    at org.testcontainers.utility.LazyFuture.get(LazyFuture.java:27)
    at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1277)
    ... 60 more
2 Answers

If you haven't seen yet: https://github.com/testcontainers/testcontainers-java/issues/3166

github user @bsideup outlined three options. This will of course only apply to you if you are a Mac User using Docker for Mac.

  • use Testcontainers 1.15.0-rc2 that includes the fix
  • Disable gRPC FUSE in Docker for Mac 2.4.0
  • Downgrade to Docker for Mac 2.3.x

This (disable gRPC) solved the issue for me so I leave this as a reference. Not using gRPC FUSE can have significant impact depending on your workflow. So you better check if that is fine or better downgrade to Docker for Mac 2.3.x. I did not pick the first option as it seems it does not stop containers which is the worst of all the options for me.

This exception came when using Ubuntu operating system and when building the java project. The integration test started to fail at once. Tried with different option but could not resolve the issue.

In Ubuntu OS, in order to access the docker command we use root permission(i.e We use sudo command with docker command). In order to build the java project I have used the sudo command. Please refer the below command.

sudo ./gradlew clean build

The above command fixed the issue.

Related