Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A developer runs Kafka in Docker with advertised.listeners=PLAINTEXT://localhost:9092. The Spring Boot app running on the host works, but the same app in another container on the same Docker network logs repeatedly: "Connection to node 1 (localhost/127.0.0.1:9092) could not be established. Node may not be available." Explain and fix.

The containerised app reaches the broker for bootstrap, but the broker's metadata tells it to use localhost:9092, and inside the app's container localhost is the app's own container, where nothing listens. The fix is two listeners: one advertised as kafka:29092 for containers on the Docker network and one advertised as localhost:9092 for the host, with listener.security.protocol.map and inter.broker.listener.name set. Containers then bootstrap with kafka:29092 and the host keeps localhost:9092.

Why this question is asked

This error message is famous among Kafka developers, and it looks like a network problem even though the network is fine. The application did connect once, which is what makes it confusing. Interviewers ask about it because the explanation requires knowing how Kafka clients really connect, and the same mistake appears in Kubernetes and cloud setups, where it is harder to spot. In this answer, we will recap the bootstrap and metadata flow, follow the failing connection step by step, fix the compose file with two listeners, show how to see the advertised addresses from inside a container, and then extend the idea to Kubernetes.

1A Quick Recap of How Kafka Clients Connect

A Kafka client is given bootstrap.servers, a list of one or more broker addresses. It connects to one of them only to ask for metadata: which brokers exist, which broker leads each partition, and at what address each broker can be reached. That address is the broker's advertised listener. After that, the client opens connections to the brokers using the advertised addresses, never the bootstrap address again.

So two different addresses must work: the bootstrap address, once, and the advertised address, for every produce and fetch. The bootstrap list is chosen by the client, but the advertised address is chosen by the broker and is the same for every client that uses that listener.

2Following the Failing Connection

In the developer's setup the broker advertises PLAINTEXT://localhost:9092. The app in the second container is configured with bootstrap-servers kafka:9092, using the compose service name, which Docker's DNS resolves to the broker container. The bootstrap succeeds. Then the metadata says node 1 is at localhost:9092, and the client tries to connect there.

Step throughWhy the container client fails
App containerDocker DNSBroker container
resolve kafka
172.18.0.2
bootstrap to kafka:9092, metadata request
node 1 is at localhost:9092
connect localhost:9092: own container, refused
log: node may not be available, retry

6 messages in order. Play them, or step through them one at a time.

The bootstrap worked; the advertised address is what fails.

Inside a container, localhost is that container's own network namespace. Nothing listens on port 9092 in the app's container, so the connection is refused, and the client logs "Connection to node 1 (localhost/127.0.0.1:9092) could not be established. Node may not be available." and retries forever. Notice that node 1 in the message is the broker id from the metadata, and the address shown is the advertised one, which is the clue. The app on the host works because, for it, localhost:9092 is the port published from the broker container.

3Fix: One Listener per Network

The broker needs to give each kind of client an address that works for it. We define two listeners with different names and ports. INTERNAL listens on 29092 and advertises kafka:29092, the name containers on the Docker network can resolve. EXTERNAL listens on 9092, which is published to the host, and advertises localhost:9092. Clients pick a listener simply by the port they bootstrap to, and the broker answers with the advertised address of the same listener.

YAML
services:
kafka:
image: apache/kafka:4.1.0
ports:
- "9092:9092" # only the host listener is published
environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka:9093
KAFKA_LISTENERS: INTERNAL://:29092,EXTERNAL://:9092,CONTROLLER://:9093
KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:29092,EXTERNAL://localhost:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,CONTROLLER:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
orders-app:
image: acme/orders-app
environment:
SPRING_KAFKA_BOOTSTRAP_SERVERS: kafka:29092 # container clients use INTERNAL
Which address each client sees
ClientBootstraps toIs told to use
App on the hostlocalhost:9092 (EXTERNAL)localhost:9092
App in a containerkafka:29092 (INTERNAL)kafka:29092
Broker to brokerINTERNALkafka:29092

