Docker 教程

Docker 解决什么问题、三个核心概念,以及第一次跑起容器的完整流程。

Beginner
Key PointsDocker 把应用和它的依赖一起打包成镜像,镜像跑起来就是容器;同一份镜像在任何装了 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.

它解决什么问题

ProblemBeforeWith Docker
Environment drift「我本地能跑」镜像里带着整个环境
Dependency conflict两个应用要不同版本的 JDK各自独立镜像,互不干扰
Slow onboarding装半天环境一条 docker run
Density一台机器几个 VM一台机器几十个容器

三个核心概念

  • 镜像(Image) — 只读模板,由 Dockerfile 构建,分层存储
  • 容器(Container) — 镜像的运行实例,最上层多一个可写层
  • 仓库(Registry) — 存放镜像的服务,Docker Hub 是默认的公共仓库

第一次运行

Bash
docker run hello-world
Text
Unable 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.

学习路线

  1. 概念与架构 —— 镜像、容器、仓库、守护进程
  2. 安装 —— 选对平台,配好镜像加速
  3. 基本操作 —— run / ps / exec / logs / stop / rm
  4. 镜像 —— build / tag / push / 分层与缓存
  5. 编排 —— Dockerfile、Compose、Swarm

Friends & Partners