Multiple processes in one container
Often I need multiple processes in one container. For example, one instance of Kafka for development purpose or one instance of Elasticsearch — if you develop services in a micro service architecture you know my problem! — but the latest versions of Kafka or Elasticsearch need an instance of Zookeeper to start. It’s very frustrating that you need a docker-compose only to make a test!
In most cases you don’t need a cluster to test your work, but only a single broker (for example), so I have created a docker image containing Zookeeper and Kafka (version 2.12-2.3.0) together, the container starts one instance of supervisor http://supervisord.org/ that manage the processes life.
I’ll show you how to do it step by step:
Download a stable version of Kafka from https://kafka.apache.org/, then:
- In the first line I want touse adoptopenjdk as base image because it is debian based (I want to use apt) and it has a valid openjdk already installed
- COPY the start.sh file in the root (details below)
- An apt update it’s needed to synchronize the package manager repositories
- Then install supervisor and add a copy of Kafka as is and copy the following supervisor.conf file
The start.sh manages the environment variable KAFKA_ADVERTISED_LISTNERS used to configure the Kafka advertise url.
At the end of start.sh, it starts the supervisor daemon with the following configuration:
Supervisor starts Zookeeper and one Kafka broker: that’s all.
The container is now running with a valid instance of Zookeeper and Kafka, you can bind the standard ports to interact with Kafka and Zookeeper.
Build
docker build -t paspaola/kafka-one-container .
Run
docker run -it -p2181:2181 -p9092:9092 -e KAFKA_ADVERTISED_LISTNERS={your-host-address}” paspaola/kafka-one-container