Given the following docker-compose.yml file:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181"
kafka:
image: wurstmeister/kafka:0.9.0.1
ports:
- "9092"
links:
- zookeeper:zk
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
I would like to scala the kafka service 3 times. Inside testcontainers I would like to get the servicePort of each scaled Kafka service.
I am using https://github.com/dimafeng/testcontainers-scala for this matter, as I work in Scala.
This is my test setup code:
import java.io.File
import com.dimafeng.testcontainers.{ForAllTestContainer, TestContainerProxy}
import org.scalatest._
import org.testcontainers.containers.DockerComposeContainer
trait KafkaTest extends ForAllTestContainer { self: Suite =>
def kafkaVersion: String
def kafkaScaling: Int
override val container =
DockerCompose(
new File(s"akka/src/test/resources/docker-compose-kafka_$kafkaVersion.yml"),
exposedService = Map("zookeeper_1" -> 2181) ++ (1 to kafkaScaling).map(x => s"kafka_$x" -> (9000 + x)).toMap,
scaling = if(kafkaScaling == 1) Map() else Map("kafka" -> kafkaScaling)
)
}
class DockerCompose(composeFile: File, exposedService: Map[String, Int], scaling: Map[String, Int]) extends TestContainerProxy[DockerComposeContainer[_]] {
type OTCContainer = DockerComposeContainer[T] forSome {type T <: DockerComposeContainer[T]}
override val container: OTCContainer = new DockerComposeContainer(composeFile)
exposedService.foreach(Function.tupled(container.withExposedService))
scaling.foreach(Function.tupled(container.withScaledService))
def getServiceHost = container.getServiceHost _
def getServicePort = container.getServicePort _
}
object DockerCompose {
def apply(composeFile: File, exposedService: Map[String, Int] = Map(), scaling: Map[String, Int] = Map()) =
new DockerCompose(composeFile, exposedService, scaling)
}
As you can see it uses DockerComposeContainer underneath and uses withExposedService and withScaledService. Aswell getServicePort.
The problem is, that docker-compose assigns a dynamic port for in this case port 9092 (kafka scaling) and the ambassador does that too, but it proxies it to the wrong ports (9000 + kafkaScaling range).
How do I scala kafka 3 times and get each port assigned by docker-compose?
Given the following
docker-compose.ymlfile:I would like to scala the
kafkaservice 3 times. Inside testcontainers I would like to get the servicePort of each scaled Kafka service.I am using https://github.com/dimafeng/testcontainers-scala for this matter, as I work in Scala.
This is my test setup code:
As you can see it uses
DockerComposeContainerunderneath and useswithExposedServiceandwithScaledService. AswellgetServicePort.The problem is, that docker-compose assigns a dynamic port for in this case port 9092 (kafka scaling) and the ambassador does that too, but it proxies it to the wrong ports (9000 + kafkaScaling range).
How do I scala kafka 3 times and get each port assigned by docker-compose?