dockerapache-kafkadocker-composeksqldb

Connect to Kafka on host from Docker (ksqlDB)


I'm running ksqldb-server from a docker-compor found here https://ksqldb.io/quickstart.html#quickstart-content

My kafka bootstrap server is running on the same VM in standard alone mode. I can see the messages in one topic with a console consumer:

sudo kafka-avro-console-consumer --from-beginning --bootstrap-server localhost:9092 --topic source-air-input  --property print.key=true --max-messages 2 

Unfortunatly running ksql from docker gives me this error.

ksqldb-server    | [2021-07-15 23:12:58,772] ERROR Failed to start KSQL (io.confluent.ksql.rest.server.KsqlServerMain:66)
ksqldb-server    | java.lang.RuntimeException: Failed to get Kafka cluster information
ksqldb-server    |      at io.confluent.ksql.services.KafkaClusterUtil.getKafkaClusterId(KafkaClusterUtil.java:107)
ksqldb-server    |      at io.confluent.ksql.rest.server.KsqlRestApplication.buildApplication(KsqlRestApplication.java:624)
ksqldb-server    |      at io.confluent.ksql.rest.server.KsqlServerMain.createExecutable(KsqlServerMain.java:152)
ksqldb-server    |      at io.confluent.ksql.rest.server.KsqlServerMain.main(KsqlServerMain.java:59)
ksqldb-server    | Caused by: java.util.concurrent.TimeoutException
ksqldb-server    |      at org.apache.kafka.common.internals.KafkaFutureImpl$SingleWaiter.await(KafkaFutureImpl.java:108)
ksqldb-server    |      at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:272)
ksqldb-server    |      at io.confluent.ksql.services.KafkaClusterUtil.getKafkaClusterId(KafkaClusterUtil.java:105)

My docker-compose.yml is the following.

 ---
version: '3.9'

services:
  ksqldb-server:
    image: confluentinc/ksqldb-server:0.18.0
    hostname: ksqldb-server
    container_name: ksqldb-server
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "8088:8088"
    environment:
      KSQL_LISTENERS: http://0.0.0.0:8088
      KSQL_BOOTSTRAP_SERVERS: host.docker.internal:9092
      KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
      KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"

  ksqldb-cli:
    image: confluentinc/ksqldb-cli:0.18.0
    container_name: ksqldb-cli
    depends_on:
      - ksqldb-server
    entrypoint: /bin/sh
    tty: true

I tried many possible configurations for the address without success. What might be wrong? I tried the suggestions from this question From inside of a Docker container, how do I connect to the localhost of the machine? without success.


Solution

  • Modify Kafka's server.properties

    listener.security.protocol.map=PLAINTEXT_DOCKER:PLAINTEXT,PLAINTEXT_LOCAL:PLAINTEXT
    
    listeners=PLAINTEXT_DOCKER://:29092,PLAINTEXT_LOCAL://localhost:9092
    
    advertised.listeners=PLAINTEXT_DOCKER://host.docker.internal:29092,PLAINTEXT_LOCAL://localhost:9092
    
    inter.broker.listener.name=PLAINTEXT_LOCAL
    

    Update your Compose like so to point at the host rather than itself

    services:
    
      # TODO: add schema-registry
      #   environment: 
      #     SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: host.docker.internal:29092
      #   extra_hosts:
      #     - "host.docker.internal:host-gateway"
    
      # or any other Kafka client 
      ksqldb-server:
        image: confluentinc/ksqldb-server:0.18.0
        hostname: ksqldb-server
        container_name: ksqldb-server
        ports:
          - "8088:8088"
        extra_hosts:
          - "host.docker.internal:host-gateway"
        environment:
           KSQL_BOOTSTRAP_SERVERS: host.docker.internal:29092
           ...
    

    (Tested on Mac), Getting /info endpoint of KSQL

    http :8088/info
    HTTP/1.1 200 OK
    content-length: 133
    content-type: application/json
    
    {
        "KsqlServerInfo": {
            "kafkaClusterId": "ZH2-h1W_SaivCW0qa8DQGA",
            "ksqlServiceId": "default_",
            "serverStatus": "RUNNING",
            "version": "0.18.0"
        }
    }
    

    Replace all host.docker.internal above with the external hostname/IP of the machine, if Kafka is a remote server