Docker Basics
Understand containerization fundamentals — how containers differ from VMs, core Docker concepts, and getting Docker installed on your system.
Containers vs Virtual Machines
Virtual machines include a full guest operating system on top of a hypervisor, consuming gigabytes of disk and minutes to boot. Containers share the host OS kernel, isolating processes with namespaces and cgroups — starting in seconds and using megabytes.
Containers package application code, runtime, libraries, and dependencies into a portable unit. The same container runs identically on a developer laptop, CI server, and production cloud instance.
- Containers share the host kernel — they are not mini-VMs
- Isolation is process-level, not hardware-level
- Containers are immutable — changes require new images
# VM: Hypervisor → Guest OS → App (heavy, slow) # Container: Host OS → Docker Engine → Container (light, fast) docker run -d --name web nginx:alpine docker ps
Core Concepts
A Docker image is a read-only template with filesystem layers. A container is a running instance of an image. A Dockerfile is a recipe for building images. A registry (Docker Hub) stores and distributes images.
Images are built in layers — each Dockerfile instruction creates a layer. Layers are cached and shared between images, making builds and pulls efficient.
# Pull an image from Docker Hub docker pull node:20-alpine # Run a container docker run -d -p 3000:3000 --name api node:20-alpine # List running containers docker ps
Installation
Install Docker Desktop on macOS and Windows for a GUI-managed experience with Kubernetes support. On Linux, install docker-ce via your package manager and add your user to the docker group.
Verify installation with docker run hello-world. This pulls a test image, runs it, prints a confirmation message, and exits.
# macOS brew install --cask docker # Linux (Ubuntu) sudo apt update && sudo apt install docker-ce docker-ce-cli sudo usermod -aG docker $USER # Verify docker --version docker run hello-world
Essential Commands
Master the daily commands: docker pull (download image), docker run (create and start container), docker ps (list containers), docker stop/start (control lifecycle), docker rm (remove container), docker rmi (remove image).
Use docker logs to view container output and docker exec to run commands inside a running container. These two commands cover most debugging needs.
- docker ps -a shows stopped containers too
- docker exec -it opens an interactive terminal session
- docker system prune removes unused images, containers, and networks
docker ps -a # All containers docker logs -f api # Follow logs docker exec -it api sh # Shell into container docker stop api && docker rm api docker system prune -f # Clean unused resources
Docker Architecture
The Docker daemon (dockerd) manages images, containers, networks, and volumes. The Docker CLI communicates with the daemon via REST API. Docker Desktop runs the daemon in a lightweight Linux VM on macOS/Windows.
Understanding this client-server model explains why Docker commands require the daemon to be running and why remote Docker hosts are possible via DOCKER_HOST.
# Check daemon info docker info # Remote Docker host export DOCKER_HOST=ssh://user@remote-host docker ps