Best Testcontainers-java code snippet using org.testcontainers.containers.DockerComposeContainer.start
Source:IntegrationTests.java
...26import java.util.Optional;27import java.util.function.Supplier;28/**29 * Base class for all tests that require the won services to run. The services30 * are only started once for all tests. If the environment variable31 * 'START_CONTAINERS' (default:true) is set to false, the services are not32 * started - in that case the tests assume the services (wonnode, owner,33 * matcher_service) to be reachable at the configured ports. (see34 * src/test/resources/docker-compose.yml)35 */36@Category(RequiresDockerServer.class)37public abstract class IntegrationTests {38 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());39 private static boolean startContainers = Optional.ofNullable(System.getenv("START_CONTAINERS"))40 .map(Boolean::valueOf).orElse(true);41 @ClassRule42 public static DockerComposeContainer environment = new Supplier<DockerComposeContainer>() {43 public DockerComposeContainer get() {44 if (startContainers) {45 DockerComposeContainer container = null;46 try {47 container = new DockerComposeContainer(48 new File("target/test-classes/docker-compose.yml"))49 .withExposedService("owner", 8082,50 Wait.forLogMessage(51 // "^.+connected with WoN52 // node:.+https://wonnode:8443/won/resource.*$",53 "^.+connected with WoN node:.+$",54 1)55 .withStartupTimeout(Duration56 .ofSeconds(120)))57 .waitingFor("matcher_service",58 Wait.forLogMessage(59 "^.+\\[akka://ClusterSystem/user/WonNodeControllerActor\\] registered won node .+ and start crawling it.+$",60 1))61 .withLocalCompose(true);62 } catch (Exception e) {63 return null;64 }65 return container;66 } else {67 return null;68 }69 }70 }.get();71 static {72 // the containers in our docker-compose file must be reachable from outside the73 // docker environment74 // by the names they have in the compose config (actually only needed for75 // wonnode, but for completeness,76 // we map all)77 DnsMappingAdder.addDnsMapping("owner", "127.0.0.1");78 DnsMappingAdder.addDnsMapping("wonnode", "127.0.0.1");79 DnsMappingAdder.addDnsMapping("postgres", "127.0.0.1");80 DnsMappingAdder.addDnsMapping("bigdata", "127.0.0.1");81 }82 @BeforeClass83 public static void checkDockerAvailable() {84 Assume.assumeTrue(isDockerAvailable());85 }86 private static boolean isDockerAvailable() {87 try {88 DockerClientFactory.instance().client();89 return true;90 } catch (Throwable ex) {91 return false;92 }93 }94 @BeforeClass95 public static void logContainerStartupMessage() {96 if (startContainers) {97 logger.info("Running tests against containers managed by testcontainers framework");98 logger.info("To run against externally managed containers, set environment variable 'START_CONTAINERS' to 'false'");99 } else {100 logger.info("Running against externally managed containers as environment variable 'START_CONTAINERS' is 'false'");101 }102 }103 @Test(timeout = 10 * 1000)104 public void testOwnerReachable() throws Exception {105 CloseableHttpClient httpclient = getHttpClientThatTrustsAnyCert();106 HttpGet httpget = new HttpGet("https://owner:8082/owner");107 try (CloseableHttpResponse response = httpclient.execute(httpget)) {108 int statusCode = response.getStatusLine().getStatusCode();109 Assert.assertTrue(200 <= statusCode && 300 > statusCode);110 } finally {...
Source:TestCheckConfiguration.java
...21 .forLogMessage(".*database system is ready to accept connections.*", 1));22 public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {23 @Override24 public void initialize(ConfigurableApplicationContext configurableApplicationContext) {25 container.start();26// environment.start();27 TestPropertyValues.of(28 "spring.datasource.url=" + container.getJdbcUrl(),29 "spring.datasource.username=" + container.getUsername(),30 "spring.datasource.password=" + container.getPassword(),31 "spring.datasource.driver-class-name=" + container.getDriverClassName()32 ).applyTo(configurableApplicationContext.getEnvironment());33 log.info(container.getLivenessCheckPortNumbers().toString());34 }35// @ClassRule36// public static DockerComposeContainer environment =37// new DockerComposeContainer(new File("src/test/resources/compose.yml"))38// .withExposedService("postgres_1", 5555)39// .withExposedService("pgadmin_1", 4444);40 }...
Source:DockerizedInfrastructure.java
...25 dockerServices = new DockerComposeContainer(DOCKER_COMPOSE)26 .withLocalCompose(true)27 .waitingFor("s3-storage", S3_STORAGE_WAIT_STRATEGY)28 .withExposedService(S3_STORAGE_SERVICE, S3_STORAGE_PORT);29 dockerServices.start();30 TestPropertyValues.of(31 format("%s=%s", SPRING_S3_STORAGE_ENDPOINT, "http://localhost:" + dockerServices.getServicePort(S3_STORAGE_SERVICE, S3_STORAGE_PORT))32 ).applyTo(configurableApplicationContext.getEnvironment());33 }34 @PreDestroy35 void preDestroy() {36 dockerServices.stop();37 }38}...
start
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.containers.wait.strategy.WaitAllStrategy;4import org.testcontainers.containers.wait.strategy.WaitStrategy;5import java.io.File;6import java.util.concurrent.TimeUnit;7public class DockerComposeContainerTest {8 public static void main(String[] args) {9 WaitStrategy waitStrategy = new WaitAllStrategy()10 .withStrategy(Wait.forLogMessage(".*started.*", 1))11 .withStrategy(Wait.forLogMessage(".*started.*", 1))12 .withStartupTimeout(new org.testcontainers.containers.wait.strategy.WaitStrategyTarget.Duration(1, TimeUnit.MINUTES));13 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))14 .withExposedService("app_1", 8080, waitStrategy)15 .withExposedService("app_2", 8080, waitStrategy)16 .withLocalCompose(true);17 container.start();18 }19}20DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))21 .withExposedService("app_1", 8080, Wait.forHttp("/").forStatusCode(200))22 .withExposedService("app_2", 8080, Wait.forHttp("/").forStatusCode(200))23 .withLocalCompose(true);24 container.start();25 at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:491)26 at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:337)27 at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:88)28 at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:335)29 at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:319)
start
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3public class 1 {4 public static void main(String[] args) {5 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))6 .withExposedService("web_1", 8000)7 .withExposedService("web_2", 8001)8 .withExposedService("web_3", 8002)9 .withExposedService("web_4", 8003);10 container.start();11 }12}13import org.testcontainers.containers.GenericContainer;14import java.io.File;15public class 2 {16 public static void main(String[] args) {17 GenericContainer container = new GenericContainer("nginx")18 .withExposedPorts(80)19 .withCommand("nginx -g 'daemon off;'");20 container.start();21 }22}23import org.testcontainers.containers.JdbcDatabaseContainer;24import java.io.File;25public class 3 {26 public static void main(String[] args) {27 JdbcDatabaseContainer container = new JdbcDatabaseContainer("mysql:5.7")28 .withExposedPorts(3306)29 .withCommand("mysqld --default-authentication-plugin=mysql_native_password");30 container.start();31 }32}33import org.testcontainers.containers.KafkaContainer;34import java.io.File;35public class 4 {36 public static void main(String[] args) {37 KafkaContainer container = new KafkaContainer("5.2.1")38 .withExposedPorts(9092)39 .withCommand("kafka-server-start.sh /etc/kafka/server.properties");40 container.start();41 }42}43import org.testcontainers.containers.Network;44import java.io.File;45public class 5 {46 public static void main(String[] args) {47 Network container = new Network();48 container.start();49 }50}
start
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3public class 1 {4 public static void main(String[] args) {5 File dockerComposeFile = new File("docker-compose.yml");6 DockerComposeContainer container = new DockerComposeContainer(dockerComposeFile).withExposedService("my_service_1", 8080);7 container.start();8 }9}10ENTRYPOINT ["sh", "-c", "while true; do echo \"HTTP/1.1 200 OK\r11\" | nc -l -p 80; done"]
start
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.containers.wait.strategy.WaitAllStrategy;4import java.io.File;5import java.util.concurrent.TimeUnit;6public class TestDockerComposeContainer {7 public static void main(String[] args) throws Exception {8 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))9 .withExposedService("web", 8080, Wait.forHttp("/").forStatusCode(200).withStartupTimeout(Duration.ofSeconds(60)))10 .withExposedService("db", 5432, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(60)))11 .withExposedService("redis", 6379, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(60)));12 container.start();13 System.out.println("Started");14 Thread.sleep(1000 * 60 * 10);15 container.stop();16 }17}
start
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import java.io.File;4import java.util.concurrent.TimeUnit;5public class Test {6 public static void main(String[] args) {7 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))8 .withExposedService("postgres_1", 5432, Wait.forListeningPort().withStartupTimeout(5, TimeUnit.MINUTES));9 container.start();10 }11}
start
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3import java.util.concurrent.TimeUnit;4import org.testcontainers.containers.wait.strategy.Wait;5public class TestDockerComposeContainer {6 public static void main(String[] args) {7 DockerComposeContainer container = new DockerComposeContainer(new File("docker-compose.yml"))8 .withExposedService("db_1", 3306, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(120)));9 container.start();10 System.out.println(container.getServiceHost("db_1", 3306));11 System.out.println(container.getServicePort("db_1", 3306));12 container.stop();13 }14}15import org.testcontainers.containers.GenericContainer;16import java.io.File;17import java.util.concurrent.TimeUnit;18import org.testcontainers.containers.wait.strategy.Wait;19public class TestDockerComposeContainer {20 public static void main(String[] args) {21 GenericContainer container = new GenericContainer(new File("docker-compose.yml"))22 .withExposedService("db_1", 3306, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(120)));23 container.start();24 System.out.println(container.getServiceHost("db_1", 3306));25 System.out.println(container.getServicePort("db_1", 3306));26 container.stop();27 }28}29import org.testcontainers.containers.JdbcDatabaseContainer;30import java.io.File;31import java.util.concurrent.TimeUnit;32import org.testcontainers.containers.wait.strategy.Wait;33public class TestDockerComposeContainer {34 public static void main(String[] args) {35 JdbcDatabaseContainer container = new JdbcDatabaseContainer(new File("docker-compose.yml"))36 .withExposedService("db_1", 3306, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(120)));37 container.start();38 System.out.println(container.getServiceHost("db_1", 3306));39 System.out.println(container.getServicePort("db_1", 3306));40 container.stop();41 }42}43import org.testcontainers.containers.JdbcDatabaseContainer;44import java
start
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3public class DockerComposeContainerTest {4 public static void main(String[] args) {5 File composeFile = new File("/home/abc/Documents/docker-compose.yml");6 DockerComposeContainer container = new DockerComposeContainer(composeFile);7 container.start();8 container.stop();9 }10}
start
Using AI Code Generation
1import org.testcontainers.containers.DockerComposeContainer;2import java.io.File;3public class DockerComposeContainerStart {4 public static void main(String[] args) {5 DockerComposeContainer dockerComposeContainer = new DockerComposeContainer(new File("docker-compose.yml"));6 dockerComposeContainer.start();7 }8}9at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:325)10at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:311)11at DockerComposeContainerStart.main(DockerComposeContainerStart.java:14)12at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:88)13at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:495)14at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:323)15at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)16Caused by: org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!