Docker is a platform that lets you package applications and their dependencies into lightweight, portable units called containers. Instead of saying "it works on my machine," you ship the machine itself.
The Problem Docker Solves
Consider a typical web application. It needs:
- A specific version of Node.js (e.g., 20.x)
- A PostgreSQL database
- Redis for caching
- Specific environment variables
- Certain system libraries
Without Docker, every developer on the team has to install all of this manually. The staging server might run a different version of Node. The production server might have a conflicting library. Things break in unpredictable ways.
Docker eliminates this by packaging everything the application needs into a container that runs the same way everywhere.
Containers vs Virtual Machines
Both containers and virtual machines (VMs) isolate applications, but they work differently:
| Feature | Virtual Machine | Container |
|---|---|---|
| Includes | Full OS + app | App + dependencies only |
| Size | Gigabytes | Megabytes |
| Boot time | Minutes | Seconds |
| Performance | Near-native with overhead | Near-native |
| Isolation | Strong (separate kernel) | Process-level (shared kernel) |
| Resource usage | Heavy | Lightweight |
A VM virtualizes the entire hardware stack and runs a complete operating system. A container shares the host operating system's kernel and only isolates the application's processes and filesystem.
┌─────────────────────────────┐ ┌─────────────────────────────┐
│ Virtual Machines │ │ Containers │
│ │ │ │
│ ┌───────┐ ┌───────┐ │ │ ┌───────┐ ┌───────┐ │
│ │ App A │ │ App B │ │ │ │ App A │ │ App B │ │
│ ├───────┤ ├───────┤ │ │ ├───────┤ ├───────┤ │
│ │ Libs │ │ Libs │ │ │ │ Libs │ │ Libs │ │
│ ├───────┤ ├───────┤ │ │ └───────┘ └───────┘ │
│ │ OS │ │ OS │ │ │ ┌─────────────────────┐ │
│ └───────┘ └───────┘ │ │ │ Container Runtime │ │
│ ┌─────────────────────┐ │ │ └─────────────────────┘ │
│ │ Hypervisor │ │ │ ┌─────────────────────┐ │
│ └─────────────────────┘ │ │ │ Host OS Kernel │ │
│ ┌─────────────────────┐ │ │ └─────────────────────┘ │
│ │ Host OS / Hardware │ │ │ ┌─────────────────────┐ │
│ └─────────────────────┘ │ │ │ Hardware │ │
└─────────────────────────────┘ └─────────────────────────────┘Docker Architecture
Docker uses a client-server architecture:
- Docker Client (
docker) — the command-line tool you interact with - Docker Daemon (
dockerd) — the background service that manages containers - Docker Registry (Docker Hub) — a repository for sharing container images
When you run docker run nginx, the client tells the daemon to pull the nginx image from Docker Hub, create a container from it, and start it.
Installing Docker
On Ubuntu/Debian:
# Remove old versions
sudo apt remove docker docker-engine docker.io containerd runc
# Install prerequisites
sudo apt update
sudo apt install ca-certificates curl gnupg
# Add Docker's official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# Add the repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
On macOS and Windows, install Docker Desktop from docker.com.
After installation, add your user to the docker group so you do not need sudo:
sudo usermod -aG docker $USER
# Log out and back in for the change to take effectVerify the installation:
docker --version
docker run hello-worldKey Docker Concepts
Image — a read-only template that contains the application code, runtime, libraries, and configuration. Think of it as a blueprint.
Container — a running instance of an image. You can create multiple containers from the same image.
Dockerfile — a text file with instructions for building an image.
Registry — a storage and distribution service for images. Docker Hub is the default public registry.
Volume — persistent storage that survives container restarts and removal.
Network — a communication channel that lets containers talk to each other.
Running Your First Container
# Run an Nginx web server
docker run -d -p 8080:80 --name my-web nginx
# What this does:
# -d Run in the background (detached)
# -p 8080:80 Map port 8080 on your machine to port 80 in the container
# --name Give the container a friendly name
# nginx The image to useNow open http://localhost:8080 in your browser — you will see the Nginx welcome page.
# See running containers
docker ps
# See all containers (including stopped ones)
docker ps -a
# View container logs
docker logs my-web
# Stop the container
docker stop my-web
# Start it again
docker start my-web
# Remove the container
docker stop my-web
docker rm my-webExploring a Container
You can open a shell inside a running container:
# Start a container and open a shell
docker run -it ubuntu bash
# Open a shell in an already-running container
docker exec -it my-web bashInside the container, you are in an isolated environment. You can install packages, modify files, and explore — none of it affects your host system.
Docker Hub
Docker Hub hosts millions of pre-built images. Some popular ones:
| Image | Description |
|---|---|
nginx | Web server |
node | Node.js runtime |
python | Python runtime |
postgres | PostgreSQL database |
redis | In-memory cache |
ubuntu | Ubuntu base image |
alpine | Minimal Linux (5 MB) |
Search for images:
docker search postgresPull an image without running it:
docker pull node:20-alpineCleaning Up
Docker resources accumulate over time. Clean them up regularly:
# Remove all stopped containers
docker container prune
# Remove unused images
docker image prune
# Remove everything unused (containers, images, networks, volumes)
docker system prune -a
# Check disk usage
docker system dfSummary
Docker packages applications into portable containers that run consistently across any environment. You learned the difference between containers and VMs, installed Docker, ran your first container, and explored Docker Hub. In the next lesson, you will dive deeper into images and containers.