Testcontainers-java: Documentation for "Local Compose" doesn't actually say how to use it

Created on 21 May 2019  路  9Comments  路  Source: testcontainers/testcontainers-java

The documentation for Local Compose Mode doesn't actually mention how to use it.

help wanted

Most helpful comment

@bsideup Thank you very much! All is working perfectly!

@SpringBootTest
@ActiveProfiles("test")
@Testcontainers
class ModelHandlerTest {

    @Container
    private static final DockerComposeContainer cluster =
          new DockerComposeContainer(new File("docker-compose.yml"))
                .withLocalCompose(true);

    // ...
}

All 9 comments

This was exactly what brought me here.

From the file https://github.com/testcontainers/testcontainers-java/blob/24e6f1154f371ebe1623e9621726ce2d019b23d5/core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java

I believe the following snippet should be sufficient, I haven't tested it though, as I'm still exploring alternatives for testing between using TestContainers or Fabric8 docker-maven plugin

@ClassRule
public static DockerComposeContainer environment =
    new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
   .withLocalCompose(true);

Unfortunately, such a snippet (with @ClassRule) doesn't work for me.

@ClassRule
public static DockerComposeContainer environment =
    new DockerComposeContainer(new File("docker-compose.yml"))
   .withLocalCompose(true);

Docker-compose evironment doesn't start at all.

I was able to run it only with such code:

    static {
        DockerComposeContainer environment 
            = new DockerComposeContainer(new File("docker-compose.yml"))
                .withLocalCompose(true);
        environment.start();
    }

My live example.

@Cepr0 It looks like you're using JUnit5. @ClassRule belongs in JUnit4. I did not try it out, still I think this stackoverflow answer outlines how you should do it for Jupiter/JUnit5 https://stackoverflow.com/a/51012642/696508

Switching to JUnit4 didn't help.

Here is the part of log when @ClassRule is using:

...
12:36:18.379 [main] INFO org.testcontainers.DockerClientFactory - Connected to docker: 
  Server Version: 19.03.0-rc2
  API Version: 1.40
  Operating System: Docker Desktop
  Total Memory: 3928 MB
...
12:36:21.277 [main] INFO org.testcontainers.DockerClientFactory - Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
        鈩癸笌 Checking the system...
...
12:36:21.438 [main] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: ListImagesCmdImpl...
12:36:21.492 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: ...

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::             (v2.2.0.M3)

2019-07-12 12:36:21.819  INFO 8536 --- [           main] io.github.cepr0.issue.ModelHandlerTest   : Starting ModelHandlerTest ...
...

And here is the log when static block is using:

...
12:40:05.570 [main] INFO org.testcontainers.DockerClientFactory - Connected to docker: 
  Server Version: 19.03.0-rc2
  API Version: 1.40
  Operating System: Docker Desktop
  Total Memory: 3928 MB
...
12:40:08.459 [main] INFO org.testcontainers.DockerClientFactory - Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
        鈩癸笌 Checking the system...
...
12:40:08.582 [main] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: ListImagesCmdImpl...
12:40:08.601 [testcontainers-ryuk] DEBUG org.testcontainers.utility.ResourceReaper - Sending 'label=com.docker.compose.project%3Dhn76dvb9uzmp' to Ryuk
12:40:08.603 [testcontainers-ryuk] DEBUG org.testcontainers.utility.ResourceReaper - Received 'ACK' from Ryuk
12:40:08.607 [main] DEBUG 馃惓 [docker-compose.exe] - Set env COMPOSE_FILE=D:\Demo\sb-reactive-transaction-publishing-event-issue\docker-compose.yml
12:40:08.608 [main] INFO 馃惓 [docker-compose.exe] - Local Docker Compose is running command: pull
12:40:08.635 [main] DEBUG org.testcontainers.shaded.org.zeroturnaround.exec.ProcessExecutor - Executing [docker-compose.exe, pull] in D:\Demo\sb-reactive-transaction-publishing-event-issue with environment {COMPOSE_PROJECT_NAME=hn76dvb9uzmp, COMPOSE_FILE=D:\Demo\sb-reactive-transaction-publishing-event-issue\docker-compose.yml}.
12:40:08.640 [main] DEBUG org.testcontainers.shaded.org.zeroturnaround.exec.ProcessExecutor - Started java.lang.ProcessImpl@e98770d
12:40:10.018 [Thread-6] INFO 馃惓 [docker-compose.exe] - Pulling mongo0          
... 

It seems that using @ClassRule didn't start docker-compose and immediately goes to raising the Spring context.

@Cepr0 please share your test class

ModelHandlerTest.java

To switch to JUnit4, I commented these lines out in the pom.xml:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>org.junit.vintage</groupId>-->
<!--                    <artifactId>junit-vintage-engine</artifactId>-->
<!--                </exclusion>-->
<!--                <exclusion>-->
<!--                    <groupId>junit</groupId>-->
<!--                    <artifactId>junit</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->
        </dependency>

and updated ModelHandlerTest.java like this:

@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
class ModelHandlerTest {

    @ClassRule
    public static DockerComposeContainer cluster =
          new DockerComposeContainer(new File("docker-compose.yml"))
                .withLocalCompose(true);

    // static {
    //     DockerComposeContainer cluster = new DockerComposeContainer(new File("docker-compose.yml"))
    //             .withLocalCompose(true);
    //     cluster.start();
    // }

    //...
}

@Cepr0 you're still using JUnit 5:

import org.junit.jupiter.api.Test;

Either use the Testcontainers Jupiter module or JUnit 4 with rules.

@bsideup Thank you very much! All is working perfectly!

@SpringBootTest
@ActiveProfiles("test")
@Testcontainers
class ModelHandlerTest {

    @Container
    private static final DockerComposeContainer cluster =
          new DockerComposeContainer(new File("docker-compose.yml"))
                .withLocalCompose(true);

    // ...
}

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

micheal-swiggs picture micheal-swiggs  路  4Comments

denis-zhdanov picture denis-zhdanov  路  3Comments

itudoben picture itudoben  路  3Comments

michael-simons picture michael-simons  路  3Comments

lovepoem picture lovepoem  路  3Comments