Intermediate · 10 lessons
Docker
Docker packages applications with all dependencies into portable containers. Containers share the host OS kernel but run in isolation — making deployment consistent from your laptop to production servers and cloud platforms.
Key Concepts
Key Takeaways
- ✓ Containers are lighter than VMs — start in seconds, use less resources.
- ✓ Dockerfile defines how to build an image; docker-compose runs multi-container apps.
- ✓ Use volumes for persistent data — containers are ephemeral by design.
- ✓ Never run containers as root in production; scan images for vulnerabilities.
Lessons in This Topic
Containers vs Virtual Machines
Understand the isolation model.
VMs include a full guest OS — heavy (GBs, minutes to start). Containers share the host kernel — lightweight (MBs, seconds to start). Docker uses Linux namespaces and cgroups for isolation. Best for microservices, CI/CD, and consistent dev environments.
Writing Dockerfiles
Build optimized container images.
FROM node:20-alpine (small base). COPY package.json → RUN npm ci. COPY source → CMD ["node", "server.js"]. Use .dockerignore. Multi-stage builds: build in one stage, copy artifact to slim production image. Order layers from least to most frequently changed for cache efficiency.
Docker Compose for Multi-Container Apps
Define app, database, and cache together.
docker-compose.yml defines services: web (Node), db (PostgreSQL), cache (Redis). One command: `docker compose up -d`. Networks connect services by name. Volumes persist database data. Override files for dev vs prod environments.
Docker Networking & Volumes
Connect containers and persist data.
Default bridge network isolates containers. User-defined networks allow service discovery by name. Port mapping: -p 8080:80 exposes container port. Named volumes survive container deletion. Bind mounts link host directories for development hot-reload.
Deploying Docker to Production
Registries, orchestration, and best practices.
Push images to Docker Hub or private registry (ECR, GCR). Pull and run on production servers or Kubernetes. Set resource limits (CPU, memory). Use health checks. Run as non-root user. Scan with Trivy or Snyk. Tag images with git SHA, not just "latest".
