@Test public void testFixedHostPortMapping() throws IOException { // first find a free port on the docker host that will work for testing GenericContainer portDiscoveryRedis = new GenericContainer("redis:3.0.2").withExposedPorts(REDIS_PORT); portDiscoveryRedis.start(); Integer freePort = portDiscoveryRedis.getMappedPort(REDIS_PORT); portDiscoveryRedis.stop(); // Set up a FixedHostPortGenericContainer as if this were a @Rule FixedHostPortGenericContainer redis = new FixedHostPortGenericContainer("redis:3.0.2").withFixedExposedPort(freePort, REDIS_PORT); redis.start(); // Config redisConfig = new Config(); // redisConfig.useSingleServer().setAddress(redis.getContainerIpAddress() + ":" + freePort); // Redisson redisson = Redisson.create(redisConfig); // // redisson.getBucket("test").set("foo"); // // assertEquals("The bucket content was successfully set", "foo", redisson.getBucket("test").get()); // assertEquals("The container returns the fixed port from getMappedPort(...)", freePort, redis.getMappedPort(REDIS_PORT)); } }
final List<String> command = new ArrayList<>(); this.container = new FixedHostPortGenericContainer<>(ETCD_DOCKER_IMAGE_NAME); this.container.withExposedPorts(ETCD_CLIENT_PORT, ETCD_PEER_PORT); this.container.withNetwork(network); this.container.withNetworkAliases(name); this.container.waitingFor(waitStrategy()); this.container.withLogConsumer(logConsumer()); container.addFileSystemBind(dataDirectory.toString(), ETCD_DATA_DIR, BindMode.READ_WRITE, SelinuxContext.SHARED); command.add("--data-dir"); command.add(ETCD_DATA_DIR); this.container.withClasspathResourceMapping( "ssl/cert/" + name + ".pem", "/etc/ssl/etcd/server.pem", SelinuxContext.SHARED); this.container.withClasspathResourceMapping( "ssl/cert/" + name + "-key.pem", "/etc/ssl/etcd/server-key.pem", this.container.withCommand(command.toArray(new String[command.size()]));
@Bean(name = ZOOKEEPER_BEAN_NAME, destroyMethod = "stop") public GenericContainer zookeeper(ZookeeperStatusCheck zookeeperStatusCheck, ZookeeperConfigurationProperties zookeeperProperties, ConfigurableEnvironment environment, Network network) { String currentTimestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH-mm-ss-nnnnnnnnn")); String zkData = Paths.get(zookeeperProperties.getDataFileSystemBind(), currentTimestamp).toAbsolutePath().toString(); log.info("Writing zookeeper data to: {}", zkData); String zkTransactionLogs = Paths.get(zookeeperProperties.getTxnLogsFileSystemBind(), currentTimestamp).toAbsolutePath().toString(); log.info("Writing zookeeper transaction logs to: {}", zkTransactionLogs); log.info("Starting zookeeper server. Docker image: {}", zookeeperProperties.getDockerImage()); int mappingPort = zookeeperProperties.getZookeeperPort(); GenericContainer zookeeper = new FixedHostPortGenericContainer<>(zookeeperProperties.getDockerImage()) .withLogConsumer(containerLogsConsumer(log)) .withEnv("ZOOKEEPER_CLIENT_PORT", String.valueOf(mappingPort)) .withFileSystemBind(zkData, "/var/lib/zookeeper/data", BindMode.READ_WRITE) .withFileSystemBind(zkTransactionLogs, "/var/lib/zookeeper/log", BindMode.READ_WRITE) .withExposedPorts(mappingPort) .withFixedExposedPort(mappingPort, mappingPort) .withNetwork(network) .withNetworkAliases(ZOOKEEPER_HOST_NAME) .waitingFor(zookeeperStatusCheck) .withStartupTimeout(zookeeperProperties.getTimeoutDuration()); zookeeper.start(); registerZookeeperEnvironment(zookeeper, environment, zookeeperProperties); return zookeeper; }
public void restart() { if (dataDirectory == null) { throw new IllegalStateException("Container not restartable, please create it with restartable=true"); } LOGGER.debug("restarting etcd container {} with command: {}", endpoint, String.join(" ", container.getCommandParts())); final int port = this.container.getMappedPort(ETCD_CLIENT_PORT); this.container.stop(); this.container.withExposedPorts(ETCD_PEER_PORT); this.container.withFixedExposedPort(port, ETCD_CLIENT_PORT); this.container.start(); }
protected static GenericContainer create(CouchbaseProperties properties, Logger containerLogger, Network network) { return new FixedHostPortGenericContainer<>(properties.dockerImage) .withFixedExposedPort(properties.carrierDirectPort, properties.carrierDirectPort) .withFixedExposedPort(properties.httpDirectPort, properties.httpDirectPort) .withFixedExposedPort(properties.queryServicePort, properties.queryServicePort) .withFixedExposedPort(properties.queryRestTrafficPort, properties.queryRestTrafficPort) .withFixedExposedPort(properties.searchServicePort, properties.searchServicePort) .withFixedExposedPort(properties.analyticsServicePort, properties.analyticsServicePort) .withFixedExposedPort(properties.memcachedSslPort, properties.memcachedSslPort) .withFixedExposedPort(properties.memcachedPort, properties.memcachedPort) .withFixedExposedPort(properties.queryRestTrafficSslPort, properties.queryRestTrafficSslPort) .withFixedExposedPort(properties.queryServiceSslPort, properties.queryServiceSslPort) .withLogConsumer(containerLogsConsumer(containerLogger)) .withNetwork(network) .withNetworkAliases(COUCHBASE_HOST_NAME) .waitingFor(getCompositeWaitStrategy(properties)) .withStartupTimeout(properties.getTimeoutDuration()); }
private GenericContainer<?> getContainerHBase(String image, Network network, String logWaitRegex, int logWaitTimes, boolean matchExposedPort) { FixedHostPortGenericContainer<?> container = new FixedHostPortGenericContainer<>(image); container.withNetwork(network); container.withNetworkAliases("hbase_alias"); container.withFixedExposedPort(HBASE_ZK_PORT, HBASE_ZK_PORT); container.withFixedExposedPort(16201, HBASE_REGION_SERVER_PORT); container.withFixedExposedPort(16000, HBASE_MASTER_PORT); container.withCreateContainerCmdModifier(cmd -> cmd.withName(HBASE_CONTAINER_NAME)); container.withCreateContainerCmdModifier(cmd -> cmd.withHostName(HBASE_HOST_NAME)); return container; }
@Profile("test") @Bean(destroyMethod = "stop") public FixedHostPortGenericContainer getFixedHostPortGenericContainer(){ String url = String.join(":", props.getElastic().getImageUrl(), props.getElastic().getVersion()); FixedHostPortGenericContainer fixed = new FixedHostPortGenericContainer(url); fixed.withFixedExposedPort(props.getClients().getHttpPort(), props.getClients().getContainerPort()); fixed.waitingFor(Wait.forHttp("/")); fixed.start(); return fixed; }
public URI peerEndpoint() { final String host = container.getContainerIpAddress(); final int port = container.getMappedPort(ETCD_PEER_PORT); return newURI(host, port); }
public void start() { LOGGER.debug("starting etcd container {} with command: {}", endpoint, String.join(" ", container.getCommandParts())); this.container.start(); this.listener.started(this); if (dataDirectory != null) { // needed in order to properly clean resources during shutdown setDataDirectoryPermissions("o+rwx"); } }
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()); }
/** * Bind a fixed TCP port on the docker host to a container port * @param hostPort a port on the docker host, which must be available * @param containerPort a port in the container * @return this container */ public SELF withFixedExposedPort(int hostPort, int containerPort) { return withFixedExposedPort(hostPort, containerPort, InternetProtocol.TCP); }
@Profile("test") @Bean(destroyMethod = "close") public RestHighLevelClient getRestClientForTest() { return new RestHighLevelClient(RestClient.builder(new HttpHost(getFixedHostPortGenericContainer().getContainerIpAddress(), props.getClients().getHttpPort())).setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(getCredentialsProvider()))); }
final List<String> command = new ArrayList<>(); this.container = new FixedHostPortGenericContainer<>(ETCD_DOCKER_IMAGE_NAME); this.container.withExposedPorts(ETCD_CLIENT_PORT, ETCD_PEER_PORT); this.container.withNetwork(network); this.container.withNetworkAliases(name); this.container.waitingFor(waitStrategy()); this.container.withLogConsumer(logConsumer()); container.addFileSystemBind(dataDirectory.toString(), ETCD_DATA_DIR, BindMode.READ_WRITE, SelinuxContext.SHARED); command.add("--data-dir"); command.add(ETCD_DATA_DIR); this.container.withClasspathResourceMapping( "ssl/cert/" + name + ".pem", "/etc/ssl/etcd/server.pem", SelinuxContext.SHARED); this.container.withClasspathResourceMapping( "ssl/cert/" + name + "-key.pem", "/etc/ssl/etcd/server-key.pem", this.container.withCommand(command.toArray(new String[command.size()]));
GenericContainer kafka = new FixedHostPortGenericContainer<>(kafkaProperties.getDockerImage()) .withLogConsumer(containerLogsConsumer(log)) .withCreateContainerCmdModifier(cmd -> cmd.withHostName(KAFKA_HOST_NAME)) .withEnv("KAFKA_ZOOKEEPER_CONNECT", containerZookeeperConnect) .withEnv("KAFKA_BROKER_ID", "-1") .withEnv("KAFKA_LISTENER_SECURITY_PROTOCOL_MAP", "EXTERNAL_PLAINTEXT:PLAINTEXT,INTERNAL_PLAINTEXT:PLAINTEXT") .withEnv("KAFKA_ADVERTISED_LISTENERS", "EXTERNAL_PLAINTEXT://localhost:" + kafkaExternalPort + ",INTERNAL_PLAINTEXT://" + KAFKA_HOST_NAME + ":" + kafkaInternalPort) .withEnv("KAFKA_LISTENERS", "EXTERNAL_PLAINTEXT://0.0.0.0:" + kafkaExternalPort+",INTERNAL_PLAINTEXT://0.0.0.0:" + kafkaInternalPort) .withEnv("KAFKA_INTER_BROKER_LISTENER_NAME", "INTERNAL_PLAINTEXT") .withEnv("KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS", "1") .withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", String.valueOf(kafkaProperties.getReplicationFactor())) .withEnv("KAFKA_LOG_FLUSH_INTERVAL_MS", String.valueOf(kafkaProperties.getLogFlushIntervalMs())) .withEnv("KAFKA_REPLICA_SOCKET_TIMEOUT_MS", String.valueOf(kafkaProperties.getReplicaSocketTimeoutMs())) .withEnv("KAFKA_CONTROLLER_SOCKET_TIMEOUT_MS", String.valueOf(kafkaProperties.getControllerSocketTimeoutMs())) .withFileSystemBind(kafkaData, "/var/lib/kafka/data", BindMode.READ_WRITE) .withExposedPorts(kafkaInternalPort, kafkaExternalPort) .withFixedExposedPort(kafkaInternalPort, kafkaInternalPort) .withFixedExposedPort(kafkaExternalPort, kafkaExternalPort) .withNetwork(network) .withNetworkAliases(KAFKA_HOST_NAME) .withExtraHost(KAFKA_HOST_NAME, "127.0.0.1") .waitingFor(kafkaStatusCheck) .withStartupTimeout(kafkaProperties.getTimeoutDuration());
public void restart() { if (dataDirectory == null) { throw new IllegalStateException("Container not restartable, please create it with restartable=true"); } LOGGER.debug("restarting etcd container {} with command: {}", endpoint, String.join(" ", container.getCommandParts())); final int port = this.container.getMappedPort(ETCD_CLIENT_PORT); this.container.stop(); this.container.withExposedPorts(ETCD_PEER_PORT); this.container.withFixedExposedPort(port, ETCD_CLIENT_PORT); this.container.start(); }
public URI clientEndpoint() { final String host = container.getContainerIpAddress(); final int port = container.getMappedPort(ETCD_CLIENT_PORT); return newURI(host, port); }
public void start() { LOGGER.debug("starting etcd container {} with command: {}", endpoint, String.join(" ", container.getCommandParts())); this.container.start(); this.listener.started(this); if (dataDirectory != null) { // needed in order to properly clean resources during shutdown setDataDirectoryPermissions("o+rwx"); } }
/** * Bind a fixed TCP port on the docker host to a container port * @param hostPort a port on the docker host, which must be available * @param containerPort a port in the container * @return this container */ public SELF withFixedExposedPort(int hostPort, int containerPort) { return withFixedExposedPort(hostPort, containerPort, InternetProtocol.TCP); }
final List<String> command = new ArrayList<>(); this.container = new FixedHostPortGenericContainer<>(ETCD_DOCKER_IMAGE_NAME); this.container.withExposedPorts(ETCD_CLIENT_PORT, ETCD_PEER_PORT); this.container.withNetwork(network); this.container.withNetworkAliases(name); this.container.waitingFor(waitStrategy()); this.container.withLogConsumer(logConsumer()); container.addFileSystemBind(dataDirectory.toString(), ETCD_DATA_DIR, BindMode.READ_WRITE, SelinuxContext.SHARED); command.add("--data-dir"); command.add(ETCD_DATA_DIR); this.container.withClasspathResourceMapping( "ssl/cert/" + name + ".pem", "/etc/ssl/etcd/server.pem", SelinuxContext.SHARED); this.container.withClasspathResourceMapping( "ssl/cert/" + name + "-key.pem", "/etc/ssl/etcd/server-key.pem", this.container.withCommand(command.toArray(new String[command.size()]));
public void restart() { if (dataDirectory == null) { throw new IllegalStateException("Container not restartable, please create it with restartable=true"); } LOGGER.debug("restarting etcd container {} with command: {}", endpoint, String.join(" ", container.getCommandParts())); final int port = this.container.getMappedPort(ETCD_CLIENT_PORT); this.container.stop(); this.container.withExposedPorts(ETCD_PEER_PORT); this.container.withFixedExposedPort(port, ETCD_CLIENT_PORT); this.container.start(); }