In one line: a container is not a "lightweight VM" — it's a process isolated by kernel namespaces + cgroups plus an isolated filesystem. Docker layers on image layering + standard interfaces + a friendly CLI to turn distribution and execution into "pull + run".
What it is#
# Run nginx in one line
docker run -d --name web -p 8080:80 nginx:1.27
# Inspect
docker ps
docker logs web -f
docker exec -it web sh
# Stop & remove
docker stop web && docker rm webAn image is a stack of read-only layers; a container is the image + a writable layer + a running process.
Analogy#
VM = a full apartment: own utilities (kernel) — heavy, but fully isolated. Container = an apartment in a shared building: shared infrastructure (kernel = the building), but its own door lock (namespace), meter (cgroup), and furniture (filesystem).
Key concepts#
How it works#
The foundation is kernel namespaces + cgroups — there's no "Docker kernel".
Practical notes#
- Don't use
latest— pin a tag or digest. Production must never run on a drifting tag. -p host:container: host port → container port. Without it, only the docker network can reach it.-v /host:/container: bind-mount a host dir. For data, prefer a named volume-v dataname:/path.- Env vars:
-e KEY=VALor--env-file .env. - Resource limits:
--cpus 1.5 --memory 1g, otherwise one container can saturate the host. - Log driver: default
json-file, switch to journald or forward to ELK / Loki in production — otherwise disk fills up. docker system df / prunecleans dangling images / volumes / networks.- Don't run as root:
USERdirective in Dockerfile, plus--read-only+--cap-drop=ALLfor hardening.
Common gotchas#
- Container exits immediately: the main process ended. Containers have no concept of background — main process exits → container ends.
- Can't reach DB on
localhostfrom a container: from the container's view, localhost is itself. Usehost.docker.internalor join the same network. - Wrong timezone: bind-mount
-v /etc/localtime:/etc/localtime:roor setTZ. - Slow networking: check if
--network=hostis in play, and bridge MTU.
Easy confusions#
One image → N containers.
Removing it doesn't affect the image.