Docker Compose

What is Docker Compose?

Docker Compose is a tool used to define and manage multi-container applications with a single configuration file (docker-compose.yml). It helps in:
✅ Running multiple containers as a service.
✅ Automating container networking.
✅ Simplifying deployment with one command.

Why Use Docker Compose?

✔ Easy Setup – Define all services in a single YAML file.
✔ Reproducibility – Ensures consistency across environments.
✔ Dependency Management – Starts containers in the correct order.

Installing Docker Compose

Docker Compose comes pre-installed with Docker Desktop. To check the version:

docker-compose –version

If not installed, install it using:

sudo apt install docker-compose  # Linux (Ubuntu/Debian)

Basic Structure of docker-compose.yml

A simple Docker Compose file for a web app using Nginx and a Node.js backend:

yaml

version: ‘3’

services:
web:
image: nginx
ports:
– “8080:80”
depends_on:
– app

app:
build: .
ports:
– “3000:3000”
environment:
– NODE_ENV=production

Understanding the File

  version – Defines the Docker Compose file format.

•  services – Lists all containers (web and app in this case).

•  image – Pulls a pre-built image from Docker Hub (nginx).

•  build – Uses the local Dockerfile to build the app container.

•  ports – Maps container ports to the host machine.

•  depends_on – Ensures web starts only after app is running.

Using Docker Compose

1️. Start Services

Run all containers defined in docker-compose.yml:

docker-compose up -d

🔹 The -d flag runs containers in the background.

2️. List Running Containers

docker-compose ps

3️. Stop All Containers

docker-compose down

4️. Restart Services

docker-compose restart

5️. View Logs of a Service

docker-compose logs -f app

Conclusion

  Docker Compose simplifies managing multiple containers.
•  Uses a YAML file (docker-compose.yml) for configuration.
  Easily start, stop, and manage services with simple commands.

Chatbot