@Test public void simpleRecursiveFileWithPermissionTest() throws TimeoutException { WaitingConsumer wait = new WaitingConsumer(); final ToStringConsumer toString = new ToStringConsumer(); GenericContainer container = new GenericContainer( new ImageFromDockerfile() .withDockerfileFromBuilder(builder -> builder.from("alpine:3.3") .copy("/tmp/foo", "/foo") .cmd("ls", "-al", "/") .build() ).withFileFromFile("/tmp/foo", new File("/mappable-resource/test-resource.txt"), 0754)) .withStartupCheckStrategy(new OneShotStartupCheckStrategy()) .withLogConsumer(wait.andThen(toString)); container.start(); wait.waitUntilEnd(60, TimeUnit.SECONDS); String listing = toString.toUtf8String(); assertThat("Listing shows that file is copied with mode requested.", Arrays.asList(listing.split("\\n")), exactlyNItems(1, allOf(containsString("-rwxr-xr--"), containsString("foo")))); }
@Test public void simpleRecursiveFileTest() throws TimeoutException { WaitingConsumer wait = new WaitingConsumer(); final ToStringConsumer toString = new ToStringConsumer(); // 'src' is expected to be the project base directory, so all source code/resources should be copied in File directory = new File("src"); GenericContainer container = new GenericContainer( new ImageFromDockerfile() .withDockerfileFromBuilder(builder -> builder.from("alpine:3.3") .copy("/tmp/foo", "/foo") .cmd("cat /foo/test/resources/test-recursive-file.txt") .build() ).withFileFromFile("/tmp/foo", directory)) .withStartupCheckStrategy(new OneShotStartupCheckStrategy()) .withLogConsumer(wait.andThen(toString)); container.start(); wait.waitUntilEnd(60, TimeUnit.SECONDS); final String results = toString.toUtf8String(); assertTrue("The container has a file that was copied in via a recursive copy", results.contains("Used for DirectoryTarResourceTest")); }
@Test public void simpleRecursiveClasspathResourceTest() throws TimeoutException { // This test combines the copying of classpath resources from JAR files with the recursive TAR approach, to allow JARed classpath resources to be copied in to an image WaitingConsumer wait = new WaitingConsumer(); final ToStringConsumer toString = new ToStringConsumer(); GenericContainer container = new GenericContainer( new ImageFromDockerfile() .withDockerfileFromBuilder(builder -> builder.from("alpine:3.3") .copy("/tmp/foo", "/foo") .cmd("ls -lRt /foo") .build() ).withFileFromClasspath("/tmp/foo", "/recursive/dir")) // here we use /org/junit as a directory that really should exist on the classpath .withStartupCheckStrategy(new OneShotStartupCheckStrategy()) .withLogConsumer(wait.andThen(toString)); container.start(); wait.waitUntilEnd(60, TimeUnit.SECONDS); final String results = toString.toUtf8String(); // ExternalResource.class is known to exist in a subdirectory of /org/junit so should be successfully copied in assertTrue("The container has a file that was copied in via a recursive copy from a JAR resource", results.contains("content.txt")); }
@Bean(name = DynamoDBProperties.BEAN_NAME_EMBEDDED_DYNAMODB, destroyMethod = "stop") public GenericContainer mariadb(ConfigurableEnvironment environment, DynamoDBProperties properties) throws Exception { log.info("Starting mariadb server. Docker image: {}", properties.dockerImage); GenericContainer mariadb = new GenericContainer(properties.dockerImage) .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(new HostPortWaitStrategy()) .withStartupTimeout(properties.getTimeoutDuration()); mariadb.start(); registerDynamodbEnvironment(mariadb, environment, properties); return mariadb; }
@Bean(name = BEAN_NAME_EMBEDDED_VOLTDB, destroyMethod = "stop") public GenericContainer voltDB(ConfigurableEnvironment environment, VoltDBProperties properties, VoltDBStatusCheck voltDbStatusCheck) { log.info("Starting VoltDB server. Docker image: {}", properties.dockerImage); GenericContainer voltDB = new GenericContainer(properties.dockerImage) .withEnv("HOST_COUNT", "1") .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(voltDbStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); voltDB.start(); registerVoltDBEnvironment(voltDB, environment, properties); return voltDB; }
@Bean(name = BEAN_NAME_EMBEDDED_NEO4J, destroyMethod = "stop") public GenericContainer neo4j(ConfigurableEnvironment environment, Neo4jProperties properties, Neo4jStatusCheck neo4jStatusCheck) throws Exception { log.info("Starting neo4j server. Docker image: {}", properties.dockerImage); GenericContainer neo4j = new GenericContainer(properties.dockerImage) .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts( properties.httpsPort, properties.httpPort, properties.boltPort) .withClasspathResourceMapping( "neo4j-health.sh", "/neo4j-health.sh", BindMode.READ_ONLY) .waitingFor(neo4jStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); neo4j.start(); registerNeo4jEnvironment(neo4j, environment, properties); return neo4j; }
@Bean(name = BEAN_NAME_EMBEDDED_MEMSQL, destroyMethod = "stop") public GenericContainer memsql(ConfigurableEnvironment environment, MemSqlProperties properties, MemSqlStatusCheck memSqlStatusCheck) { log.info("Starting memsql server. Docker image: {}", properties.dockerImage); GenericContainer memsql = new GenericContainer<>(properties.dockerImage) .withEnv("IGNORE_MIN_REQUIREMENTS", "1") .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .withCopyFileToContainer(MountableFile.forClasspathResource("mem.sql"), "/schema.sql") .waitingFor(memSqlStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); memsql.start(); registerMemSqlEnvironment(memsql, environment, properties); return memsql; }
@Bean(name = AEROSPIKE_BEAN_NAME, destroyMethod = "stop") public GenericContainer aerospike(AerospikeWaitStrategy aerospikeWaitStrategy, ConfigurableEnvironment environment, AerospikeProperties properties) { log.info("Starting aerospike server. Docker image: {}", properties.dockerImage); WaitStrategy waitStrategy = new WaitAllStrategy() .withStrategy(aerospikeWaitStrategy) .withStrategy(new HostPortWaitStrategy()) .withStartupTimeout(Duration.of(60, SECONDS)); GenericContainer aerospike = new GenericContainer<>(properties.dockerImage) .withExposedPorts(properties.port) .withLogConsumer(containerLogsConsumer(log)) // see https://github.com/aerospike/aerospike-server.docker/blob/master/aerospike.template.conf .withEnv("NAMESPACE", properties.namespace) .withEnv("SERVICE_PORT", String.valueOf(properties.port)) .withEnv("MEM_GB", String.valueOf(1)) .withEnv("STORAGE_GB", String.valueOf(1)) .withCreateContainerCmdModifier(cmd -> cmd.withCapAdd(Capability.NET_ADMIN)) .waitingFor(waitStrategy) .withStartupTimeout(properties.getTimeoutDuration()); aerospike.start(); registerAerospikeEnvironment(aerospike, environment, properties); return aerospike; }
@Bean(name = BEAN_NAME_EMBEDDED_REDIS, destroyMethod = "stop") public GenericContainer redis(ConfigurableEnvironment environment, RedisProperties properties, RedisStatusCheck redisStatusCheck) throws Exception { log.info("Starting redis server. Docker image: {}", properties.dockerImage); GenericContainer redis = new GenericContainer(properties.dockerImage) .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .withEnv("REDIS_USER", properties.getUser()) .withEnv("REDIS_PASSWORD", properties.getPassword()) .withCommand("redis-server", "--requirepass", properties.getPassword()) .waitingFor(redisStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); redis.start(); registerRedisEnvironment(redis, environment, properties); return redis; }
@SuppressWarnings("resource") private DockerRabbitMQ(Optional<String> hostNamePrefix, Optional<String> clusterIdentity, Optional<String> erlangCookie, Optional<Network> net) { this.hostNameSuffix = clusterIdentity.orElse(UUID.randomUUID().toString()); this.rabbitHostName = hostName(hostNamePrefix); this.container = new GenericContainer<>(Images.RABBITMQ) .withCreateContainerCmdModifier(cmd -> cmd.withName(this.rabbitHostName)) .withCreateContainerCmdModifier(cmd -> cmd.withHostName(this.rabbitHostName)) .withExposedPorts(DEFAULT_RABBITMQ_PORT, DEFAULT_RABBITMQ_ADMIN_PORT) .waitingFor(waitStrategy()) .withLogConsumer(frame -> LOGGER.debug(frame.getUtf8String())) .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig() .withTmpFs(ImmutableMap.of("/var/lib/rabbitmq/mnesia", "rw,noexec,nosuid,size=100m"))); net.ifPresent(this.container::withNetwork); erlangCookie.ifPresent(cookie -> this.container.withEnv(RABBITMQ_ERLANG_COOKIE, cookie)); this.nodeName = DEFAULT_RABBIT_NODE_NAME_PREFIX + "@" + this.rabbitHostName; this.container.withEnv(RABBITMQ_NODENAME, this.nodeName); }
@Bean(name = BEAN_NAME_EMBEDDED_POSTGRESQL, destroyMethod = "stop") public GenericContainer postgresql(ConfigurableEnvironment environment, PostgreSQLProperties properties, PostgreSQLStatusCheck postgreSQLStatusCheck) { log.info("Starting postgresql server. Docker image: {}", properties.dockerImage); GenericContainer postgresql = new GenericContainer(properties.dockerImage) .withEnv("POSTGRES_USER", properties.getUser()) .withEnv("POSTGRES_PASSWORD", properties.getPassword()) .withEnv("PGPASSWORD", properties.password) // for health check .withEnv("POSTGRES_DB", properties.getDatabase()) .withCommand("postgres") .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(postgreSQLStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); postgresql.start(); registerPostgresqlEnvironment(postgresql, environment, properties); return postgresql; }
static GenericContainer create(ElasticSearchProperties properties, Logger containerLogger) { return new FixedHostPortGenericContainer(properties.dockerImage) .withExposedPorts(properties.httpPort, properties.transportPort) .withEnv("cluster.name", properties.getClusterName()) .withEnv("discovery.type", "single-node") .withEnv("ES_JAVA_OPTS", getJavaOpts(properties)) .withLogConsumer(containerLogsConsumer(containerLogger)) .waitingFor(getCompositeWaitStrategy(properties)) .withStartupTimeout(properties.getTimeoutDuration()); }
@Bean(name = BEAN_NAME_EMBEDDED_MARIADB, destroyMethod = "stop") public GenericContainer mariadb(ConfigurableEnvironment environment, MariaDBProperties properties, MariaDBStatusCheck mariaDBStatusCheck) throws Exception { log.info("Starting mariadb server. Docker image: {}", properties.dockerImage); GenericContainer mariadb = new GenericContainer(properties.dockerImage) .withEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "true") .withEnv("MYSQL_USER", properties.getUser()) .withEnv("MYSQL_PASSWORD", properties.getPassword()) .withEnv("MYSQL_DATABASE", properties.getDatabase()) .withCommand( "--character-set-server=" + properties.getEncoding(), "--collation-server=" + properties.getCollation()) .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(mariaDBStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); mariadb.start(); registerMariadbEnvironment(mariadb, environment, properties); return mariadb; }
@SuppressWarnings("resource") public DockerCassandra() { client = DockerClientFactory.instance().client(); cassandraContainer = new GenericContainer<>("cassandra:3.11.3") .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withTmpFs(ImmutableMap.of("/var/lib/cassandra", "rw,noexec,nosuid,size=200m"))) .withExposedPorts(CASSANDRA_PORT) .withLogConsumer(DockerCassandra::displayDockerLog); cassandraContainer .waitingFor(new CassandraWaitStrategy(cassandraContainer)); }
@SuppressWarnings("resource") public DockerCassandra() { client = DockerClientFactory.instance().client(); cassandraContainer = new GenericContainer<>("cassandra:3.11.3") .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withTmpFs(ImmutableMap.of("/var/lib/cassandra", "rw,noexec,nosuid,size=200m"))) .withExposedPorts(CASSANDRA_PORT) .withLogConsumer(DockerCassandra::displayDockerLog); cassandraContainer .waitingFor(new CassandraWaitStrategy(cassandraContainer)); }
public DockerSwiftContainer() { this.swiftContainer = new GenericContainer<>(SWIFT_DOCKER_IMAGE); this.swiftContainer .withExposedPorts(KEYSTONE_ADMIN_PORT) .withExposedPorts(SWIFT_PORT) .withLogConsumer(DockerSwiftContainer::displayDockerLog) .waitingFor( new WaitAllStrategy() .withStrategy( forHttp("/v3") .forPort(KEYSTONE_ADMIN_PORT) .forStatusCode(200) .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND) ).withStrategy( forHttp("/info") .forPort(SWIFT_PORT) .forStatusCode(200) .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND) ) ); }