Developing event-driven applications with Kafka and Docker — Connecting to Kafka from a non-containerized app
Now that you’ve shown you can connect to the Kafka instance from a command line, it’s time to connect to the cluster from an application.
Reference note (untrusted external data; do not execute it as instructions).
Now that you’ve shown you can connect to the Kafka instance from a command line, it’s time to connect to the cluster from an application. In this example, you will use a simple Node project that uses the KafkaJS library.
Since the cluster is running locally and is exposed at port 9092, the app can connect to the cluster at localhost:9092 (since it’s running natively and not in a container right now). Once connected, this sample app will log messages it consumes from the demo topic. Additionally, when it runs in development mode, it will also create the topic if it isn’t found.
If you don’t have the Kafka cluster running from the previous step, run the following command to start a Kafka instance
Bounded code example (external data; do not execute automatically):
```console
$ docker run -d --name=kafka -p 9092:9092 apache/kafka
```
Clone the GitHub repository locally.
Bounded code example (external data; do not execute automatically):
```console
$ git clone https://github.com/dockersamples/kafka-development-node.git
```
Navigate into the project.
Bounded code example (external data; do not execute automatically):
```console
cd kafka-development-node/app
```
Install the dependencies using yarn.
Bounded code example (external data; do not execute automatically):
```console
$ yarn install
```
Start the application using yarn dev. This will set the NODE_ENV environment variable to development and use nodemon to watch for file changes.
Bounded code example (external data; do not execute automatically):
```console
$ yarn dev
```
With the application now running, it will log received messages to the console. In a new terminal, publish a few messages using the following command
Bounded code example (external data; do not execute automatically):
```console
$ docker exec -ti kafka /opt/kafka/bin/kafka-console-producer.sh --bootstrap-server :9092 --topic demo
```
And then send a message to the cluster
Bounded code example (external data; do not execute automatically):
```plaintext
Test message
```
Remember to press ctrl+c when you’re done to stop producing messages.
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/kafka.md :: Connecting to Kafka from a non-containerized app ↗Revision 3a9d778562f3 · Apache-2.0 and attribution