Connect to Kafka on host from Docker (ksqlDB)

Solution 1:

Modify Kafka's server.properties

listeners=PLAINTEXT_DOCKER://:29092,PLAINTEXT_LOCAL://localhost:9092

advertised.listeners=PLAINTEXT_DOCKER://host.docker.internal:29092,PLAINTEXT_LOCAL://localhost:9092

listener.security.protocol.map=PLAINTEXT_DOCKER:PLAINTEXT,PLAINTEXT_LOCAL:PLAINTEXT
inter.broker.listener.name=PLAINTEXT_LOCAL

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

version: '3.9'

services:
  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"
    }
}