Docker 教程
Docker 解决什么问题、三个核心概念,以及第一次跑起容器的完整流程。
Docker 是什么
Docker is a container platform: it packages code, runtime, system libraries and config into an image, then runs it as a container anywhere Docker is installed. It does not virtualise hardware — it shares the host kernel, isolating with namespaces and limiting with cgroups, so startup is sub-second and overhead is near zero.
它解决什么问题
| Problem | Before | With Docker |
|---|---|---|
| Environment drift | 「我本地能跑」 | 镜像里带着整个环境 |
| Dependency conflict | 两个应用要不同版本的 JDK | 各自独立镜像,互不干扰 |
| Slow onboarding | 装半天环境 | 一条 docker run |
| Density | 一台机器几个 VM | 一台机器几十个容器 |
三个核心概念
- 镜像(Image) — 只读模板,由 Dockerfile 构建,分层存储
- 容器(Container) — 镜像的运行实例,最上层多一个可写层
- 仓库(Registry) — 存放镜像的服务,Docker Hub 是默认的公共仓库
第一次运行
docker run hello-worldUnable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Hello from Docker!
This message shows that your installation appears to be working correctly.That single command did four things: no local image → pull from Docker Hub → create a container → run it. The container still exists after exit (docker ps -a), unless you add --rm.
docker run is docker pull + docker create + docker start plus attaching a terminal. Once that clicks, docker create / docker start stop looking redundant.学习路线
- 概念与架构 —— 镜像、容器、仓库、守护进程
- 安装 —— 选对平台,配好镜像加速
- 基本操作 —— run / ps / exec / logs / stop / rm
- 镜像 —— build / tag / push / 分层与缓存
- 编排 —— Dockerfile、Compose、Swarm
Docker 是什么
Docker 是一个容器化平台:把应用代码、运行时、系统库、配置一起打成镜像,在任何装了 Docker 的机器上以容器的形式运行。它不虚拟硬件,而是复用宿主机内核,用命名空间做隔离、用 cgroups 做资源限制,所以启动是秒级、开销接近零。
它解决什么问题
| 问题 | 没有容器时 | 用容器后 |
|---|---|---|
| 环境不一致 | 「我本地能跑」 | 镜像里带着整个环境 |
| 依赖冲突 | 两个应用要不同版本的 JDK | 各自独立镜像,互不干扰 |
| 新人上手慢 | 装半天环境 | 一条 docker run |
| 部署密度 | 一台机器几个 VM | 一台机器几十个容器 |
三个核心概念
- 镜像(Image) —— 只读模板,由 Dockerfile 构建,分层存储
- 容器(Container) —— 镜像的运行实例,最上层多一个可写层
- 仓库(Registry) —— 存放镜像的服务,Docker Hub 是默认的公共仓库
第一次运行
docker run hello-worldUnable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Hello from Docker!
This message shows that your installation appears to be working correctly.这一条命令做了四件事:本地找不到镜像 → 去 Docker Hub 拉取 → 创建容器 → 运行并输出。容器退出后仍然存在(docker ps -a 能看到),加 --rm 可以退出即删。
docker run = docker pull + docker create + docker start + 附加终端。理解这一点,后面看到 docker create / docker start 就不会觉得多余。学习路线
- 概念与架构 —— 镜像、容器、仓库、守护进程
- 安装 —— 选对平台,配好镜像加速
- 基本操作 —— run / ps / exec / logs / stop / rm
- 镜像 —— build / tag / push / 分层与缓存
- 编排 —— Dockerfile、Compose、Swarm