Docker Basics
Learn the core concepts of Docker containers, images, volumes, and everyday CLI commands.
TL;DR
A Docker image is a read-only template of your app and its dependencies; a container is a running instance of that image sharing the host Linux kernel, with ephemeral writable storage unless you attach a volume.
Why it exists
Applications often fail between laptops and servers because of mismatched runtimes, system libraries, or OS packages. Containers package the app and its user-space dependencies into one portable, repeatable artifact without the memory and boot overhead of a full virtual machine.
Mental model
Keep three concepts distinct:
- Image: Read-only layered filesystem + default startup config (
Dockerfile→docker build). - Container: A live process (plus a thin writable layer) started from an image (
docker run). - Volume: Persistent storage managed by Docker outside the container's lifecycle (
docker volume).
Do it
Verify Docker is working, run a container, inspect running containers, and clean up:
docker run --rm hello-world
docker run --rm -d --name web -p 8080:80 nginx:1.27-alpine
docker ps
docker logs web
docker stop webGotchas
Data written inside a container's writable layer disappears as soon as the container is removed. Always mount a named volume (-v pgdata:/var/lib/postgresql/data) for stateful services.
- Mutable
:latesttags: Pin explicit version tags (or digests) inDockerfileanddocker runso two machines never pull different builds under the same tag. - Leftover containers: Pass
--rmfor one-offdocker runcommands so exited containers don't accumulate on disk.
Production checklist
- Pin base image tags to an explicit version (never
:latest). - Run containers as a non-root
USER. - Store persistent state in named volumes or external managed storage, never the container layer.
- Add a
HEALTHCHECKor orchestrator probe so unhealthy containers are detected automatically.
Check yourself
Check Yourself
1. Why do Docker containers start much faster than traditional virtual machines?
2. What happens to files written inside /tmp or /app inside a container when you run docker rm?
Sources
Observability: Logs, Metrics & Traces
Use logs, metrics, and traces with RED/USE, symptom-based alerts, SLO error budgets, and OpenTelemetry—and know what a static site needs vs a backend.
Image Layers and Build Cache
How Docker caches read-only filesystem layers and why COPY order determines build speed.