Docker: What is Docker?
Docker ensures that applications run exactly the same way in any environment—it packages "code + dependencies + configuration" into a portable container, putting an end to the "it works on my machine" nightmare.
1. What You'll Learn
- The Fundamental Difference Between Containers and Virtual Machines
- Docker's three-tier architecture (Client / Daemon / Registry)
- The Relationship Between Images and Containers
- Key Use Cases for Docker
- The Container Startup Process
2. A True Story of a Backend Developer
(1) Pain Point: Disasters Caused by Inconsistent Environments
Alice is a backend developer. Although her project runs smoothly on her local machine, she frequently encounters the "it works on my machine" problem: her colleagues' Ubuntu environments are missing dependencies, the library versions on the CentOS test server are incorrect, and the Red Hat production environment requires recompilation. Every deployment feels like a gamble—three environments, five rounds of debugging, and two days wasted on "environment configuration" instead of writing code.
(2) The Docker Solution
Bob recommended that she use Docker: "With just one command, the app will run exactly the same way on any Linux system."
# Build once, run anywhere
docker build -t myapp:1.0 .
docker run -d -p 8080:80 myapp:1.0
(3) Benefit: Environmental Consistency
After Alice packaged the application into a Docker image, the same image was used to run the application in all three environments—development, testing, and production—reducing deployment time from two days to 10 minutes and eliminating all environment-related issues.
3. What Is Containerization?
Containerization is a lightweight virtualization technology that packages an application and all its dependencies into an isolated runtime environment, ensuring consistent execution on any infrastructure.
graph TB
A[Traditional Deployment<br/>App + OS + Hardware] --> B[Virtualization<br/>App + Guest OS + Hypervisor + Host OS]
B --> C[Containerization<br/>App + Container Engine + Host OS]
C --> D["Lighter · Faster · More consistent"]
(1) The Core Concept of Containerization
- Build-and-Deploy: Application + dependencies + configuration = one image; wherever the image goes, the application goes
- Non-virtual isolation: Shares the host kernel; does not emulate a full operating system
- Build once, run anywhere: Images built in the development environment can be used directly in the production environment
(2) Why Are They Called "Containers"?
Think of containers in the transportation industry: whether they’re filled with cars, clothes, or food, the ships, trains, and trucks that carry them don’t need to know—they simply transport standard-sized containers. Docker containers are the containers of the software world:
| Dimension | Shipping Container | Docker Container |
|---|---|---|
| Standardization | Uniform dimensions (20/40 feet) | Uniform image format (OCI standard) |
| Isolation | No interference between loads | Process/network/filesystem isolation |
| Portable | Can be transported by ship, train, or truck | Runs in development, testing, and production environments |
| Highly Efficient | No need to unpack and switch cars | No need to reinstall dependencies |
4. Containers vs. Virtual Machines
Both containers and virtual machines provide isolated environments, but they are implemented in completely different ways.
graph TB
subgraph VM["Virtual Machine Architecture"]
VM_APP1["App 1"] --> VM_OS1["Guest OS"]
VM_APP2["App 2"] --> VM_OS2["Guest OS"]
VM_OS1 --> VM_H["Hypervisor"]
VM_OS2 --> VM_H
VM_H --> VM_HOST["Host OS"]
VM_HOST --> VM_HW["Hardware"]
end
subgraph CT["Container Architecture"]
CT_APP1["App 1"] --> CT_APP2["App 2"]
CT_APP2 --> CT_CE["Container Engine<br/>Docker"]
CT_CE --> CT_KERNEL["Host OS Kernel"]
CT_KERNEL --> CT_HW["Hardware"]
end
(1) Comparison of Key Differences
| Dimension | Virtual Machine (VM) | Container |
|---|---|---|
| Isolation Level | Hardware-level isolation (separate kernel) | Process-level isolation (shared kernel) |
| Boot Speed | Minutes (to boot the entire OS) | Seconds (to start a process) |
| Resource Usage | GB-level (including Guest OS) | MB-level (application dependencies only) |
| Image Size | 1–20 GB | 5–500 MB |
| Performance Overhead | 5–15% (virtualization overhead) | ≈0% (close to native) |
| Density | 5–10 VMs per machine | 100–1,000 containers per machine |
(2) Comparison of Use Cases
| Scenario | Recommended Virtual Machine | Recommended Container |
|---|---|---|
| Requires different operating system kernels | ✅ (e.g., Windows and Linux running in parallel) | ❌ |
| Microservices/Stateless Applications | ❌ | ✅ (Lightweight, fast scaling) |
| Strong isolation security requirements | ✅ (Kernel-level isolation) | ⚠️ (Requires additional hardening) |
| CI/CD Build Environment | ❌ | ✅ (Launches and shuts down in seconds) |
| Development Environment Consistency | ❌ | ✅ (The image is the environment) |
| Run traditional GUI applications | ✅ | ⚠️ (Requires additional configuration) |
5. Docker Architecture
Docker uses a client-server architecture and consists of three core components.
graph TB
CLI["Docker Client<br/>docker build/run/pull"]
CLI -->|REST API| D["Docker Daemon<br/>dockerd"]
D -->|pull/push| R["Docker Registry<br/>Docker Hub / Private"]
D -->|create/start| C["Containers<br/>Running instances"]
D -->|build/cache| I["Images<br/>Read-only templates"]
I -->|docker run| C
R -->|docker pull| I
(1) A Detailed Explanation of the Three-Tier Architecture
| Component | Function | Common Commands |
|---|---|---|
| Docker Client | The command-line interface that users use to interact with Docker | docker run / docker build / docker pull |
| Docker Daemon | Background service that manages images, containers, networks, and volumes | systemctl start docker / dockerd |
| Docker Registry | A repository for storing and distributing images | docker pull / docker push |
(2) The Relationship Between Images and Containers
An image is a read-only template, and a container is a running instance of an image. To draw an analogy with object-oriented programming:
| Concept | OOP Analogy | Description |
|---|---|---|
| Image | Class | A read-only template that defines everything needed to run the application |
| Container | Instance | The running state of an image; readable and writable; has a lifecycle |
| Dockerfile | Source Code | Instruction file for building images |
(3) OCI Standard
Docker adheres to the OCI (Open Container Initiative) standard to ensure container portability:
- Image Specifications: Defines the format and layout of the image
- Runtime Specification: Defines the container's runtime environment
- Distribution Specification: Defines the distribution protocol for the image
6. Key Use Cases for Docker
(1) Scenario Overview
| Scenario | Description | Example |
|---|---|---|
| Standardization of the Development Environment | Unified development environment for the team | New members can set up the complete development stack in 10 minutes |
| Microservice Deployment | Each service runs in its own container | API, DB, and Cache are each containerized |
| CI/CD Pipeline | Consistent and repeatable build environment | GitHub Actions + Docker for automated deployment |
| Rapid Scaling | Instances launched/shut down in seconds | 1→10 replicas during traffic peaks, 10→1 during off-peak hours |
| Containerization of Legacy Applications | Migrate legacy applications without modification | Package a 2015 PHP 5.4 project into a container |
(2) The Container Startup Process
When you run docker run hello-world, the following steps take place internally in Docker:
graph LR
A["docker run<br/>hello-world"] --> B{"There is a local mirror?"}
B -->|No| C["docker pull<br/>from Registry Pull"]
B -->|Yes| D["Create a Container<br/>Writable layer"]
C --> D
D --> E["Start the container<br/>Execute CMD"]
E --> F["Output Results<br/>Hello from Docker!"]
7. Hands-On Experience with Docker
▶ Example: Running "hello-world" (Difficulty: ⭐)
The Simplest Way to Try Docker—Verify That Docker Is Installed Correctly.
# Pull and run the hello-world image
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:7be1...2c2d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
▶ Example: Viewing Docker Configuration Information (Difficulty: ⭐)
docker info Displays the complete configuration of the Docker daemon.
# Display system-wide Docker information
docker info
Client: Docker Engine - Community
Version: 24.0.7
Context: default
Server:
Containers: 3
Running: 1
Paused: 0
Stopped: 2
Images: 5
Server Version: 24.0.7
Storage Driver: overlay2
Kernel Version: 5.15.0-91-generic
Operating System: Ubuntu 22.04
▶ Example: Browse Docker command categories (Difficulty: ⭐)
The Docker CLI is categorized by management objects for easy memorization.
# List all Docker management commands
docker --help
Management Commands:
builder Manage builds
container Manage containers
image Manage images
network Manage networks
volume Manage volumes
system Manage Docker
Commands:
run Create and run a new container
ps List containers
images List images
build Build an image from a Dockerfile
pull Pull an image from a registry
▶ Example: Try running the Nginx web server (Difficulty: ⭐⭐)
Start a web server with a single Docker command—no need to install Nginx.
# Run Nginx in detached mode, map port 8080 to container port 80
docker run -d -p 8080:80 --name my-nginx nginx:latest
# Verify the container is running
docker ps
CONTAINER ID IMAGE STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:latest Up 10 seconds 0.0.0.0:8080->80/tcp my-nginx
Open your browser and go to http://localhost:8080 to see the Nginx welcome page.
▶ Example: View the list of currently running containers (Difficulty: ⭐)
# List all running containers
docker ps
# List all containers (including stopped ones)
docker ps -a
CONTAINER ID IMAGE STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:latest Up 5 minutes 8080->80 my-nginx
f7e8d9c0b1a2 hello-world Exited (0) 3 minutes ago hungry_einstein
hello-world container automatically exits after running (status: Exited). This is not an error—it is a demo container designed to exit once it has finished running.
8. Verifying the Docker Version and Installation
(1) Docker Versioning System
| Version | Positioning | Update Frequency | Use Cases |
|---|---|---|---|
| Docker Desktop | Developer Desktop Edition | Quarterly Updates | Windows / Mac Development Environment |
| Docker Engine | Server Edition | Quarterly Updates | Linux Production Environment |
| Docker CE | Community Edition (Free) | Continuously updated | Individuals and small teams |
| Docker EE | Enterprise Edition (Paid) | Continuously Updated | Large Enterprises + Commercial Support |
(2) Verify that the installation was successful
# Check Docker client and server versions
docker version
# Quick verification: run hello-world
docker run hello-world
If docker version displays both Client and Server information, the installation was successful.
9. Complete Example: A Step-by-Step Guide to Getting Started with Docker
# ============================================
# Complete walkthrough: First Docker experience
# Covers: version check, info, run, ps, clean
# ============================================
# 1. Verify Docker is installed and running
docker version
# 2. View Docker system information
docker info
# 3. Run the hello-world container (first pull, then run)
docker run hello-world
# 4. Run an Nginx web server
docker run -d -p 8080:80 --name web nginx:latest
# 5. List running containers
docker ps
# 6. Stop and remove the Nginx container
docker stop web
docker rm web
# 7. View all containers (including stopped)
docker ps -a
# 8. Clean up: remove the stopped hello-world container
docker rm $(docker ps -a -q --filter "ancestor=hello-world")
# docker version
Client: Docker Engine - Community
Version: 24.0.7
Server: Docker Engine - Community
Version: 24.0.7
# docker run hello-world
Hello from Docker!
# docker ps (while Nginx is running)
CONTAINER ID IMAGE STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:latest Up 10 seconds 0.0.0.0:8080->80/tcp web
❓ FAQ
docker run hello-world in the terminal to verify it works.docker command. This tutorial starts from scratch, and any necessary Linux concepts will be explained as we go.📖 Summary
- Containerization is a lightweight virtualization technology that shares the kernel and provides process isolation; it is lighter and faster than virtual machines.
- Key differences between containers and virtual machines: Containers share the host’s kernel, start up in seconds, and use only a few megabytes of memory
- Docker's three-tier architecture: Client (CLI) → Daemon (backend service) → Registry (image repository)
- An image is a read-only template, and a container is a running instance of an image (analogy: class vs. object)
- Key Docker Use Cases: Environment Standardization, Microservices Deployment, CI/CD, Rapid Scaling
docker run hello-worldis the first step in verifying the Docker installation
📝 Exercises
- Basic Exercise (Difficulty: ⭐): Install Docker Desktop and run
docker run hello-world, then take a screenshot or copy the output. - Advanced Exercise (Difficulty ⭐⭐): Run
docker infoto view your local Docker configuration, and record the values for the three fields: Number of Containers, Storage Driver, and Kernel Version. - Challenge Question (Difficulty: ⭐⭐⭐): List three advantages and three disadvantages of containers and virtual machines, respectively, and explain in which scenarios you should choose containers and in which scenarios you should choose virtual machines.