I use VS Code with the remote-containers extension.
I have 2 project folders for with each has it's own docker-compose file.
- zookeeper/kafka (listening to port 9092)
- python dev container (running as a remote-container)
In the python container I have a producer that sends messages to the kafka container. Unfortunately I cannot reach the kafka container from within the python container. I get the following error:
kafka.errors.NoBrokersAvailable: NoBrokersAvailable
(If I run the producer directly from the host then it works like a charm!)
How can I connect from the python remote-container to the kafka container?
I tried port forwarding and publishing in the devcontainer.json without success:
"forwardPorts": [9092]
"appPort": [9092]
I even tried temporary port forwarding with
Forward a port >> 9092
I'm a docker beginner so I guess I have a misconception here, I would highly appreciate if someone could help me out.
Thanks!
carlo
devcontainer.json
{
"name": "python",
"dockerComposeFile": ["../docker-compose.yml"],
"service": "python",
"workspaceFolder": "/workspace",
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"python.pythonPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintPath": "/usr/local/bin/pylint",
"python.formatting.autopep8Path": "/usr/local/bin/autopep8",
"python.formatting.blackPath": "/usr/local/bin/black"
},
"extensions": [
"ms-python.python",
"coenraads.bracket-pair-colorizer",
"wayou.vscode-todo-highlight"
],
// forward ports
"forwardPorts": [9092],
// publish ports to the host
"appPort": [9092]
}
producer.py
from kafka import KafkaProducer
import json
from data import get_registered_user
import time
def json_serializer(data):
return json.dumps(data).encode('utf-8')
producer = KafkaProducer(
bootstrap_servers=['localhost:9092'],
value_serializer=json_serializer,
)
if __name__ == "__main__":
while True:
registered_user = get_registered_user()
producer.send('registered_user', registered_user)
print(registered_user)
time.sleep(3)