tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Container Management > Docker > How to use docker-compose for Multi-Container Applications

How to use docker-compose for Multi-Container Applications

Author: Venkata Sudhakar

Docker Compose is a tool for defining and running multi-container Docker applications using a single YAML configuration file called docker-compose.yml. In real-world development, an application typically needs multiple services running together - a Spring Boot API, a MySQL database, a Redis cache, and perhaps a Kafka broker. Without Compose, you would have to start each container manually with long docker run commands, managing ports, networks, and volumes by hand. Docker Compose lets you define all of these services, their configuration, and their relationships in one file, then start everything with a single command.

Compose creates a private network for all the services defined in the file, so they can communicate with each other using service names as hostnames. For example, if you define a service named mysql, your Spring Boot application can connect to it at jdbc:mysql://mysql:3306/appdb. Compose also handles startup order dependencies using the depends_on directive and manages persistent data using named volumes that survive container restarts.

The below example shows a complete docker-compose.yml for a Spring Boot application with MySQL, Redis, and Apache Kafka, covering all essential configuration options.


It gives the following output when you run the stack,

# Start all services in detached mode
docker compose up -d

[+] Running 6/6
  Container zookeeper  Started
  Container mysql      Started
  Container redis      Started
  Container kafka      Started
  Container myapp      Started

# Check all running services
docker compose ps

NAME        IMAGE                STATUS          PORTS
myapp       myapp:latest         Up 30s          0.0.0.0:8080->8080/tcp
mysql       mysql:8.0            Up 30s (healthy) 0.0.0.0:3306->3306/tcp
redis       redis:7-alpine       Up 30s          0.0.0.0:6379->6379/tcp
kafka       cp-kafka:7.5.0      Up 20s          0.0.0.0:9092->9092/tcp
zookeeper   cp-zookeeper:7.5.0  Up 25s          2181/tcp

The below example shows the most commonly used docker-compose commands for managing the lifecycle of the stack during development.


It gives the following output,

# docker compose logs -f app
myapp  | Started MyAppApplication in 3.2 seconds
myapp  | Flyway: Successfully applied 4 migrations to schema appdb
myapp  | Tomcat started on port(s): 8080 (http)

# docker compose down -v
[+] Running 7/7
  Container myapp      Removed
  Container kafka      Removed
  Container redis      Removed
  Container mysql      Removed
  Container zookeeper  Removed
  Volume mysql_data    Removed
  Volume redis_data    Removed

Docker Compose vs Docker Run:

A docker run command for the Spring Boot app alone would be: docker run -d -p 8080:8080 -e SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/appdb -e SPRING_DATASOURCE_USERNAME=appuser --network appnet --name myapp myapp:latest. Multiplied across 5 services with volumes, health checks, and environment variables, Compose reduces hundreds of lines of shell commands to a single well-documented YAML file that can be committed to source control and shared with every developer on the team.


 
  


  
bl  br