0

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.

Cedan Misquith
  • 1,134
  • 9
  • 20

1 Answers1

1

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

  mainClass in Compile := Some("com.example.Scala_producer")
partha_devArch
  • 414
  • 2
  • 10
  • i tried doing it. My main class name is Scala_producer. What is the exact parameter that goes into Some(). I replaced it with com.example.Scala_producer. I get error saying could not find or load com.example.Scala_producer. Also i followed commands given in this link for my scala code :https://medium.com/jeroen-rosenberg/lightweight-docker-containers-for-scala-apps-11b99cf1a666 – Vaibhav Srivastava Aug 08 '19 at 10:09
  • I also followed that commands, and for me worked fine. You probably are missing the package in your object – Pedro Correia Luís Aug 08 '19 at 10:15
  • Also i want to know when we issue the command publishLocal what happens to the dockerfile. It needs to use a docker file to build an image right. How do we configure it for our own purpose. – Vaibhav Srivastava Aug 08 '19 at 10:27
  • The above answer seems to be running my container, but no messages are produced. It just runs and closes after 6s . – Vaibhav Srivastava Aug 08 '19 at 10:41
  • @VaibhavSrivastava The [documentation](https://www.scala-sbt.org/sbt-native-packager/formats/docker.html) explains how to configure `publishLocal` and modify the Dockerfile – Tim Aug 08 '19 at 11:12
  • You can follow the link which you yourself shared. Or, you need to share your build.sbt too, for furthur analysis. – partha_devArch Aug 08 '19 at 12:26
  • @partha_devArch thanks for your help. My container seems to be running fine, but its not reaching while loop. Why is it not able to reach inside while loop and after 60 s it throws an error saying failed to update metadata. Sorry if doubts are silly. This is my first time on dockers. – Vaibhav Srivastava Aug 08 '19 at 13:40
  • 1
    Where are you running your Kafka instance? Based on your error my guess is that Kafka is running on your host machine which your docker container is unable to see. For local testing you can set `--net=host` on your `docker run` command. – Shane Perry Aug 08 '19 at 15:24