Skip to content

Introduction to Docker

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

What is Docker?

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host.

Key Benefits

BenefitDescription
PortabilityBuild once, run anywhere. Containers run consistently across development, testing, and production environments.
EfficiencyContainers share the host OS kernel, making them far more efficient than traditional virtual machines.
IsolationEach container runs in its own isolated environment with its own filesystem, networking, and process space.
ScalabilityEasily scale applications horizontally by spinning up additional container instances.
SpeedContainers start in seconds, enabling rapid development and deployment cycles.
Version ControlDocker images are versioned, allowing you to track changes and roll back when needed.

Docker vs Virtual Machines

Understanding the difference between Docker containers and virtual machines (VMs) is fundamental:

┌─────────────────────────────────────┐    ┌─────────────────────────────────────┐
│         Virtual Machines            │    │           Containers                │
├─────────────────────────────────────┤    ├─────────────────────────────────────┤
│  ┌─────┐ ┌─────┐ ┌─────┐          │    │  ┌─────┐ ┌─────┐ ┌─────┐          │
│  │App A│ │App B│ │App C│          │    │  │App A│ │App B│ │App C│          │
│  ├─────┤ ├─────┤ ├─────┤          │    │  ├─────┤ ├─────┤ ├─────┤          │
│  │Bins │ │Bins │ │Bins │          │    │  │Bins │ │Bins │ │Bins │          │
│  │Libs │ │Libs │ │Libs │          │    │  │Libs │ │Libs │ │Libs │          │
│  ├─────┤ ├─────┤ ├─────┤          │    │  └──┬──┘ └──┬──┘ └──┬──┘          │
│  │Guest│ │Guest│ │Guest│          │    │     └───────┼───────┘              │
│  │ OS  │ │ OS  │ │ OS  │          │    │        Docker Engine               │
│  └─────┘ └─────┘ └─────┘          │    │                                     │
│        Hypervisor                   │    │          Host OS                    │
│        Host OS                      │    │        Infrastructure              │
│      Infrastructure                 │    └─────────────────────────────────────┘
└─────────────────────────────────────┘

Comparison Table

FeatureContainersVirtual Machines
Startup TimeSecondsMinutes
SizeMegabytesGigabytes
PerformanceNear-nativeOverhead from hypervisor
IsolationProcess-levelFull OS-level
OS SupportShared kernelIndependent OS per VM
DensityHundreds per hostTens per host

The Docker Platform

Docker provides tooling and a platform to manage the lifecycle of your containers:

  1. Develop your application and its supporting components using containers.
  2. Test your application using the container as the unit of distribution.
  3. Deploy your application into your production environment as a container or orchestrated service.

Docker Architecture Overview

Docker uses a client-server architecture:

┌──────────────┐     ┌──────────────────────────────────┐
│              │     │         Docker Host                │
│  Docker CLI  │────▶│  ┌──────────┐  ┌──────────────┐  │
│  (Client)    │     │  │  Docker   │  │  Containers   │  │
│              │     │  │  Daemon   │──│  ┌────┐┌────┐ │  │
└──────────────┘     │  │ (dockerd) │  │  │ C1 ││ C2 │ │  │
                     │  └──────────┘  │  └────┘└────┘ │  │
                     │       │         └──────────────┘  │
                     │       ▼                            │
                     │  ┌──────────┐                      │
                     │  │  Images   │                      │
                     │  └──────────┘                      │
                     └──────────────────────────────────┘


                     ┌──────────────────┐
                     │  Docker Registry  │
                     │  (Docker Hub)     │
                     └──────────────────┘
  • Docker Client: The primary way users interact with Docker via the CLI or API.
  • Docker Daemon (dockerd): Listens for Docker API requests and manages Docker objects.
  • Docker Registry: Stores Docker images. Docker Hub is the default public registry.

Use Cases

Docker is widely adopted across many industries and use cases:

Application Development

bash
# Run a development environment instantly
docker run -it --rm -v $(pwd):/app -w /app node:20 npm start

Microservices Architecture

Docker enables breaking monolithic applications into smaller, independently deployable services:

yaml
# docker-compose.yml for a microservices stack
services:
  frontend:
    image: my-frontend:latest
    ports:
      - "3000:3000"
  api:
    image: my-api:latest
    ports:
      - "8080:8080"
  database:
    image: postgres:16
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

CI/CD Pipelines

Docker provides consistent environments for continuous integration and delivery:

yaml
# GitHub Actions example
jobs:
  test:
    runs-on: ubuntu-latest
    container:
      image: node:20
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npm test

Cloud-Native Applications

Docker containers are the foundation for cloud-native platforms like Kubernetes:

bash
# Deploy to Kubernetes
kubectl create deployment my-app --image=my-app:1.0
kubectl expose deployment my-app --port=80 --type=LoadBalancer

Getting Started

To start using Docker, you need to:

  1. Install Docker Desktop on your machine — see the Installation Guide.
  2. Understand core concepts like images, containers, and registries — see Core Concepts.
  3. Follow the Quick Start Tutorial to build and run your first container — see Quick Start.
bash
# Verify your Docker installation
docker --version
docker info

# Run your first container
docker run hello-world

Docker Editions

Docker is available in two editions:

EditionUse CaseFeatures
Docker DesktopDevelopment environmentsGUI, Kubernetes, Extensions, Dev Environments
Docker EngineProduction serversCLI-only, lightweight, Linux-native

Next Steps

基于 MIT 许可发布