Fullstack CourseLearn by building
Back to week 4

Topic

Docker & Compose

Definition

A container packages an application with its runtime dependencies into an isolated, reproducible unit, while a multi-container orchestration file such as docker-compose.yml declares how several related containers, such as an API, database, cache, and broker, run and network together.

In simpler words

Docker ships this exact app plus everything it needs as one predictable box. Compose wires several boxes together for local development.

Judge Docker the same conceptual way as Redis or RabbitMQ: where it solves a real environment problem, and where it stops mattering for this course.

After this you can

  • Explain what a container isolates versus a virtual machine
  • Read a docker-compose.yml and name each service role
  • Reason about why it works on my machine gets fixed by containers
  • Describe the tradeoff of adding Docker to a small team workflow

Why containers instead of installing locally

Definition

Environment drift occurs when developer machines have different tool, library, or operating system versions than production, producing bugs that only reproduce on some machines. Containers eliminate that drift by shipping the exact runtime.

In simpler words

Docker stops it works on my laptop but not in staging by shipping the whole environment, not only the code.

A container bundles the app, its language runtime, and system libraries into one image that runs identically on a laptop, a CI runner, or a production host.

Compared to a full virtual machine, a container shares the host kernel and starts in seconds, which is why local development stacks combining a database, cache, broker, and API commonly run as containers instead of virtual machines.

This monorepo could run Postgres in a container for local development specifically so every contributor gets the same database version without installing Postgres natively.

Reading a compose file

services:
  api:
    build: ./apps/api
    ports: ["3001:3001"]
    depends_on: [postgres, redis]
  postgres:
    image: postgres:16
    environment: { POSTGRES_PASSWORD: dev }
  redis:
    image: redis:7

Each service is one container. depends_on only orders startup; it does not wait for the database to accept connections.

What Compose does and does not guarantee

Definition

docker-compose.yml declares service definitions, networking, and startup order for local or single-host multi-container environments. It does not manage rolling deploys, autoscaling, or multi-host orchestration.

In simpler words

Compose is great for running a whole stack locally with one command. It is not a production deployment platform by itself.

depends_on controls container start order, not application readiness. Nest must still retry its Postgres connection on boot rather than assume the database is already accepting queries.

Named volumes persist data, such as a Postgres data directory, across container restarts. Without one, stopping the stack can silently wipe local development data.

Production-grade orchestration, such as rolling updates, health-based routing, and scaling, is typically a separate tool such as Kubernetes or a managed container service. Compose is the local development and single-host layer.

Keep in mind

  • Containers fix environment drift; they do not fix incorrect code.
  • depends_on is start order, not a readiness check, so code defensively.
  • Reach for full orchestration only once the single-host model of Compose is not enough.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 3 · syntax 2 · practical 3 · logic 2

Concept1. Which statement best defines Docker and Compose?
Syntax2. Which code-level choice matches Docker and Compose?
Practical3. A reviewer spots a bug related to Docker and Compose. What is the right fix?
Logic4. Which reasoning correctly explains Docker and Compose?
Concept5. Which boundary does correct use of Docker and Compose preserve?
Practical6. What is the safest next step when applying Docker and Compose?
Syntax7. Which implementation direction matches the rule for Docker and Compose?
Logic8. Which consequence follows from applying Docker and Compose correctly?
Concept9. Which claim about Docker and Compose is true in this Nest + Postgres monorepo?
Practical10. Which team practice best demonstrates Docker and Compose?