How to create topics in an Apache Kafka Docker image with docker-compose?

One of the easy solutions is to create a side container that will start after the broker and create the relevant topics. You can express the dependencies with depends_on and healthcheck operations:

version: '3.1'
services:
  kafka:
	image: 'bitnami/kafka:3.5' 
	ports:
  	- '9092:9092'
  	- '9094:9094'
	environment:
  	# KRaft settings
  	- KAFKA_CFG_NODE_ID=0
  	- KAFKA_CFG_PROCESS_ROLES=controller,broker
  	- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093
  	# Listeners
  	- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093,EXTERNAL://:9094
  	- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092,EXTERNAL://localhost:9094
  	- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,EXTERNAL:PLAINTEXT,PLAINTEXT:PLAINTEXT
  	- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
  	- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=PLAINTEXT
  	- KAFKA_DELETE_TOPIC_ENABLE=true
	healthcheck:
  	test: ["CMD", "kafka-topics.sh", "--bootstrap-server=localhost:9092", "--list"]
  	start_period: 15s
  	interval: 10s
  init-kafka:
	image: bitnami/kafka:3.5
	depends_on:
  	kafka:
    	condition: service_healthy
	entrypoint: [ '/bin/sh', '-c' ]
	command: |
  	"
  	# blocks until kafka is reachable
  	kafka-topics.sh --bootstrap-server kafka:9092 --list
  	echo -e 'Creating Kafka topics'
  	kafka-topics.sh --bootstrap-server kafka:9092 --create --if-not-exists --topic topic1 --replication-factor 1 --partitions 2

  	echo -e 'Successfully created the following topics:'
  	kafka-topics.sh --bootstrap-server kafka:9092 --list
  	"