Wikipedia defines Docker as

an open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualization on Linux.

What is a Docker Compose file?

Dockerfile is used to build a docker image and docker run command to run the application. While building a solution generally there will be many number of applications. What if there are hundred of them? Oh God... Please help the deployer of these applications. Docker-compose comes to our rescue. With a single command the tedious task of running each application can be done effortlessly. Docker-compose helps to start all the applications from the provided configuration. In this post we will learn from how to install docker-compose and finally use it.

Install docker-compose

The steps from 3 show how to install docker-compose. For those who already have installed the docker-compose but want to upgrade can follow from step 1.

Step 1: Check for the version and remove

	docker-compose --version

docker_version

	sudo apt-get remove docker-compose

docker_core

Step 2: Download and install

	sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

docker_core

Give all permissions

	sudo chmod +x /usr/local/bin/docker-compose
	sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
	docker-compose --version

dockercompose_permission

Creating the first docker-compose file

In my previous post I have mentioned how to create a hello world docker image.I will be using the same docker image in this example.

version: '2.0'
services:
studentmanager:
image: helloworld:1.0
Check for the hello world image using docker command. ``` docker images ``` images

Run docker-compose

Use this command to run the docker image. A default network is created in case we don't provide a network.

	docker-compose up

dockercompose_up

Run the docker-compose in a background mode

This command help to run the containers in a silent mode.

	docker-compose up -d

Stop the running containers

	docker-compose down

An Example

The following example shows a docker-compose file which has various services along with zookeeper and kafka.

version: '3'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
hostname: zookeeper
container_name: zookeeper
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
networks:
- local_network
kafka:
image: confluentinc/cp-kafka:latest
hostname: kafka
container_name: kafka
depends_on:
- zookeeper
ports:
- 9092:9092
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
networks:
- local_network
landoop:
image: landoop/schema-registry-ui:latest
hostname: landoop
container_name: landoop
depends_on:
- schema-registry
ports:
- "8000:8000"
environment:
SCHEMAREGISTRY_URL: http://schema-registry:8081
PROXY: "true"
networks:
- local_network
schema-registry:
image: confluentinc/cp-schema-registry:5.3.0
hostname: schema-registry
container_name: schema-registry
depends_on:
- zookeeper
- kafka
ports:
- "8081:8081"
environment:
SCHEMA_REGISTRY_HOST_NAME: schema-registry
SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL: 'zookeeper:2181'
networks:
- local_network
studentmanager:
image: localhost:5000/student-manager-service:0.0.1
depends_on:
- kafka
- schema-registry
environment:
- KAFKA_BROKER=kafka
- KAFKA_INPUT_TOPIC=avro.topic2
- KAFKA_OUTPUT_TOPIC=avro.topic1
- SCHEMA_REGISTRY_URL=http://schema-registry:8081
ports:
- "9090:9090"
networks:
- local_network
admissionmanager:
image: localhost:5000/admission-manager-service:0.0.1
depends_on:
- kafka
- schema-registry
environment:
- KAFKA_BROKER=kafka
- KAFKA_INPUT_TOPIC=avro.topic1
- KAFKA_OUTPUT_TOPIC=avro.topic2
- SCHEMA_REGISTRY_URL=http://schema-registry:8081
ports:
- "7070:7070"
networks:
- local_network
networks:
local_network:
external: true

If you have any question or feedback, please do reach out to me by commenting below.