Docker: 如何连接到 docker-compose.yml 文件中未定义的 kafka 容器
Docker: How to connect to a kafka container which is not defined in docker-compose.yml file
我有 3 个 docker-compose 文件。一个启动kafka,另外两个是消费者和生产者。在其他 docker-compose 文件中添加了 external_links 到 kafka,但仍然无法从容器内部访问 kafka。从容器外部,我可以通过 localhost:9092 访问,但是在 docker 容器内部呢?
# docker-compose1.yml
version: "3.6"
services:
zookeeper:
image: 'docker.io/bitnami/zookeeper:3.7'
container_name: zookeeper
ports:
- '2181:2181'
volumes:
- 'zookeeper_data:/bitnami'
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: 'docker.io/bitnami/kafka:3'
container_name: kafka
ports:
- '9092:9092'
volumes:
- 'kafka_data:/bitnami'
environment:
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_LISTENERS=PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
- KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
- KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_ADVERTISED_HOST_NAME=localhost
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_AUTO_CREATE_TOPICS_ENABLE=true
depends_on:
- zookeeper
volumes:
zookeeper_data:
external: true
kafka_data:
external: true
# docker-compose2.yml
version: "3.6"
services:
web:
hostname: ocp-transmitter
image: 'ocp/transmitter'
command: bash -c "bundle install && foreman start"
ports:
- '3000:3000'
volumes:
- .:/app:cached
stdin_open: true
tty: true
external_links:
- kafka
首先,删除这些,它们已被弃用
- KAFKA_ADVERTISED_HOST_NAME=localhost
- KAFKA_ADVERTISED_PORT=9092
其次,更仔细地阅读bitnami图像文档,所有Kafka属性都以KAFKA_CFG_
开头,然后阅读关于internal/external listeners
的部分
链接的答案正确
运行 docker network create
用一个名称来设置一个独立于 Compose 的外部桥接网络,然后将 networks
部分添加到该网络中的每个服务(Zookeeper、Kafka 和你的 Kafka客户)。然后确保它是外部的
networks:
example-net:
external: true
然后你会在你的应用程序中使用 kafka:29092
,而不是本地主机,而不是端口 9092
我有 3 个 docker-compose 文件。一个启动kafka,另外两个是消费者和生产者。在其他 docker-compose 文件中添加了 external_links 到 kafka,但仍然无法从容器内部访问 kafka。从容器外部,我可以通过 localhost:9092 访问,但是在 docker 容器内部呢?
# docker-compose1.yml
version: "3.6"
services:
zookeeper:
image: 'docker.io/bitnami/zookeeper:3.7'
container_name: zookeeper
ports:
- '2181:2181'
volumes:
- 'zookeeper_data:/bitnami'
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: 'docker.io/bitnami/kafka:3'
container_name: kafka
ports:
- '9092:9092'
volumes:
- 'kafka_data:/bitnami'
environment:
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_LISTENERS=PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
- KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
- KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_ADVERTISED_HOST_NAME=localhost
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_AUTO_CREATE_TOPICS_ENABLE=true
depends_on:
- zookeeper
volumes:
zookeeper_data:
external: true
kafka_data:
external: true
# docker-compose2.yml
version: "3.6"
services:
web:
hostname: ocp-transmitter
image: 'ocp/transmitter'
command: bash -c "bundle install && foreman start"
ports:
- '3000:3000'
volumes:
- .:/app:cached
stdin_open: true
tty: true
external_links:
- kafka
首先,删除这些,它们已被弃用
- KAFKA_ADVERTISED_HOST_NAME=localhost
- KAFKA_ADVERTISED_PORT=9092
其次,更仔细地阅读bitnami图像文档,所有Kafka属性都以KAFKA_CFG_
开头,然后阅读关于internal/external listeners
链接的答案正确
运行 docker network create
用一个名称来设置一个独立于 Compose 的外部桥接网络,然后将 networks
部分添加到该网络中的每个服务(Zookeeper、Kafka 和你的 Kafka客户)。然后确保它是外部的
networks:
example-net:
external: true
然后你会在你的应用程序中使用 kafka:29092
,而不是本地主机,而不是端口 9092