1. Docker 入门基础
Docker 是一个开源的容器化平台,它让应用的打包、交付和运行变得更加简单。与传统的虚拟机不同,Docker 容器共享宿主机的操作系统内核,因此启动速度更快、资源占用更少。
1.1 核心概念
- 镜像(Image):应用的只读模板,包含代码、运行时、依赖和配置。
- 容器(Container):镜像的运行实例,可启动、停止、删除。
- Dockerfile:构建镜像的脚本文件,定义了镜像的构建步骤。
- 仓库(Registry):存储和分发镜像的地方,如 Docker Hub。
1.2 安装 Docker
以 Ubuntu 为例,安装 Docker Engine:
bash
# 卸载旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
# 更新软件包索引
sudo apt-get update
# 安装依赖
sudo apt-get install -y ca-certificates curl gnupg
# 添加 Docker 官方 GPG 密钥
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 添加 Docker 软件源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装 Docker
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# 验证安装
sudo docker --version
1.3 验证 Docker 是否可用
bash
# 查看 Docker 版本
docker version
# 运行 Hello World 容器
docker run hello-world
# 查看本地镜像
docker images
如果看到 "Hello from Docker!" 的输出,说明 Docker 已成功安装并运行。
2. Docker 核心操作实践
2.1 镜像管理
bash
# 拉取镜像
docker pull nginx:latest
# 查看本地镜像
docker images
# 删除镜像
docker rmi nginx:latest
# 搜索镜像
docker search nginx
2.2 容器生命周期管理
bash
# 运行容器(前台)
docker run nginx
# 运行容器(后台)
docker run -d --name my-nginx nginx
# 查看运行中的容器
docker ps
# 查看所有容器(含已停止)
docker ps -a
# 停止容器
docker stop my-nginx
# 启动已停止的容器
docker start my-nginx
# 进入容器
docker exec -it my-nginx bash
# 删除容器
docker rm my-nginx
2.3 端口映射与数据卷
bash
# 端口映射:宿主机 8080 -> 容器 80
docker run -d -p 8080:80 --name web nginx
# 数据卷:挂载宿主机目录到容器
docker run -d -p 8080:80 -v /home/user/html:/usr/share/nginx/html --name web nginx
# 查看容器日志
docker logs -f web
2.4 编写 Dockerfile
以部署一个 Node.js 应用为例:
dockerfile
# 基础镜像
FROM node:18-alpine
# 设置工作目录
WORKDIR /app
# 复制依赖清单
COPY package*.json ./
# 安装依赖
RUN npm install
# 复制源码
COPY . .
# 暴露端口
EXPOSE 3000
# 启动命令
CMD ["node", "server.js"]
构建并运行:
bash
# 构建镜像
docker build -t my-node-app .
# 运行容器
docker run -d -p 3000:3000 --name my-app my-node-app
3. Docker Compose 多容器编排
3.1 什么是 Docker Compose
Docker Compose 用于定义和运行多容器应用。通过一个 docker-compose.yml 文件,可以一次性启动多个相互关联的服务。
3.2 安装 Docker Compose
bash
# 下载 Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 赋予执行权限
sudo chmod +x /usr/local/bin/docker-compose
# 验证安装
docker-compose --version
3.3 编写 docker-compose.yml
以部署一个 Web 应用 + MySQL 为例:
yaml
version: "3.8"
services:
web:
build: .
ports:
- "8080:3000"
depends_on:
- db
environment:
- DB_HOST=db
- DB_USER=root
- DB_PASSWORD=123456
db:
image: mysql:8.0
environment:
- MYSQL_ROOT_PASSWORD=123456
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
3.4 Compose 常用命令
bash
# 启动所有服务
docker-compose up -d
# 查看服务状态
docker-compose ps
# 查看日志
docker-compose logs -f
# 停止服务
docker-compose down
# 停止并删除数据卷
docker-compose down -v
# 重新构建并启动
docker-compose up -d --build
4. 生产环境部署与优化
4.1 镜像优化技巧
dockerfile
# 多阶段构建:减小镜像体积
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
4.2 日志与监控
bash
# 配置 Docker 日志轮转
docker run -d \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
--name my-app my-node-app
4.3 资源限制
bash
# 限制 CPU 和内存
docker run -d \
--cpus="1.5" \
--memory="512m" \
--name my-app my-node-app
4.4 部署流程总结
#mermaid-svg-QV5zESOBInlXxwDe{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-QV5zESOBInlXxwDe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-QV5zESOBInlXxwDe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-QV5zESOBInlXxwDe .error-icon{fill:#552222;}#mermaid-svg-QV5zESOBInlXxwDe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-QV5zESOBInlXxwDe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-QV5zESOBInlXxwDe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-QV5zESOBInlXxwDe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-QV5zESOBInlXxwDe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-QV5zESOBInlXxwDe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-QV5zESOBInlXxwDe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-QV5zESOBInlXxwDe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-QV5zESOBInlXxwDe .marker.cross{stroke:#333333;}#mermaid-svg-QV5zESOBInlXxwDe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-QV5zESOBInlXxwDe p{margin:0;}#mermaid-svg-QV5zESOBInlXxwDe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-QV5zESOBInlXxwDe .cluster-label text{fill:#333;}#mermaid-svg-QV5zESOBInlXxwDe .cluster-label span{color:#333;}#mermaid-svg-QV5zESOBInlXxwDe .cluster-label span p{background-color:transparent;}#mermaid-svg-QV5zESOBInlXxwDe .label text,#mermaid-svg-QV5zESOBInlXxwDe span{fill:#333;color:#333;}#mermaid-svg-QV5zESOBInlXxwDe .node rect,#mermaid-svg-QV5zESOBInlXxwDe .node circle,#mermaid-svg-QV5zESOBInlXxwDe .node ellipse,#mermaid-svg-QV5zESOBInlXxwDe .node polygon,#mermaid-svg-QV5zESOBInlXxwDe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-QV5zESOBInlXxwDe .rough-node .label text,#mermaid-svg-QV5zESOBInlXxwDe .node .label text,#mermaid-svg-QV5zESOBInlXxwDe .image-shape .label,#mermaid-svg-QV5zESOBInlXxwDe .icon-shape .label{text-anchor:middle;}#mermaid-svg-QV5zESOBInlXxwDe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-QV5zESOBInlXxwDe .rough-node .label,#mermaid-svg-QV5zESOBInlXxwDe .node .label,#mermaid-svg-QV5zESOBInlXxwDe .image-shape .label,#mermaid-svg-QV5zESOBInlXxwDe .icon-shape .label{text-align:center;}#mermaid-svg-QV5zESOBInlXxwDe .node.clickable{cursor:pointer;}#mermaid-svg-QV5zESOBInlXxwDe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-QV5zESOBInlXxwDe .arrowheadPath{fill:#333333;}#mermaid-svg-QV5zESOBInlXxwDe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-QV5zESOBInlXxwDe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-QV5zESOBInlXxwDe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QV5zESOBInlXxwDe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-QV5zESOBInlXxwDe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QV5zESOBInlXxwDe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-QV5zESOBInlXxwDe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-QV5zESOBInlXxwDe .cluster text{fill:#333;}#mermaid-svg-QV5zESOBInlXxwDe .cluster span{color:#333;}#mermaid-svg-QV5zESOBInlXxwDe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-QV5zESOBInlXxwDe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-QV5zESOBInlXxwDe rect.text{fill:none;stroke-width:0;}#mermaid-svg-QV5zESOBInlXxwDe .icon-shape,#mermaid-svg-QV5zESOBInlXxwDe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QV5zESOBInlXxwDe .icon-shape p,#mermaid-svg-QV5zESOBInlXxwDe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-QV5zESOBInlXxwDe .icon-shape .label rect,#mermaid-svg-QV5zESOBInlXxwDe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QV5zESOBInlXxwDe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-QV5zESOBInlXxwDe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-QV5zESOBInlXxwDe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 编写 Dockerfile
构建镜像
推送到镜像仓库
服务器拉取镜像
运行容器
配置反向代理
监控与日志
4.5 常用运维命令
bash
# 查看容器资源占用
docker stats
# 清理无用资源
docker system prune -a
# 查看容器详细信息
docker inspect my-app
# 容器自启动
docker run -d --restart=always --name my-app my-node-app
5. 总结与学习资源
通过本教程,你已经掌握了 Docker 的核心概念、镜像与容器管理、Docker Compose 多容器编排,以及生产环境的部署优化技巧。从入门到精通,关键在于多动手实践,把每个命令都亲手敲一遍。
推荐学习资源
- Docker 官方文档:https://docs.docker.com/
- Docker Hub 镜像仓库:https://hub.docker.com/
- Docker Compose 文档:https://docs.docker.com/compose/
- Play with Docker 在线练习:https://labs.play-with-docker.com/
- Docker 官方 GitHub:https://github.com/docker