Notice listener.security.protocol.map. Listener names are free labels, so Kafka needs to know which security protocol each one uses; here all three are PLAINTEXT. inter.broker.listener.name says which listener brokers use to talk to each other, and with custom names it must be set. The app on the host keeps bootstrap-servers localhost:9092 and is told localhost:9092; the app in the container uses kafka:29092 and is told kafka:29092. Both now get an address they can reach.

4Checking the Advertised Addresses From Inside the Client Container

Rather than guess, we can ask the broker what it advertises, from the same place the client runs. kcat prints the metadata, and Kafka's own scripts show the brokers they connect to.

Shell
docker compose exec orders-app kcat -b kafka:29092 -L
# Metadata for all topics (from broker 1: kafka:29092/1):
# 1 brokers:
# broker 1 at kafka:29092 (controller) # reachable from this container
docker compose exec kafka /opt/kafka/bin/kafka-broker-api-versions.sh --bootstrap-server kafka:29092
# kafka:29092 (id: 1 rack: null) ... # the advertised address of the listener

If the broker line still says localhost:9092, the client will fail no matter what bootstrap address it uses. The tools may need installing in the client image, or we run them from a throwaway container on the same Docker network.

5The Same Problem in Kubernetes and the Cloud

The rule is the same everywhere: the advertised address must be resolvable and reachable from the client. In Kubernetes, brokers usually advertise internal pod or headless service names such as kafka-0.kafka-headless.kafka.svc, which work inside the cluster but cannot be resolved from a laptop or another cloud network. A client outside bootstraps through a load balancer, gets internal names back and fails exactly like our container. In the cloud, a broker advertising a private IP fails for clients on the public internet or in a peered network without the right DNS.

6Follow-up questions

How would you expose a broker in Kubernetes to clients outside the cluster?

With a second, external listener per broker, because each client must reach each broker individually, not just a shared address. Each broker gets its own externally reachable address, for example a LoadBalancer service per broker, a NodePort per broker, or an ingress with TLS passthrough and a hostname per broker, and the external listener advertises that address. The internal listener keeps advertising the cluster-internal names for clients inside. Operators such as Strimzi configure this for us with listener types like loadbalancer, nodeport or ingress. The external listener should use TLS and authentication, since it is reachable from outside.

Could the containerised app use host.docker.internal:9092 instead?

On Docker Desktop that name points to the host, so bootstrap would work, but the broker would still advertise localhost:9092 and the client would fail in the same way after bootstrap. Changing only the client's bootstrap address never fixes an advertised listener problem.

7Conclusion

In this answer, we saw that the containerised app bootstrapped fine but then used the advertised address localhost:9092, which inside its own container points to itself. Defining two listeners, INTERNAL advertised as kafka:29092 for the Docker network and EXTERNAL advertised as localhost:9092 for the host, with listener.security.protocol.map and inter.broker.listener.name set, gives every client a reachable address. kcat -L or kafka-broker-api-versions from inside the client container shows what the broker advertises, and the same rule explains external access problems in Kubernetes and the cloud.

What to say in the interviewAbout 80 seconds aloud

Open with

The app in the container gets through bootstrap, because kafka:9092 resolves through Docker DNS to the broker. But Kafka clients only use the bootstrap address to fetch metadata.

Then, if they want more

The metadata contains each broker's advertised listener, and the client then connects to that address for all real traffic. The broker advertises localhost:9092, and inside the app's container localhost is the app container itself, where nothing listens, so it keeps failing with node may not be available. The host app works because for it localhost:9092 is the published port.

The fix is two listeners. One, say INTERNAL, listens on 29092 and advertises kafka:29092 for containers on the Docker network. The other, EXTERNAL, listens on 9092, is published to the host and advertises localhost:9092. I map both names to PLAINTEXT in listener.security.protocol.map and set inter.broker.listener.name to INTERNAL.

Then containers use bootstrap servers kafka:29092 and the host uses localhost:9092, and each gets back an address it can reach. To verify, I would run kcat with minus L, or kafka-broker-api-versions, from inside the client container and look at the broker addresses it prints.

The same issue happens in Kubernetes when brokers advertise internal service names to clients outside the cluster, which is solved with a separate external listener per broker.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A developer runs Kafka in Docker with advertised.listeners=PLAINTEXT://localhost:9092....": https://scrivio-interview-library.netlify.app/spring-boot/kafka/20ff269ba1/