简体   繁体   English

Docker:如何连接到 docker-compose.yml 文件中未定义的 kafka 容器

[英]Docker: How to connect to a kafka container which is not defined in docker-compose.yml file

I have 3 docker-compose files.我有 3 个 docker-compose 文件。 One to start the kafka and the other two are consumer and producer.一个启动kafka,另外两个是消费者和生产者。 Added external_links in the other docker-compose files to kafka, but still unable to access kafka from inside containers.将其他docker-compose文件中的external_links添加到kafka,但仍然无法从容器内部访问kafka。 From outside the container, I can access through localhost:9092, but what about inside docker container.从容器外部,我可以通过 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

First, remove these, they are deprecated首先,删除这些,它们已被弃用

  - KAFKA_ADVERTISED_HOST_NAME=localhost
  - KAFKA_ADVERTISED_PORT=9092

Second, read the bitnami image documentation more carefully, all the Kafka properties start with KAFKA_CFG_ , then read the section about internal/external listeners其次,更仔细地阅读 bitnami 图像文档,所有 Kafka 属性都以KAFKA_CFG_ ,然后阅读有关内部/外部侦听器的部分

The linked answer(s) are correct Communication between multiple docker-compose projects链接的答案是正确的多个 docker-compose 项目之间的通信

Run docker.network create with a name to setup an external bridge.network separately from Compose, then add networks section to each service in that.network (Zookeeper, Kafka, and your Kafka clients).使用名称运行docker.network create以设置外部 bridge.network 与 Compose 分开,然后将networks部分添加到该网络中的每个服务(Zookeeper、Kafka 和您的 Kafka 客户端)。 Then make sure it's external然后确保它是外部的

networks:
  example-net:
    external: true

Then you'd use kafka:29092 in your apps, not localhost, and not port 9092然后你会在你的应用程序中使用kafka:29092 ,而不是本地主机,而不是端口 9092

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM