Docker Compose for Managing Multiple Containers

Docker Compose is an essential tool for effectively managing multi-container-based applications. With Docker Compose, you can effortlessly set up and deploy multiple containers, making it a valuable asset for your containerized projects. This guide will walk you through the key concepts and commands related to Docker Compose.

What is Docker Compose?

  1. Docker Compose is a tool used to manage multi-container-based applications.

  2. With Docker Compose, you can easily set up and deploy multiple containers.

  3. To provide container information to Docker Compose, you need to use a "docker-compose.yml" file.

  4. The Docker Compose YAML file should contain all the information related to container creation.

Docker Compose YAML File

Below is an example of a Docker Compose YAML file, which serves as the blueprint for your containerized application:

version: 
services:
network:
volumes:

Installing Docker Compose

To get started with Docker Compose, follow these installation steps:

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

# Grant execution permission
$ sudo chmod +x /usr/local/bin/docker-compose

# Verify the installation
$ docker-compose --version

Deploying Spring Boot + MySQL with Docker Compose

Here's how you can deploy a Spring Boot application along with a MySQL database using Docker Compose:

# Clone the project repository
$ git clone https://github.com/Pankaj-Surya/spring-boot-mysql-docker-compose

# Navigate to the project directory
$ cd spring-boot-mysql-docker-compose

# Build the application with Maven
$ mvn clean package

# Build a Docker image for the Spring Boot application
$ docker build -t spring-boot-mysql-app .

# List Docker images
$ docker images

# Start the application and database containers
$ docker-compose up -d

# Access the application at
# URL: http://ec2-public-ip:8080/

Output :

Here are the commands for checking the app container logs, connecting to the DB container, and performing various MySQL-related tasks

# Check application container logs
$ docker logs <app-container-name>

# Connect to the database container
$ docker exec -it <db-container-name> /bin/bash

# Connect to the MySQL database using the MySQL client
$ mysql -u root -p

# Display available databases in MySQL
$ show databases

# Select the database (sbms is our database name)
$ use sbms

# Display tables created in the database
$ show tables

# Display data from a specific table (e.g., 'book' is our table name)
$ select * from book;

# Exit from the MySQL database
$ exit

# Exit from the container
$ exit

Docker Compose Commands

Here are some essential Docker Compose commands to manage your containers:

# Create and start containers defined in the Docker Compose file
$ docker-compose up

# Create and start containers using a different file name
$ docker-compose -f <filename> up

# Run containers in detached mode
$ docker-compose up -d

# Display information about containers created by Docker Compose
$ docker-compose ps

# Display Docker images used by the Compose file
$ docker-compose images

# View container logs in real-time
$ docker logs -f <container-name>

# Stop and remove containers defined in the Docker Compose file
$ docker-compose down

This comprehensive guide covers the fundamentals of Docker Compose, from installation to deploying applications. Docker Compose simplifies the management of multiple containers, making it an invaluable tool for container orchestration.