Connect to Kafka on host from Docker (ksqlDB)

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

version: '3.9'

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

Leave a Comment