What is Docker and Why Use It?
Chapter 1 — What is Docker and Why Use It?
If you've ever set up a web server, a Python project, or a database and thought "this took ages — I never want to do this again", Docker is for you. Docker lets you package an application and everything it needs to run — the runtime, the libraries, the config — into a single portable unit called a container. You can start it in seconds, stop it cleanly, copy it to another machine, and it will behave exactly the same way every time.
In this chapter you'll understand what Docker actually is, how it differs from virtual machines, and get it installed and running your very first container.
1. The Problem Docker Solves
Picture this: you set up Apache on your Raspberry Pi. It works perfectly. A few months later your hosting provider's server has a slightly different OS version, different library versions, different PHP config — and suddenly things break in ways that are hard to debug. This is so common it has a name: "works on my machine".
Docker solves this by making the environment part of the application. Instead of installing Apache onto a server and hoping the server stays consistent, you run an Apache container that includes everything Apache needs. The server just needs Docker — nothing else.
2. Containers vs Virtual Machines
Both containers and virtual machines (VMs) solve the "consistent environment" problem, but they do it very differently — and the difference matters for everyday use.
A VM runs a complete operating system inside a hypervisor. It's powerful and fully isolated, but it takes minutes to boot and gigabytes of disk space. A Docker container shares the host's OS kernel and only packages what's different — the libraries and config your app needs. The result is a container that starts in one or two seconds and might be 50 MB instead of 10 GB.
- Start time: VM = minutes. Container = seconds.
- Disk space: VM = gigabytes. Container = megabytes.
- Isolation: Both are isolated. Containers share the kernel but have their own filesystem, processes, and network.
- Portability: Both are portable, but a Docker image moves far more easily than a VM disk file.
3. Key Concepts
Docker has three core concepts you need to understand before anything else makes sense. Everything in Docker flows from these three ideas.
Think of it like a cake recipe. The recipe itself isn't a cake.
Using the recipe analogy — a container is the actual cake you baked from it.
docker command.
4. Installing Docker
On Windows (Docker Desktop)
docker.com/products/docker-desktop and download the
Windows installer. Run it and follow the prompts — it will install WSL 2
if needed and ask you to restart.
On Ubuntu / Debian Linux
curl -fsSL https://get.docker.com | sh — it handles the ARM
architecture automatically. Then run sudo usermod -aG docker $USER
and log back in.
On macOS
Download Docker Desktop from docker.com/products/docker-desktop,
choose the correct version for your chip (Apple Silicon or Intel), and drag it
to Applications. Start it and wait for the whale icon to settle in the menu bar.
5. Verifying the Install
Open a terminal (PowerShell on Windows, or any terminal on Linux/Mac) and run these two commands to confirm Docker is installed and the engine is running:
If docker info returns an error like "Cannot connect to the Docker
daemon", the Docker engine isn't running. On Windows/Mac, make sure Docker Desktop
is open. On Linux, start it with sudo systemctl start docker.
6. Your First Container
The traditional first Docker command pulls a tiny test image from Docker Hub and runs it. It's deliberately simple — but watch what actually happens:
Notice what happened automatically:
- Docker looked for the image locally — it wasn't there
- Docker pulled it from Docker Hub —
library/hello-worldis an official image - Docker created and ran a container from it — the container printed its message and exited
- The container stopped — it finished its job, so it stopped. Containers only run as long as their process runs
Now try something slightly more interesting — run an interactive Ubuntu shell inside a container:
You just ran a full Ubuntu environment, got a shell inside it, ran a command, and exited — all without installing Ubuntu on your machine. The two flags used here are important:
-i— interactive mode (keep STDIN open)-t— allocate a terminal (so you get a proper shell prompt)
These are almost always used together as -it whenever you want
to type commands inside a container.
7. What Happened Under the Hood
Every docker run command goes through the same sequence:
ubuntu image is already on your machine. If not, it pulls it from Docker Hub automaticallybash) runs inside the isolated container environment. When this process exits, the container stopsubuntu
and a custom image built on top of ubuntu, they share the same
underlying Ubuntu layers on disk. Docker only stores each layer once, which is
why pulling a second Ubuntu-based image is much faster than the first.
8. Three Things to Remember About Containers
These three properties catch beginners out. Keep them in mind and a lot of Docker will make more sense:
- Containers are ephemeral by default. When a container stops, any files written inside it are gone the next time you start a fresh container from the same image. This is intentional — containers are meant to be disposable. Chapter 4 covers how to persist data using volumes.
- Each container has its own network. A web server running inside a container is not automatically accessible from your browser — you have to explicitly map a port from the container to your host. You'll see this in Chapter 5 and in the Apache scenario in Chapter 7.
- The image is never modified by running it. No matter what you do inside a running container, the original image stays exactly as it was. Next time you create a container from that image, it starts fresh. To save changes into a new image, you write a Dockerfile (Chapter 6).
- Run hello-world and read the output carefully. It tells you exactly the four steps Docker took to produce the message. Make sure you can explain each step in your own words.
- Run an interactive Ubuntu container with
docker run -it ubuntu bash. Once inside, runapt update && apt install -y curlto install curl. Then exit the container and run a brand newdocker run -it ubuntu bash— confirm that curl is no longer installed. This demonstrates that containers are ephemeral. - Run an Alpine container —
docker run -it alpine sh(Alpine usesshnotbash). Alpine is a tiny Linux distro, very popular in Docker images. Check its size: exit the container and rundocker imagesto compare the size ofalpinevsubuntu. - List your images and containers: run
docker imagesto see what you've pulled, anddocker ps -ato see all containers including stopped ones. Notice that eachdocker runcreated a separate container even when using the same image. - Clean up: remove all stopped containers with
docker container pruneand confirm withdocker ps -a. This is a habit worth building — stopped containers don't use CPU or memory, but they do use a small amount of disk space.