Docker 架构

客户端、守护进程、containerd、runc 的分工,以及一次 docker run 的完整链路。

Beginner
Key Pointsdocker 命令只是客户端,真正干活的是 dockerd;dockerd 把容器运行交给 containerd,containerd 调 runc 创建进程。

四个角色

Text
docker CLI  ──HTTP/Unix socket──▶  dockerd(守护进程)
                                     │
                                     ├── 镜像管理、网络、卷
                                     └── containerd ──▶ runc ──▶ 容器进程
                                     ▲
                               Registry(Docker Hub / 私有仓库)
ComponentJob
docker (CLI)Parse commands, talk to the daemon over a socket
dockerdBuild images, manage networks/volumes, expose the API
containerdImage pull, container lifecycle, snapshots
runcCreate the actual process with namespaces + cgroups
RegistryStore and distribute images

一次 docker run 发生了什么

  1. CLI 把请求发给 /var/run/docker.sock
  2. dockerd 检查本地有没有该镜像,没有就去 Registry 拉取并解包成层
  3. dockerd 准备网络(默认 bridge)、分配 IP、准备挂载点
  4. 交给 containerd,containerd 起一个 shim 进程托管容器
  5. shim 调 runc:创建命名空间、设 cgroups、pivot_root 到镜像根文件系统、exec 入口命令
  6. runc 退出,容器进程由 shim 接管(所以 dockerd 重启不影响运行中的容器)

看架构

Bash
docker version              # 客户端与服务端版本
docker info                 # 存储驱动、cgroup 版本、运行时
docker system df            # 镜像/容器/卷占用的空间

远程访问守护进程

Docker listens only on the local Unix socket by default. Exposing it to other machines means opening TCP with TLS — a bare 2375 port hands root to the whole network.

Bash
# 客户端指向远程(生产必须走 TLS)
export DOCKER_HOST=tcp://docker.example.com:2376
export DOCKER_TLS_VERIFY=1
export DOCKER_CERT_PATH=$HOME/.docker/certs
docker ps
Port 2375 is plaintext and scanners find it within minutes. Use 2376 with client certificates, or tunnel over SSH.

Friends & Partners