Kafka producer not working in Docker container

I've created simple Kafka app that sends message to a topic. It works perfectly when I'm run it in local environment. But when I move it to Docker container it cannot connect to the broker. I think problem in container network settings but I cannot figure it out.

App code:

from kafka import KafkaProducer

producer = KafkaProducer(
    bootstrap_servers='xxxxxxxxxx.mdb.yandexcloud.net:9091',
    security_protocol="SASL_SSL",
    sasl_mechanism="SCRAM-SHA-512",
    sasl_plain_password='xxxxxxxxxx',
    sasl_plain_username='xxxxxxxxxx',
    ssl_cafile="YandexCA.crt",
    api_version=(0,11,5))

producer.send('test_topic', b'test message')
producer.flush()
producer.close()

Dockerfile:

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "app.py"]

So it's runs perfectly in terminal but fails in Docker. What can cause it?

Upvotes: 0

Views: 137

Answers (1)

So the problem was in the password. There was characters with escaping like:

ENV PASS=xxxxx\6xxxxx

And when set by env vars it worked correctly but when set in docker file it was escaped. So in Dockerfile I set it like that:

ENV PASS="xxxxx\6xxxxx"

And everything started working.

Upvotes: 1

Related Questions