Skip to content

02 - Docker 安装验证与基本命令

前置条件:确认 Docker Desktop 已安装

在 macOS 上,Docker Desktop 是最方便的选择。它内置了:

  • Docker Engine
  • Docker CLI
  • Docker Compose
  • Kubernetes(可选开启)

验证安装

bash
# 查看 Docker 版本
docker version

# 查看 Docker 系统信息(引擎、存储驱动、内核等)
docker info

# 运行测试容器,验证一切正常
docker run hello-world

如果 hello-world 运行成功并打印欢迎信息,说明 Docker 环境就绪。


镜像操作命令

查找和拉取镜像

bash
# 从 Docker Hub 搜索镜像
docker search nginx

# 拉取镜像(默认 tag 为 latest)
docker pull nginx

# 拉取指定版本
docker pull nginx:1.25-alpine

# 拉取并指定平台(M1/M2 Mac 有时需要)
docker pull --platform linux/amd64 nginx:1.25

管理本地镜像

bash
# 列出本地所有镜像
docker images
# 等同于
docker image ls

# 查看镜像详细信息
docker image inspect nginx

# 查看镜像的分层历史
docker image history nginx

# 删除镜像
docker rmi nginx
# 等同于
docker image rm nginx

# 清理所有未使用的镜像(悬空镜像)
docker image prune

# 清理所有未被容器引用的镜像
docker image prune -a

容器生命周期

  docker create        docker start        docker stop
  ───────────► Created ──────────► Running ──────────► Stopped
                                     │                    │
                                     │ docker pause       │ docker start
                                     ▼                    │
                                   Paused ────────────────┘
                                     docker unpause
                                     
  任何状态 ──── docker rm ────► Deleted

创建和运行容器

bash
# 最常用:运行一个容器(拉取 + 创建 + 启动)
docker run nginx

# 后台运行(detached 模式)
docker run -d nginx

# 指定容器名称
docker run -d --name my-nginx nginx

# 端口映射:宿主机 8080 → 容器 80
docker run -d -p 8080:80 --name my-nginx nginx

# 交互式运行(进入容器内 shell)
docker run -it ubuntu:22.04 /bin/bash

# 运行后自动删除容器
docker run --rm -it alpine sh

# 设置环境变量
docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql:8

# 限制资源
docker run -d --memory=512m --cpus=1.0 nginx

关键参数解释

参数含义
-dDetached,后台运行
-itInteractive + TTY,交互式终端
-p 宿主:容器端口映射
-v 宿主:容器数据卷挂载
-e KEY=VALUE设置环境变量
--name指定容器名称
--rm容器停止后自动删除
--network指定网络
--memory内存限制
--cpusCPU 限制

管理运行中的容器

bash
# 查看运行中的容器
docker ps

# 查看所有容器(包括已停止的)
docker ps -a

# 停止容器
docker stop my-nginx

# 启动已停止的容器
docker start my-nginx

# 重启容器
docker restart my-nginx

# 进入运行中的容器
docker exec -it my-nginx /bin/bash
# 如果容器没有 bash,用 sh
docker exec -it my-nginx /bin/sh

# 在容器中执行单条命令
docker exec my-nginx cat /etc/nginx/nginx.conf

# 查看容器日志
docker logs my-nginx
docker logs -f my-nginx          # 持续跟踪
docker logs --tail 100 my-nginx  # 最后 100 行

# 查看容器资源使用
docker stats
docker stats my-nginx

# 查看容器详细信息
docker inspect my-nginx

# 查看容器内进程
docker top my-nginx

删除容器

bash
# 删除已停止的容器
docker rm my-nginx

# 强制删除运行中的容器
docker rm -f my-nginx

# 删除所有已停止的容器
docker container prune

# 核弹选项:删除所有容器
docker rm -f $(docker ps -aq)

实操练习 Lab 01:运行你的第一个 Web 服务器

目标

在本地运行一个 Nginx Web 服务器,通过浏览器访问。

步骤

bash
# 1. 拉取 Nginx 镜像
docker pull nginx:alpine

# 2. 运行容器,映射端口
docker run -d \
  --name lab01-nginx \
  -p 8080:80 \
  nginx:alpine

# 3. 验证容器运行
docker ps

# 4. 浏览器访问 http://localhost:8080 ,应该看到 Nginx 欢迎页

# 5. 查看日志
docker logs lab01-nginx

# 6. 进入容器看看里面有什么
docker exec -it lab01-nginx sh
# 在容器内:
ls /usr/share/nginx/html/   # 网站根目录
cat /etc/nginx/nginx.conf   # Nginx 配置
exit

# 7. 清理
docker stop lab01-nginx
docker rm lab01-nginx

扩展练习

bash
# 挂载本地目录作为网站内容
mkdir -p /tmp/my-site
echo '<h1>Hello Docker!</h1>' > /tmp/my-site/index.html

docker run -d \
  --name lab01-custom \
  -p 8080:80 \
  -v /tmp/my-site:/usr/share/nginx/html:ro \
  nginx:alpine

# 访问 http://localhost:8080 应该看到 "Hello Docker!"
# 修改本地的 index.html,刷新浏览器,内容实时更新

# 清理
docker stop lab01-custom && docker rm lab01-custom

实操练习 Lab 02:多容器体验

目标

同时运行多个容器,理解容器隔离和端口映射。

bash
# 同时运行 3 个不同的 web 服务
docker run -d --name web1 -p 8081:80 nginx:alpine
docker run -d --name web2 -p 8082:80 httpd:alpine    # Apache
docker run -d --name web3 -p 8083:80 nginx:alpine

# 查看所有容器
docker ps

# 分别访问:
# http://localhost:8081 → Nginx
# http://localhost:8082 → Apache
# http://localhost:8083 → Nginx

# 查看资源占用
docker stats --no-stream

# 清理
docker stop web1 web2 web3
docker rm web1 web2 web3

思考题

  1. 为什么 web1 和 web3 都是 nginx 但可以同时运行?
  2. 如果两个容器都映射到宿主机 8080 端口会怎样?
  3. 容器内部的端口 80 冲突吗?为什么?

答案提示:每个容器有自己独立的网络命名空间(Namespace),容器内部的端口互不影响。冲突只发生在宿主机端口映射层面。


常用清理命令

bash
# 清理已停止的容器
docker container prune

# 清理未使用的镜像
docker image prune

# 清理未使用的卷
docker volume prune

# 清理未使用的网络
docker network prune

# 一键清理所有未使用的资源(慎用)
docker system prune

# 包括未使用的镜像
docker system prune -a

# 查看 Docker 磁盘使用情况
docker system df

下一步

03 - Dockerfile 编写与镜像构建