Docker Overview
什么 Docker 是, 如何容器工作, and 为什么它们 revolutionized software 部署.
你将学到
- 什么 Docker 是 and problem 它解决
- 容器对比虚拟 machines
- Docker 架构: engine, 镜像, and 容器
- 核心 Docker commands
概念讲解
What Is Docker?
Docker is a platform for building, running, and managing applications in containers. A container is a lightweight, standalone package that includes your application code plus everything it needs to run — libraries, system tools, configuration — all isolated from the host system. Docker solves the "它工作 on my machine" problem by guaranteeing that an application runs identically wherever its container runs.
Containers vs Virtual Machines
A virtual machine (VM) runs a full operating system with its own kernel, which makes it heavy and slow to start. A container shares the host operating system's kernel and only isolates the application processes. This makes containers much smaller (megabytes vs gigabytes) and faster to start (seconds vs minutes). You can run dozens of containers on a single machine where you could only fit a few VMs.
Docker Architecture
Docker uses a client-server architecture. The Docker client (docker command) talks to the Docker daemon (server), which builds, runs, and manages containers. An image is a read-only template with instructions for creating a container — think of it as a blueprint. A container is a running instance of an image. Images are stored in registries like Docker Hub or a private registry.
Core Commands
The essential Docker commands are simple. docker 构建 creates an image from a Dockerfile. docker 运行 starts a container from an image. docker ps lists running containers. docker 镜像 lists available images. docker 停止 and docker rm stop and remove containers. These commands are the foundation of all Docker workflows.
The Docker Ecosystem
Docker is more than just a container runtime. Docker Hub is a public registry with millions of pre-built images. Docker Compose orchestrates multi-container applications. Docker Swarm provides native clustering. The broader ecosystem includes Kubernetes for large-scale orchestration, containerd as a lightweight runtime, and Podman as a daemonless alternative. Understanding Docker is the entry point to this entire ecosystem — the concepts of images, containers, and registries carry over to every tool that builds on Docker's foundation.
示例代码
# Pull and run a ready-made image
docker pull nginx:alpine
docker run -d --name web -p 8080:80 nginx:alpine
# Inspect what is running
docker ps # list running containers
docker ps -a # list all containers (including stopped)
docker images # list available images
docker logs web # view container logs
# Stop and clean up
docker stop web
docker rm web
docker rmi nginx:alpine # remove the image
docker 拉取 downloads 镜像, docker 运行开始容器, docker ps 列表容器, and docker logs shows 输出. 这些是 daily Docker commands.
动手试一试
- Dockerfile Generator
Generate Dockerfiles for 你的 applications.
- Docker Compose Generator
创建 multi-容器 Docker Compose 配置.
- YAML Validator
验证 Docker Compose YAML files.
常见错误
错误写法
运行容器 as 根通过默认
正确做法
创建非-根用户 in 你的 Dockerfile and 切换到 它使用 USER. 运行 as 根内部容器是 security risk 如果 attacker escapes 容器.
错误写法
不 cleaning 上停止容器 and 镜像
正确做法
Use docker 系统 prune regularly 到移除 unused 容器, 镜像, and networks. 它们 accumulate 快速地 and consume disk 空间.
练习 Docker Overview
1 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。