Docker 架构
客户端、守护进程、containerd、runc 的分工,以及一次 docker run 的完整链路。
Key Pointsdocker 命令只是客户端,真正干活的是 dockerd;dockerd 把容器运行交给 containerd,containerd 调 runc 创建进程。
四个角色
Text
docker CLI ──HTTP/Unix socket──▶ dockerd(守护进程)
│
├── 镜像管理、网络、卷
└── containerd ──▶ runc ──▶ 容器进程
▲
Registry(Docker Hub / 私有仓库)| Component | Job |
|---|---|
| docker (CLI) | Parse commands, talk to the daemon over a socket |
| dockerd | Build images, manage networks/volumes, expose the API |
| containerd | Image pull, container lifecycle, snapshots |
| runc | Create the actual process with namespaces + cgroups |
| Registry | Store and distribute images |
一次 docker run 发生了什么
- CLI 把请求发给
/var/run/docker.sock - dockerd 检查本地有没有该镜像,没有就去 Registry 拉取并解包成层
- dockerd 准备网络(默认 bridge)、分配 IP、准备挂载点
- 交给 containerd,containerd 起一个 shim 进程托管容器
- shim 调 runc:创建命名空间、设 cgroups、pivot_root 到镜像根文件系统、exec 入口命令
- 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 psPort 2375 is plaintext and scanners find it within minutes. Use 2376 with client certificates, or tunnel over SSH.
四个角色
Text
docker CLI ──HTTP/Unix socket──▶ dockerd(守护进程)
│
├── 镜像管理、网络、卷
└── containerd ──▶ runc ──▶ 容器进程
▲
Registry(Docker Hub / 私有仓库)| 组件 | 职责 |
|---|---|
| docker(命令行) | 解析命令,通过 socket 与守护进程通信 |
| dockerd | 构建镜像、管理网络与卷、提供 API |
| containerd | 镜像拉取、容器生命周期、快照 |
| runc | 用命名空间与 cgroups 真正创建进程 |
| 仓库 | 存储与分发镜像 |
一次 docker run 发生了什么
- CLI 把请求发给
/var/run/docker.sock - dockerd 检查本地有没有该镜像,没有就去 Registry 拉取并解包成层
- dockerd 准备网络(默认 bridge)、分配 IP、准备挂载点
- 交给 containerd,containerd 起一个 shim 进程托管容器
- shim 调 runc:创建命名空间、设 cgroups、pivot_root 到镜像根文件系统、exec 入口命令
- runc 退出,容器进程由 shim 接管(所以 dockerd 重启不影响运行中的容器)
看架构
Bash
docker version # 客户端与服务端版本
docker info # 存储驱动、cgroup 版本、运行时
docker system df # 镜像/容器/卷占用的空间远程访问守护进程
Docker 默认只监听本地 Unix socket。要让别的机器访问,需要显式开放 TCP,并配置 TLS——裸露的 2375 端口等于把 root 权限交给整个网络。
Bash
# 客户端指向远程(生产必须走 TLS)
export DOCKER_HOST=tcp://docker.example.com:2376
export DOCKER_TLS_VERIFY=1
export DOCKER_CERT_PATH=$HOME/.docker/certs
docker ps2375 是明文端口,扫描器几分钟就能找到。要远程管理就用 2376 + 客户端证书,或者干脆走 SSH 隧道 / 跳板机。