I'm trying to implement blackbox testing of my app. So I created compose file
version: '3.4'
services:
kafka:
image: "spotify/kafka"
environment:
- ADVERTISED_HOST=kafka
- ADVERTISED_PORT=9092
ports:
- "9092:9092"
- "2181:2181"
networks:
testing_net:
ipv4_address: 172.17.0.2
app:
build: ./blackbox
image: app
networks:
testing_net:
ipv4_address: 172.17.0.4
networks:
testing_net:
ipam:
driver: default
config:
- subnet: "172.17.0.0/16"
It works fine, application inside app container can connect to kafka using kafka:9092 host. But app is consumer, and producer is located outside of containers. When I try to populate data outside containers I get error:
[kafka-producer-network-thread | producer] WARN o.apache.kafka.clients.NetworkClient - [Producer clientId=producer] Error connecting to node kafka:9092 (id: 0 rack: null)
I could add "kafka" host to hosts file and it maybe will work, but I have to run this scenario not only at local machine but at CI\CD
I tried to set ADVERTISED_HOST=172.17.0.2. It doesn't work as well.
I can not edit any kafka config files, I can only use docker-compose file
Any suggestions would be appreciated. Thanks
UPD.
Solved this issue using dirty hack. For windows for sure, for other systems need to check Docker adds following host to hosts file: kubernetes.docker.internal:127.0.0.1 In that case inside container I use
kafka:
image: "spotify/kafka"
environment:
- ADVERTISED_HOST=kubernetes.docker.internal
- ADVERTISED_PORT=9092
ports:
- "9092:9092"
- "2181:2181"
networks:
testing_net:
ipv4_address: 172.16.238.102
extra_hosts:
- "kubernetes.docker.internal:172.16.238.102"
And kafka broker works for outside application because kubernetes.docker.internal is known host