-2

docker-compose.yml

I'm trying to run my app in dockerfile along with kafka and zookeeper.While it runs in docker container it is giving an error " connect ECONNREFUSED 127.0.0.1:9092","broker":"1"

Can anyone tell me how i can connect with kafka?

The app is running a basic index file which is producing messages in kafka with broker ["localhost":"9092"]

EDIT:Resolved using calling container name instead of url like i replace 'http://localhost:9092' with kafka:9092

1 Answers1

-2

In order your services to be able to talk to each other, they need to belong to the same network. By default, a container/service is fully isolated, unless you connect it explicitly.
In you compose, they "depend" on each other, but are not connected in any way.

You need to create a network (from within the docker-compose itself, or as an external network : it depends wether it is devoted to this very stack or it is going to be shared with other stacks).

Once this network is added, each service will then have to connect to it

services:
  serviceA:
   ....
     networks:
      - mynetwork
  serviceB:
   ...
     networks:
      - mynetwork
     depends_on: 
      - serviceA

networks:
  
networks:
   mynetwork:
     external: true

From now, each service can connect to the other through <service_name>:<inside_port> with inside port being the native port (not the one defined on the left part of - '9093:9093', though this would not be a pb since they are the same here)

Marvin
  • 1,650
  • 4
  • 19
  • 41
  • 1
    You don't need to do this; Compose creates a network named `default` for you. [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation describes this setup. (In most cases I'd recommend _deleting_ all of the `networks:` blocks you show here in the name of simplicity.) – David Maze Mar 15 '22 at 11:01
  • @DavidMaze So how can i change my file nothing seems to be working.I am calling localhost:9092 in my api file to connect with kafka which is running on port 9092 localhost – Divyanshu Sen Mar 15 '22 at 11:29
  • 1
    They're different `localhost`s (in the same way the Stack Overflow Web server you're posting to believes it's `localhost` as well). Please read the "Networking in Compose" link in my previous comment. – David Maze Mar 15 '22 at 13:21
  • Kafka requires more setup than a simple network bridge. See duplicate post – OneCricketeer Mar 15 '22 at 14:13
  • Resolved via fixing the ip of host and then connecting to that ip @DavidMaze – Divyanshu Sen Mar 16 '22 at 06:19