The Problem: It Works on My Machine
An app needs the right language version, libraries and settings. On a different computer something differs and it breaks. Docker packages the app together with everything it needs into a container image. That image runs the same on a laptop, a test server and the cloud.
Containers vs Virtual Machines
| Virtual machine | Container | |
|---|---|---|
| Includes | Full guest operating system | App and libraries only, shares the host kernel |
| Size | Gigabytes | Megabytes to a few hundred MB |
| Start time | Minutes | Seconds or less |
| Isolation | Strong (hardware level) | Process level (namespaces and cgroups) |
| Density | Few per host | Many per host |
Key Words
| Term | Meaning |
|---|---|
| Image | Read-only template: app plus dependencies, built in layers |
| Container | A running (or stopped) instance of an image |
| Dockerfile | Text recipe to build an image |
| Registry | Server that stores images (Docker Hub, GitHub Container Registry, Amazon ECR, Azure ACR, Google Artifact Registry) |
| Volume | Storage that survives container deletion |
| Network | Virtual network connecting containers |
Analogy: an image is a class, a container is an object. You can start many containers from one image.
Install and Check
Run Your First Containers
What the flags mean:
| Flag | Meaning |
|---|---|
-d | Detached: run in the background |
--name web | Give the container a name |
-p 8080:80 | Publish: host_port:container_port |
-e KEY=value | Set an environment variable |
-v name:/path | Mount a volume |
--rm | Delete the container when it exits |
-it | Interactive terminal (for shells) |
--restart unless-stopped | Restart after crashes or reboots |
--memory 512m --cpus 1 | Resource limits |
Manage Containers
Run a Database in One Command
-v pgdata:/var/lib/postgresql/data) for anything you want to keep.
Images
An image name looks like registry/namespace/name:tag. A tag is a label such as 1.0 or latest. The latest tag is just a name, not a guarantee of newest, so pin exact versions in real projects.
Image Layers
Each instruction in a Dockerfile creates a read-only layer. Layers are cached and shared, so pulling or rebuilding only changes what is different. This is why instruction order matters (next lesson).
Clean Up Disk Space
Troubleshooting a Container That Exits
docker ps -ashows the exit code. Code 0 is normal exit, 137 usually means out of memory or killed, 1 is an application errordocker logs <name>shows why- A container stops when its main process ends. A shell script that finishes means the container finishes
docker run -it --entrypoint sh imagelets you look insidedocker inspectchecks environment and mounts, anddocker eventsshows daemon activity
Practice Tasks
- Run nginx on port 8081 and change the page by mounting a local
index.htmlwith-v $(pwd)/site:/usr/share/nginx/html:ro - Start a PostgreSQL container with a volume, create a table, remove the container, start a new one with the same volume and check the data survived
- Run a container with
--memory 64mand observe it indocker stats - Find all stopped containers and remove them with one command
- Explain why
docker run -p 8080:80is needed to reach the app from your browser
Conclusion
Docker packages an app and its dependencies into an image, and runs it as an isolated container. Learn run, ps, logs, exec, volumes, port mapping and cleaning up, and you can run almost any software in seconds.

