Run a Pulsar cluster locally with Docker Compose

To run Pulsar loccally with Docker Compose, follow the steps below.

Step 1: Configure the compose.yml template

To get up and run a Pulsar cluster quickly, you can use the following template to create a compose.yml file by modifying or adding the configurations in the environment section.

  1. version: '3'
  2. networks:
  3. pulsar:
  4. driver: bridge
  5. services:
  6. # Start zookeeper
  7. zookeeper:
  8. image: apachepulsar/pulsar:latest
  9. container_name: zookeeper
  10. restart: on-failure
  11. networks:
  12. - pulsar
  13. volumes:
  14. - ./data/zookeeper:/pulsar/data/zookeeper
  15. environment:
  16. - metadataStoreUrl=zk:zookeeper:2181
  17. - PULSAR_MEM=-Xms256m -Xmx256m -XX:MaxDirectMemorySize=256m
  18. command: >
  19. bash -c "bin/apply-config-from-env.py conf/zookeeper.conf && \
  20. bin/generate-zookeeper-config.sh conf/zookeeper.conf && \
  21. exec bin/pulsar zookeeper"
  22. healthcheck:
  23. test: ["CMD", "bin/pulsar-zookeeper-ruok.sh"]
  24. interval: 10s
  25. timeout: 5s
  26. retries: 30
  27. # Init cluster metadata
  28. pulsar-init:
  29. container_name: pulsar-init
  30. hostname: pulsar-init
  31. image: apachepulsar/pulsar:latest
  32. networks:
  33. - pulsar
  34. command: >
  35. bin/pulsar initialize-cluster-metadata \
  36. --cluster cluster-a \
  37. --zookeeper zookeeper:2181 \
  38. --configuration-store zookeeper:2181 \
  39. --web-service-url http://broker:8080 \
  40. --broker-service-url pulsar://broker:6650
  41. depends_on:
  42. zookeeper:
  43. condition: service_healthy
  44. # Start bookie
  45. bookie:
  46. image: apachepulsar/pulsar:latest
  47. container_name: bookie
  48. restart: on-failure
  49. networks:
  50. - pulsar
  51. environment:
  52. - clusterName=cluster-a
  53. - zkServers=zookeeper:2181
  54. - metadataServiceUri=metadata-store:zk:zookeeper:2181
  55. # otherwise every time we run docker compose uo or down we fail to start due to Cookie
  56. # See: https://github.com/apache/bookkeeper/blob/405e72acf42bb1104296447ea8840d805094c787/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Cookie.java#L57-68
  57. - advertisedAddress=bookie
  58. - BOOKIE_MEM=-Xms512m -Xmx512m -XX:MaxDirectMemorySize=256m
  59. depends_on:
  60. zookeeper:
  61. condition: service_healthy
  62. pulsar-init:
  63. condition: service_completed_successfully
  64. # Map the local directory to the container to avoid bookie startup failure due to insufficient container disks.
  65. volumes:
  66. - ./data/bookkeeper:/pulsar/data/bookkeeper
  67. command: bash -c "bin/apply-config-from-env.py conf/bookkeeper.conf && exec bin/pulsar bookie"
  68. # Start broker
  69. broker:
  70. image: apachepulsar/pulsar:latest
  71. container_name: broker
  72. hostname: broker
  73. restart: on-failure
  74. networks:
  75. - pulsar
  76. environment:
  77. - metadataStoreUrl=zk:zookeeper:2181
  78. - zookeeperServers=zookeeper:2181
  79. - clusterName=cluster-a
  80. - managedLedgerDefaultEnsembleSize=1
  81. - managedLedgerDefaultWriteQuorum=1
  82. - managedLedgerDefaultAckQuorum=1
  83. - advertisedAddress=broker
  84. - advertisedListeners=external:pulsar://127.0.0.1:6650
  85. - PULSAR_MEM=-Xms512m -Xmx512m -XX:MaxDirectMemorySize=256m
  86. depends_on:
  87. zookeeper:
  88. condition: service_healthy
  89. bookie:
  90. condition: service_started
  91. ports:
  92. - "6650:6650"
  93. - "8080:8080"
  94. command: bash -c "bin/apply-config-from-env.py conf/broker.conf && exec bin/pulsar broker"

Step 2: Create a Pulsar cluster

As preparation, create data directories and change the data directory ownership to uid(10000) which is the default user id used in the Pulsar Docker container.

  1. sudo mkdir -p ./data/zookeeper ./data/bookkeeper
  2. # this step might not be necessary on other than Linux platforms
  3. sudo chown -R 10000 data

To create a Pulsar cluster by using the compose.yml file, run the following command.

  1. docker compose up -d

Step 3: Destroy the Pulsar cluster

If you want to destroy the Pulsar cluster with all the containers, run the following command. It will also delete the network that the containers are connected to.

  1. docker compose down