Search code examples
scaladockerdockerfilekafka-producer-api

How to dockerize and write a dockerfile a Scala written kafka producer


I am new to dockers and been trying to understand how to write a docker file to create my custom image. My Scala class is producing message to a topic continuously. I want to reproduce the same functionality with dockers. Can someone help me with the docker file.

I have tried using sbt docker:publishLocal, it creates the image but when i try to run the image it says unble to find the class. I am specifically looking to run it using docker file.

Here is the code which is working in intelliJ

import java.util.Properties

import org.apache.kafka.clients.producer._



object Scala_producer extends App{




      val  props = new Properties()
      props.put("bootstrap.servers", "localhost:9092")

      props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
      props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

      val producer = new KafkaProducer[String, String](props)


      val TOPIC="tt"
      println(producer.partitionsFor(TOPIC))

      while(true){
        val record = new ProducerRecord(TOPIC, "key", "hello ")
        producer.send(record)
       println("producing")
      }

      producer.close()

    }

i expect to run the docker and get infinite producing message.


Solution

  • If this is the only issue, then add the mainclass to your build.sbt:

      mainClass in Compile := Some("com.example.Scala_producer")