@Test public void createContainerCmdHookTest() { // Use random name to avoid the conflicts between the tests String randomName = Base58.randomString(5); try( GenericContainer container = new GenericContainer<>("redis:3.0.2") .withCommand("redis-server", "--help") .withCreateContainerCmdModifier(cmd -> cmd.withName("overrideMe")) // Preserves the order .withCreateContainerCmdModifier(cmd -> cmd.withName(randomName)) // Allows to override pre-configured values by GenericContainer .withCreateContainerCmdModifier(cmd -> cmd.withCmd("redis-server", "--port", "6379")) ) { container.start(); assertEquals("Name is configured", "/" + randomName, container.getContainerInfo().getName()); assertEquals("Command is configured", "[redis-server, --port, 6379]", Arrays.toString(container.getContainerInfo().getConfig().getCmd())); } }
@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 = 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; }
private GenericContainer<?> getContainer(String image, int port, Network network, String logWaitRegex, int logWaitTimes, boolean matchExposedPort) { GenericContainer<?> container = new GenericContainer<>(image) .withExposedPorts(port) .waitingFor( Wait.forLogMessage(logWaitRegex, logWaitTimes).withStartupTimeout(Duration.ofMinutes(5L)) ); if (network != null) { container.withNetwork(network); } if(matchExposedPort) { container.withCreateContainerCmdModifier( command -> command.withPortBindings(PortBinding.parse(String.format("%d:%d", port, port))) ); } return container; }
@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)); }