Docker Compose — Complete Beginner to Intermediate Guide

🐳 Docker Compose — Complete Beginner to Intermediate Guide


1️⃣ What Is Docker Compose?

Docker Compose is a tool used to:

✅ Define and run multi-container Docker applications
✅ Manage everything using a single YAML file
✅ Start/Stop full application using one command

Instead of running multiple docker run commands manually, we use one configuration file.


2️⃣ Why Do We Need Docker Compose?

Without Compose:

docker run nginx
docker run mysql
docker run redis

With Compose:

docker compose up -d

Compose is:

  • Cleaner

  • Easier to maintain

  • Production-like structure

  • Version controlled (YAML file)


3️⃣ File Naming & Structure

✅ Default File Name

docker-compose.yml

OR

compose.yaml

Docker automatically detects these.


📁 Typical Project Structure

project-folder/

├── docker-compose.yml
├── Dockerfile (optional)
├── .env (optional)
└── app/

4️⃣ Basic Structure of docker-compose.yml

version: "3.8"

services:
web:
image: nginx
ports:
- "5001:80"

5️⃣ Mandatory vs Optional Fields

✅ Mandatory

These are required:

FieldDescription
services:Root section
At least one serviceRequired
image: OR build:Required inside service

Without these → Compose fails.


🟢 Optional (Commonly Used)

FieldPurpose
container_nameCustom name
portsPort mapping
volumesPersistent storage
environmentEnvironment variables
depends_onService start order
networksCustom networks
restartRestart policy
commandOverride default CMD

6️⃣ Real Example (Web + Database + Volume)

version: "3.8"

services:

web:
image: shaikwebsite:v1
container_name: shaik-web
ports:
- "5001:80"
depends_on:
- db
restart: always

db:
image: mysql:8
container_name: shaik-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: shaikdb
volumes:
- mysql_data:/var/lib/mysql

volumes:
mysql_data:

7️⃣ What Happens When You Run Compose?

When you execute:

docker compose up -d

Docker Compose:

  1. Reads YAML file

  2. Creates default network

  3. Creates defined volumes

  4. Pulls required images

  5. Builds images (if build: used)

  6. Starts containers

  7. Connects services via DNS


8️⃣ Basic Operations in Docker Compose


▶️ Start Services

docker compose up

Detached mode:

docker compose up -d

🛑 Stop Services

docker compose stop

🔄 Restart Services

docker compose restart

Specific service:

docker compose restart web

❌ Remove Everything

docker compose down

Remove volumes also:

docker compose down -v

Remove images also:

docker compose down --rmi all

9️⃣ Build Related Commands

If using build::

docker compose build

Rebuild without cache:

docker compose build --no-cache

Build + Run:

docker compose up -d --build

🔎 10️⃣ Debugging & Monitoring


Check Running Services

docker compose ps

View Logs

docker compose logs

Follow logs:

docker compose logs -f

Specific service:

docker compose logs web

Execute Inside Container

docker compose exec web bash

1️⃣1️⃣ Scaling Services

docker compose up -d --scale web=3

Creates:

  • web_1

  • web_2

  • web_3

Used for stateless services.


1️⃣2️⃣ Volumes in Compose

Example:

volumes:
mysql_data:

Purpose:

  • Persistent storage

  • Data survives container deletion

Two types:

  1. Named volumes

  2. Bind mounts


1️⃣3️⃣ Networking in Compose

Compose automatically creates:

projectname_default

Services communicate using:

service_name:port

Example:

db:3306

No need to use IP address.


1️⃣4️⃣ Environment Variables (.env)

Create .env file:

PORT=5001

Use inside compose:

ports:
- "${PORT}:80"

Compose automatically loads .env.


1️⃣5️⃣ Common Real-World Workflow

docker compose pull
docker compose up -d
docker compose ps
docker compose logs -f
docker compose down

1️⃣6️⃣ When To Use Docker Compose?

Use Compose for:

  • Local development

  • Integration testing

  • CI/CD environments

  • Small multi-container setups

For large production systems → Kubernetes.


1️⃣7️⃣ Summary

Docker Compose allows you to:

  • Define multi-container applications

  • Manage networking automatically

  • Use persistent storage

  • Scale services

  • Debug easily

  • Version control infrastructure


🔥 Final DevOps Insight

Docker Compose is the bridge between:

docker run → docker compose → Kubernetes

If you want next blog section, we can write:

  • Deep Dive Into Volumes

  • Deep Dive Into Networking

  • Real Production-like Project Setup

Tell me what to document next 🚀

docker build and scaling, debug related commands in detail with commenting

Excellent Ghouse 🔥
Now we’ll document Docker Build + Scaling + Debug Commands clearly with explanations (blog-ready format).


🐳 Docker Build, Scaling & Debug — Deep Dive Guide


🧱 PART 1 — Docker Build (Image Creation)


1️⃣ Basic Build Command

docker build -t image_name:tag .

Example:

docker build -t shaikwebsite:v1 .

Explanation:

  • docker build → Create image

  • -t → Tag name

  • . → Current directory (build context)


2️⃣ What Is Build Context?

. means:

Docker can access only files inside this folder.

If file is outside → Docker cannot COPY it.


3️⃣ Build With Custom Dockerfile Name

