The Lunchbox Analogy
Planning lunch:
| Approach | Outcome |
|---|---|
| Without lunchbox | Loose rice, sandwich, soup... messy, might spill |
| With lunchbox | Everything neatly packaged, easy to transport |
Docker is a lunchbox for your application. It packages everything your app needs into one portable unit.
What Is Docker?
Docker is a platform for working with containers:
| Capability | Tool |
|---|---|
| Building containers | Dockerfile |
| Running containers | docker run |
| Sharing containers | Docker Hub |
| Managing multi-container apps | Docker Compose |
Docker is one of the most popular tools for working with containers.
Core Concepts
Dockerfile
A recipe for building an image:
| Instruction | Purpose |
|---|---|
FROM node:lts | Start with a Node.js base image |
WORKDIR /app | Set working directory |
COPY package*.json . | Copy dependency list |
RUN npm install | Install dependencies |
COPY . . | Copy application code |
EXPOSE 3000 | Document the port |
CMD ["npm", "start"] | Command to run |
Build an image: docker build -t my-app:dev .
Image
A packaged snapshot:
| Contains | Example |
|---|---|
| Base OS | Alpine Linux (minimal) |
| Runtime | Node.js, Python, Java |
| Dependencies | npm packages, pip modules |
| Your code | app.js, main.py |
| Configuration | Environment setup |
Images are treated as immutable â you typically rebuild a new image when something changes.
Container
A running instance of an image:
| Analogy | Meaning |
|---|---|
| Image : Container | Class : Object |
| Recipe : Dish | Blueprint : Building |
Containers are isolated processes with their own filesystem and network.
Registry
Storage for images (like GitHub for code):
| Registry | Type |
|---|---|
| Docker Hub | Public |
| AWS ECR | Private |
| Google GCR | Private |
| Azure ACR | Private |
Common Docker Commands
Building
| Command | Purpose |
|---|---|
docker build -t my-app:dev . | Build image from Dockerfile |
-t | Tag (name:version) |
. | Build context (current directory) |
Running
| Command | Purpose |
|---|---|
docker run my-app:dev | Run a container |
docker run -p 8080:3000 my-app | Map host:container port |
docker run -d my-app | Run detached (background) |
docker run -e API_KEY=xxx my-app | Set environment variable |
Managing
| Command | Purpose |
|---|---|
docker ps | List running containers |
docker ps -a | List all containers |
docker stop [id] | Stop a container |
docker rm [id] | Remove a container |
docker logs [id] | View container logs |
Images
| Command | Purpose |
|---|---|
docker images | List local images |
docker pull nginx:stable | Download from registry |
docker push my-repo/my-app:dev | Upload to registry |
docker rmi [id] | Remove an image |
Docker Compose
For multi-container applications:
Problem: Application needs: web server + database + cache = 3 containers that work together.
Solution: Define everything in docker-compose.yaml:
services:
web:
build: .
ports:
- "3000:3000"
db:
image: postgres:stable
environment:
- POSTGRES_PASSWORD=secret
cache:
image: redis:stable
| Command | Purpose |
|---|---|
docker compose up | Start all services |
docker compose down | Stop all services |
docker compose logs | View all logs |
Volumes: Persistent Data
The Problem
Container deleted â Data gone!
| Without Volume | With Volume |
|---|---|
| Database in container | Mount volume to host |
| Container crashes | Data persists |
| All data lost! | Data survives |
Solution
| Volume Type | Usage |
|---|---|
| Named volume | docker volume create mydata |
| Bind mount | -v /host/path:/container/path |
| Anonymous volume | Auto-created, harder to manage |
Named volumes are the recommended approach for production data.
Multi-Stage Builds
Reduce image size by separating build from runtime:
# Stage 1: Build
FROM node:lts AS builder
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build
# Stage 2: Production
FROM node:lts-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/index.js"]
| Stage | Contents | Size |
|---|---|---|
| Builder | Source, dev deps, build tools | Large |
| Production | Mostly compiled output | Minimal |
Common Mistakes
1. Not Using .dockerignore
| â Include | â Exclude |
|---|---|
| node_modules | node_modules/ |
| .git | .git/ |
| temp files | *.log |
Smaller build context = faster builds.
2. Using "latest" Tag
| â Bad | â Better |
|---|---|
FROM node:latest | FROM node:<pinned-tag> |
| Unknown tag | Predictable, traceable |
3. Running as Root
Container process as root = security risk. Use non-root user:
USER node
CMD ["node", "app.js"]
4. Too Many Layers
Each RUN, COPY, ADD creates a layer. Combine when possible:
| â Bad | â Good |
|---|---|
| Multiple RUN commands | Combine with && |
| Many small layers | Fewer, logical layers |
Networking
Container Networks
| Mode | Description |
|---|---|
| Bridge | Default, containers on same network |
| Host | Container uses host's network |
| None | No network |
Connecting Containers
Containers on the same network can reach each other by name:
| From | To | Address |
|---|---|---|
| web container | db container | db:5432 |
| app | redis | cache:6379 |
Docker Compose creates a network automatically.
FAQ
Q: Docker vs container?
Docker is the platform. Container is the running unit. Docker builds and runs containers.
Q: Docker vs virtual machines?
| VMs | Docker |
|---|---|
| Full guest OS | Shares host kernel |
| GBs in size | MBs in size |
| Minutes to start | Seconds to start |
| Stronger isolation | Process-level isolation |
Q: Do I need Docker Compose?
For single containers, no. For multi-container apps, highly recommended.
Q: How do I choose a base image?
Alpine for smallest size. Official language images for compatibility. Check security scans.
Summary
Docker is the platform that builds, runs, and manages containers, making application deployment consistent and portable.
Key Takeaways:
- Dockerfile defines how to build an image
- Images are immutable snapshots
- Containers are running instances
- Docker Compose handles multi-container apps
- Volumes persist data beyond container lifecycle
- Use specific version tags, not
latest - Run as non-root for security
- Multi-stage builds reduce image size
Docker helped make containers much more accessible.
Related Concepts
Leave a Comment
Comments (0)
Be the first to comment on this concept.
Comments are approved automatically.