Kafka and zookeeper docker containers talking to my api on the host machine

step 1:

I’ve followed some steps in this guide and have created a zookeeper container and connected it to a Kafka container so they can talk to each other via local ports. I’ve also created a topic "foo" with 1 partition and sent 42 messages with a docker command in there. As I understand there is a Kafka port on my local os (localhost:29092) which is exposed to producing and listening to messages.

enter image description here

step 2:

Next, I’m trying to connect my Kafka producer and consumer scripts in my Springboot boot API without any docker-composing to this port as follow (I’d like to prove if these two containers are able two work as a normal Kafka distributive on my host os):

public class KafkaProducer {private static final String TOPIC = "foo";...}
public class KafkaConsumer { @KafkaListener(topics = "foo", groupId = "group1")...}

spring.kafka.producer.bootstrap-servers: localhost:29092
spring.kafka.consumer.bootstrap-servers: localhost:29092

as result, I’m getting these ERRORS:

2020-12-23 14:38:45.937  WARN 14056 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-group1-1, groupId=group1] Connection to node -1 (localhost/127.0.0.1:29092) could not be established. Broker may not be available.
2020-12-23 14:38:45.937  WARN 14056 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-group1-1, groupId=group1] Bootstrap broker localhost:29092 (id: -1 rack: null) disconnected.

Could you please tell me what I got wrong? I’m completely new to Java, Kafka, and Docker. What should I read to understand it better?

Source: Docker Questions