Lesson 02 beginner

Why Use Docker?

The practical reasons teams adopt containers: consistent environments, fast onboarding, reliable CI/CD and denser servers, plus when Docker is not the right tool.

Knowing what a container is matters less than knowing what it changes in your daily work. This lesson walks through the problems Docker removes, with small examples you can try, and ends with the cases where you should not reach for it.

The same environment everywhere

An image carries the exact operating system packages, language runtime and libraries your app was tested with. The image that passed CI is byte for byte the one that reaches production, identified by a digest that cannot change.

$ docker image inspect --format '{{index .RepoDigests 0}}' nginx:1.27
nginx@sha256:5f0574409b3add89581b96c68afe9e9c7b284651c3a974b6e8bac46bf95e6b7f

Deploying nginx@sha256:... instead of a tag guarantees nobody swapped the image under you.

Onboarding in minutes, not days

A new developer used to spend a day installing databases and fighting version conflicts. With Docker Compose, the whole stack is described in one file and starts with one command:

services:
  api:
    build: .
    ports:
      - "8000:8000"
    environment:
      DATABASE_URL: postgres://app:app@db:5432/app
    depends_on:
      - db
  db:
    image: postgres:17
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: app
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
$ docker compose up -d
[+] Running 3/3
 ✔ Volume "shop_pgdata"  Created
 ✔ Container shop-db-1   Started
 ✔ Container shop-api-1  Started

Postgres runs at the exact version production uses, and nothing is installed on the laptop itself. docker compose down removes it all again.

Isolation without conflicts

Two projects need Python 3.10 and Python 3.13? Two services need different versions of the same library? Each container has its own filesystem, so they never collide:

$ docker run --rm python:3.10-slim python --version
Python 3.10.16
$ docker run --rm python:3.13-slim python --version
Python 3.13.3

Reliable CI/CD

Pipelines become simpler when every step runs in a known image. A typical flow:

  1. CI builds the image once from the commit.
  2. Tests run inside that image.
  3. The same image is pushed to a registry with the commit SHA as tag.
  4. Staging and production pull that exact image. Nothing is rebuilt.

Because the artifact never changes between stages, “it passed in CI but broke in prod” becomes rare, and rolling back means running the previous tag.

More apps per server

Containers share the host kernel and start in milliseconds, so one server can run many more of them than virtual machines. Docker also lets you cap each one so a noisy neighbor cannot starve the others:

$ docker run -d --name worker --memory=256m --cpus=0.5 my-worker:2.3

When Docker is not the best fit

Docker is a great default for server software, but not for everything:

  • Desktop GUI applications are awkward to containerize and distribute.
  • Software tied to a specific kernel or hardware (custom drivers, some real-time workloads) may need the host directly or a VM.
  • A single static binary on one server may not need the extra layer at all.
  • Stateful systems such as databases run fine in containers, but you must plan storage, backups and upgrades carefully (lesson 9 covers volumes).

Rule of thumb: if you deploy it more than once, to more than one place, or with other services around it, a container will pay off.

Exercise

Two apps on the same server need Python 3.9 and Python 3.13. How does Docker help?

  1. Both apps must first be upgraded to the same Python version
  2. Docker installs both versions system-wide and picks one at runtime
  3. You need one virtual machine per app
  4. Each image ships its own Python, so the two versions never conflict
Show answer

Answer: D. Every container has its own filesystem from its image. Dependencies that would clash on the host live side by side in separate containers.

Key takeaways

  • Images make environments identical from laptop to production.
  • Docker Compose starts a whole multi-service stack with one command.
  • Build once in CI, then promote the same image through every environment.
  • Limits like --memory and --cpus let many containers share one server safely.

FAQ

Does Docker make my application faster?

Not by itself. A container runs at nearly native speed, but it does not optimize your code. The gains are in delivery: faster setup, safer deploys and better use of servers.

Should I run my database in Docker?

For development and testing, almost always yes. In production it works well too, as long as the data lives in a volume and you have tested backups. Many teams still prefer a managed database service for production.

Is Docker Compose meant for production?

Compose is excellent on a single server and for development. When you need several machines, self-healing and rolling updates, an orchestrator such as Kubernetes is the next step.

↑ ↓ navigate Enter open Esc close