title: How to ensure unique broker IDs in a docker kafka service
body: <p>I want to create a scalable kafka cluster, but each kafka instance needs a unique broker ID. What is a good way to ensure this? </p>
  
  <p>Below you can find the compose file I am using to deploy my application. I am using <code>docker stack deploy --compose-file docker-compose.yml mystack</code> to deploy it to my docker swarm. Currently the broker ID is set to 1, but this will not work when I increase the number of kafka instances from one to two with e.g. <code>docker service scale &lt;KAFKA-SERVICE-ID&gt;=2</code>, and that is the problem that I am trying to solve.</p>
  
  <pre><code>---
  version: '3'
  services:
    zookeeper:
      image: confluentinc/cp-zookeeper:latest
      environment:
        ZOOKEEPER_SERVER_ID: 1
        ZOOKEEPER_CLIENT_PORT: 22181
        ZOOKEEPER_TICK_TIME: 2000
        ZOOKEEPER_INIT_LIMIT: 5
        ZOOKEEPER_SYNC_LIMIT: 2
        ZOOKEEPER_SERVERS: localhost:22888:23888
      network_mode: host
      extra_hosts:
        - "moby:127.0.0.1"
    kafka:
      image: confluentinc/cp-kafka:latest
      network_mode: host
      depends_on:
        - zookeeper
      environment:
        KAFKA_BROKER_ID: 1
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:22181
        KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:19092
      extra_hosts:
        - "moby:127.0.0.1"
  </code></pre>