Arbiteria Logo
Docker & Docker Compose for Coders
Zero2AI Logo

🚒 The Container Challenge

Imagine you're shipping an app to production. Your code runs perfectly on your laptop, but it crashes on the server. Sound familiar?

Quick Experiment: Click the Containers

Click each container below to see what happens when dependencies aren't isolated:

πŸ“¦
App A
Python 3.8
πŸ“¦
App B
Python 3.11
πŸ–₯️
Shared Server
??? Python

πŸ“š What's in this lesson

  • Docker fundamentals & commands
  • Docker Compose architecture
  • Key differences & use cases
  • Real-world decision framework
  • Hands-on examples

πŸ’‘ Why this matters (WIIFM)

  • Eliminate "works on my machine"
  • Deploy faster with confidence
  • Manage complex multi-service apps
  • Industry-standard skillset
  • Simplify dev environments

What is Docker? 🐳

Docker is a platform that packages your application and all its dependencies into standardized, isolated units called containers.

Container Anatomy

πŸ“ Your Application Code
πŸ“š Dependencies & Libraries
βš™οΈ Runtime Environment
= 🐳 Complete Docker Container

Key Concept: Containers are lightweight, portable, and consistent across any environmentβ€”laptop, test server, or production cloud.

Essential Docker Commands

# Pull an image from Docker Hub docker pull nginx:latest # Run a container from an image docker run -d -p 8080:80 --name my-nginx nginx:latest # List running containers docker ps # Stop and remove a container docker stop my-nginx && docker rm my-nginx

What is Docker Compose? 🎼

Docker Compose is a tool for defining and running multi-container Docker applications using a single YAML file.

Compose Orchestration

πŸ“„ docker-compose.yml
↓
🌐 Web App
πŸ—„οΈ Database
⚑ Cache

Key Concept: Compose lets you define your entire application stackβ€”web servers, databases, caches, queuesβ€”in one file and launch everything with one command.

Sample docker-compose.yml

1 version: '3.8'
2 services:
3 web:
4 image: nginx:latest
5 ports: ["8080:80"]
6 db:
7 image: postgres:14
# Start all services with one command docker compose up -d

Knowledge Check 1

What is the primary purpose of Docker?

Docker vs. Docker Compose: Key Differences πŸ”

🐳 Docker (CLI)

πŸ“¦
Single Container
Manual commands for each service

🎼 Docker Compose

πŸ“¦πŸ“¦πŸ“¦
Multi-Container
One YAML file, one command
Aspect Docker Docker Compose
Scope Single container management Multi-container orchestration
Configuration Command-line arguments YAML file (declarative)
Networking Manual network setup Automatic network creation
Best For Simple apps, single services Complex multi-service stacks

Important: Docker Compose uses Docker under the hood. It's not a replacementβ€”it's an abstraction layer that simplifies managing multiple containers.

Real-World Example: Blog Platform πŸ“

Let's compare how you'd run a typical three-tier blog application (web + database + cache).

❌ Docker: Multiple Commands

docker network create blog-net docker run -d --name db ... docker run -d --name cache ... docker run -d --name web ...
4 separate commands to remember

βœ… Compose: Single Command

services: web: ... db: ... cache: ...
docker compose up

Real Impact: Docker Compose automatically creates networks, manages startup order with depends_on, and lets services reference each other by name. What takes 10+ lines of Docker commands becomes 15 lines of declarative YAML.

Knowledge Check 2

When running multiple related containers (e.g., web app + database + cache), what is a key advantage of using Docker Compose?

Decision Framework: When to Use What? 🎯

How many services does your app need?
↓
Just One
↓
🐳
Use Docker
Multiple
↓
🎼
Use Compose

Use Docker When:

  • βœ“ Running a single container
  • βœ“ Learning Docker fundamentals
  • βœ“ Testing individual microservices

Use Docker Compose When:

  • βœ“ Managing multiple services
  • βœ“ Setting up dev environments
  • βœ“ Onboarding new team members

Knowledge Check 3

Your team is building a web app with React frontend, Node.js API, PostgreSQL database, and Redis cache. Which tool is most appropriate for local development?

Powerful Docker Compose Features πŸš€

πŸ’Ύ
Volume Persistence

Preserve data even when containers restart

πŸ”
Environment Variables

Manage configs and secrets safely

πŸ’š
Health Checks

Ensure services are ready before connecting

Volume Persistence Example

1 services:
2 db:
3 image: postgres:14
4 volumes:
5 - db-data:/var/lib/postgresql/data

Real Impact: These features eliminate hours of manual setup, making complex environments reproducible in seconds.

Knowledge Check 4

What does the depends_on field accomplish in a docker-compose.yml file?

Common Pitfalls & Best Practices ⚠️

❌ Common Mistakes

Using Compose in production without orchestration (use Kubernetes/Swarm)
Hardcoding secrets in YAML files
Not using volumes for persistent data
Running containers as root user

βœ“ Best Practices

Use specific image tags (postgres:14.5 not :latest)
Keep Compose files in version control
Use .dockerignore to reduce build size
Implement health checks for all services

Industry Insight: According to Docker's 2023 survey, teams using Compose report 3x faster onboarding for new developers.

Quick Command Reference πŸ“‹

🐳 Docker Commands

# Build & run docker build -t app:v1 . docker run -d -p 8080:80 app:v1 # Manage docker ps docker logs name docker stop name docker rm name

🎼 Compose Commands

# Build & run docker compose up -d docker compose up --build # Manage docker compose ps docker compose logs -f docker compose down

Note: Modern Docker uses docker compose (no hyphen). The older docker-compose command is being phased out.

Ready to Test Your Knowledge? 🎯

You've learned the fundamentals of Docker and Docker Compose! Now it's time to demonstrate your understanding.

Assessment Details

  • 5 multiple-choice questions
  • Real-world scenarios
  • Passing score: 80% (4/5 correct)
  • Certificate upon passing

Assessment Question 1 of 5

Which file format does Docker Compose use to define multi-container applications?

Assessment Question 2 of 5

You're joining a new team with an app that has 7 microservices. What's the best way to get the dev environment running on your laptop?

Assessment Question 3 of 5

What is the relationship between Docker and Docker Compose?

Assessment Question 4 of 5

Which scenario is BEST suited for using standard Docker commands rather than Docker Compose?

Assessment Question 5 of 5

In a docker-compose.yml file, what is the primary benefit of defining volumes for your database service?