Lesson 30 of 49 intermediate

Docker — Containers Explained

Ship Your App in a Box

Open interactive version (quiz + challenge)

Real-world analogy

Remember when you shared a school project and your friend said 'it doesn't work on my computer'? Docker solves this. It puts your app in a shipping container (image) that works EXACTLY the same on ANY computer. No more 'works on my machine'!

What is it?

Docker packages your application and all its dependencies into a standardized container. A container is like a lightweight virtual machine that runs consistently on any system with Docker installed.

Real-world relevance

Almost every company uses Docker: Google runs billions of containers weekly. Spotify, PayPal, and Uber all use Docker for deployment. It's become a standard part of modern development.

Key points

Code example

// Dockerfile — the recipe for your app container 📝
FROM node:20-alpine

WORKDIR /app

# Install dependencies first (cached layer!)
COPY package.json pnpm-lock.yaml ./
RUN npm install -g pnpm && pnpm install

# Copy source code
COPY . .

# Build the app
RUN pnpm build

# Expose port for documentation
EXPOSE 3000

# Start the app (always last!)
CMD ["node", "dist/main.js"]

# docker-compose.yml — orchestrate multiple containers 🎼
services:
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - mongodb
      - redis
    environment:
      - DATABASE_URL=mongodb://mongodb:27017/myapp
      - REDIS_URL=redis://redis:6379

  mongodb:
    image: mongo:7
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db  # Data persists!

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

volumes:
  mongo_data:

# One command to start EVERYTHING: 🚀
# docker compose up -d

Line-by-line walkthrough

  1. 1. Dockerfile — the recipe for your app container 📝
  2. 2. Dockerfile instruction
  3. 3.
  4. 4. Dockerfile instruction
  5. 5.
  6. 6. Install dependencies first (cached layer!)
  7. 7. Dockerfile instruction
  8. 8. Dockerfile instruction
  9. 9.
  10. 10. Copy source code
  11. 11. Dockerfile instruction
  12. 12.
  13. 13. Build the app
  14. 14. Dockerfile instruction
  15. 15.
  16. 16. Start the app
  17. 17. Dockerfile instruction
  18. 18.
  19. 19. Dockerfile instruction
  20. 20.
  21. 21. docker-compose.yml — orchestrate multiple containers 🎼
  22. 22.
  23. 23. Docker Compose section definition
  24. 24.
  25. 25.
  26. 26.
  27. 27.
  28. 28.
  29. 29.
  30. 30.
  31. 31.
  32. 32.
  33. 33.
  34. 34.
  35. 35.
  36. 36.
  37. 37.
  38. 38.
  39. 39. Docker Compose section definition
  40. 40.
  41. 41.
  42. 42.
  43. 43.
  44. 44.
  45. 45.
  46. 46.
  47. 47. Docker Compose section definition
  48. 48.
  49. 49.
  50. 50. One command to start EVERYTHING: 🚀
  51. 51. docker compose up -d

Spot the bug

FROM node:20-alpine
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
CMD ["node", "dist/main.js"]
Need a hint?
Think about Docker layer caching when files change...
Show answer
Copying all files before npm install means any code change invalidates the install cache. Fix: COPY package*.json first, run npm install, THEN copy source code.

Explain like I'm 5

Docker is like a lunchbox for your app. At home, your sandwich is great. But bring it to school without a lunchbox, it gets squished! Docker puts your app in a special container so it works perfectly everywhere - your computer, friend's computer, or a big server.

Fun fact

The Docker logo (a whale carrying containers) is named 'Moby Dock'. And the Docker company was originally called 'dotCloud' — they pivoted entirely to containers because it was so popular! 🐋

Hands-on challenge

Write a Dockerfile for a NestJS app that uses multi-stage builds: Stage 1 installs ALL dependencies and compiles TypeScript. Stage 2 copies ONLY the compiled JS and production node_modules. Compare the image sizes of single-stage vs multi-stage. Can you get the final image under 150MB? Hint: use node:20-alpine as the base.

More resources

Open interactive version (quiz + challenge) ← Back to course: Full-Stack Playbook