docker build -t myimage:v1 -f MyDockerfile .
  • -f → Specify Dockerfile name


4️⃣ Build Without Cache

docker build --no-cache -t myimage:v1 .

Used when:

  • Dependencies changed

  • You want fresh install


5️⃣ Build With Build Arguments

Dockerfile:

ARG VERSION
RUN echo $VERSION

Build command:

docker build --build-arg VERSION=1.0 -t myimage:v1 .

6️⃣ Multi-Stage Build (Advanced)

Used to reduce image size.

Example:

FROM node:18 as builder
WORKDIR /app
COPY . .
RUN npm install && npm run build

FROM nginx
COPY --from=builder /app/build /usr/share/nginx/html

Purpose:

  • Keep final image small

  • Remove build tools


📈 PART 2 — Scaling Containers

Scaling means running multiple instances of same service.


🔹 Using Docker Run (Manual Scaling)

docker run -d -p 5001:80 --name web1 nginx
docker run -d -p 5002:80 --name web2 nginx

Manual and messy.


🔹 Using Docker Compose (Recommended)

docker compose up -d --scale web=3

This creates:

  • web_1

  • web_2

  • web_3

Important:

  • Works best for stateless apps

  • Needs load balancer for production


Check Scaled Containers

docker compose ps

🛠 PART 3 — Debug & Troubleshooting Commands

This is very important for DevOps.


🔍 1️⃣ Check Running Containers

docker ps

Show all (including stopped):

docker ps -a

📜 2️⃣ View Logs

docker logs container_name

Follow live logs:

docker logs -f container_name

Last 100 lines:

docker logs --tail 100 container_name

🧪 3️⃣ Enter Running Container

docker exec -it container_name bash

If bash not available:

docker exec -it container_name sh

🧠 4️⃣ Inspect Container Details

docker inspect container_name

Shows:

  • IP address

  • Mounts

  • Network

  • Ports

  • Environment variables


🌐 5️⃣ Check Port Mapping

docker port container_name

🧬 6️⃣ Check Resource Usage

docker stats

Shows:

  • CPU usage

  • Memory usage

  • Network IO

Very important in production debugging.


🔄 7️⃣ Restart Container

docker restart container_name

🧹 8️⃣ Remove Stopped Containers

docker container prune

🧱 9️⃣ Remove Unused Images

docker image prune

Remove everything unused:

docker system prune -a

🧩 1️⃣0️⃣ Check Networks

docker network ls

Inspect network:

docker network inspect network_name

🗂 1️⃣1️⃣ Check Volumes

docker volume ls

Inspect volume:

docker volume inspect volume_name

🔥 Compose Debug Commands


Logs (Compose)

docker compose logs

Follow:

docker compose logs -f

Execute Inside Service

docker compose exec web bash

Validate Compose File

docker compose config

Very important — checks YAML syntax.


🎯 Real Debug Workflow Example

If website not opening:

1️⃣ Check container running

docker ps

2️⃣ Check logs

docker logs web

3️⃣ Check port mapping

docker port web

4️⃣ Enter container

docker exec -it web bash

5️⃣ Check service inside container

curl localhost

🧠 Important DevOps Concepts

Container stops immediately?

Reason:

  • No foreground process running

Port not accessible?

Reason:

  • Missing -p mapping

  • Firewall issue

  • Wrong container port

Data lost after restart?

Reason:

  • No volume used


📌 Summary

Docker Build → Create image
Scaling → Run multiple instances
Debug → Logs + Inspect + Exec + Stats

These are core skills required for:

  • DevOps roles

  • SRE roles

  • Cloud engineers

  • Kubernetes understanding


🚀 Next Logical Deep Topics

1️⃣ Advanced Networking
2️⃣ Deep Dive into Volumes
3️⃣ Docker Security Best Practices
4️⃣ Docker in CI/CD


Tell me next topic, Ghouse 🔥

can we explicitly give name to containers in compose?

Yes ✅ Ghouse — we can explicitly give a name to containers in Docker Compose.


✅ How To Give Explicit Container Name

Use:

container_name: your_custom_name

🧱 Example

version: "3.8"

services:
web:
image: nginx
container_name: shaik-web
ports:
- "5001:80"

Now when you run:

docker compose up -d

Container name will be:

shaik-web

Instead of default:

projectname_web_1

🧠 What Happens If You Don’t Specify?

Compose automatically names containers as:

projectname_servicename_1

Example:

composeproject_web_1

⚠️ Important Limitation (Very Important)

If you use:

container_name: shaik-web

You CANNOT scale that service.

This will fail:

docker compose up -d --scale web=3

Because container names must be unique.


🎯 Best Practice

SituationRecommendation
Single containercontainer_name is fine
Scalable service❌ Avoid container_name
ProductionAvoid hardcoding container_name

🔥 DevOps Interview Insight

Question:

What happens if we define container_name and try to scale?

Answer:

Scaling fails because container names must be unique.


So yes, you can explicitly name containers — but use it carefully.


Next question for you 👇
Do you want to understand how Compose project names affect container naming? 🚀

Comments

Popular posts from this blog

Managing Amazon EBS Volumes and Snapshots Across Regions

Git for Beginners: Complete Guide from Installation to First Push on GitHub

AWS - Amazon Web Services