Docker Compose

Creating YAML file and using the command docker-compose makes it easy to download and run images with required configuration or run multiple images at once.

Get docker-compose: https://docs.docker.com/compose/install/

I had a tech stack in which I had to use ElasticSearch for db , kafka for message processing and zookeeper for maintaining configuration and synchronization of distributed systems.So here is the sample YAML file :

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.3.0
    ports:
      - "9200:9200"
    environment:
      - "discovery.type=single-node"

version : depicts the version number of docker-compose (latest is 3).
services : since version 2 all of the required images with their key calue has to be under services.
zookeeper,kafka,elasticsearch : key value for the respective images.
image: the name of the image from where it is getting downloaded.
ports:assigning the host port number to container port number so that it can be accessible from the host.
environment: setting up the environment variables for the images.
volumes: mapping of the required directory in the host machine to the container machine directory.

After writing and saving the file as .yml file move to the respective folder where it is saved and run
docker-compose up

Running docker ps command will show all the running containers:

For more info: https://docs.docker.com/compose/