第一个容器 #

运行Hello World容器 #

最简单的容器运行 #

bash
docker run hello-world

输出结果:

text
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

运行过程解析 #

text
┌─────────────────────────────────────────────────┐
│ 1. 检查本地是否存在hello-world镜像              │
│    ↓ (不存在)                                   │
│ 2. 从Docker Hub拉取镜像                         │
│    ↓                                            │
│ 3. 创建容器                                     │
│    ↓                                            │
│ 4. 启动容器并执行命令                           │
│    ↓                                            │
│ 5. 输出结果后容器退出                           │
└─────────────────────────────────────────────────┘

运行交互式容器 #

进入Ubuntu容器 #

bash
docker run -it ubuntu bash

参数说明:

  • -i: 保持STDIN打开
  • -t: 分配一个伪终端
  • ubuntu: 镜像名称
  • bash: 要执行的命令

在容器中操作 #

bash
# 在容器内部
root@container_id:/# cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
...

# 安装软件
root@container_id:/# apt-get update
root@container_id:/# apt-get install -y vim

# 退出容器
root@container_id:/# exit

运行后台容器 #

启动Nginx服务 #

bash
docker run -d -p 8080:80 --name my-nginx nginx

参数说明:

  • -d: 后台运行
  • -p 8080:80: 端口映射,宿主机8080映射到容器80
  • --name my-nginx: 指定容器名称
  • nginx: 镜像名称

验证服务 #

bash
# 查看容器状态
docker ps

# 访问服务
curl http://localhost:8080

# 或在浏览器打开 http://localhost:8080

常用容器操作命令 #

查看容器 #

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

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

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

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

容器生命周期管理 #

bash
# 停止容器
docker stop my-nginx

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

# 重启容器
docker restart my-nginx

# 暂停容器
docker pause my-nginx

# 恢复容器
docker unpause my-nginx

# 删除容器
docker rm my-nginx

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

进入运行中的容器 #

bash
# 使用exec进入容器
docker exec -it my-nginx bash

# 使用attach连接容器
docker attach my-nginx

查看容器日志 #

bash
# 查看日志
docker logs my-nginx

# 实时查看日志
docker logs -f my-nginx

# 查看最近100行日志
docker logs --tail 100 my-nginx

# 查看指定时间段的日志
docker logs --since 2h my-nginx

容器与宿主机文件交互 #

bash
# 从宿主机复制文件到容器
docker cp ./index.html my-nginx:/usr/share/nginx/html/

# 从容器复制文件到宿主机
docker cp my-nginx:/etc/nginx/nginx.conf ./nginx.conf

实践:运行一个Web应用 #

步骤1:拉取镜像 #

bash
docker pull nginx:alpine

步骤2:运行容器 #

bash
docker run -d \
  --name web-server \
  -p 8080:80 \
  -v $(pwd)/html:/usr/share/nginx/html \
  nginx:alpine

步骤3:创建测试页面 #

bash
mkdir -p html
echo '<h1>Hello Docker!</h1>' > html/index.html

步骤4:验证 #

bash
curl http://localhost:8080
# 输出: <h1>Hello Docker!</h1>

步骤5:清理 #

bash
docker stop web-server
docker rm web-server

容器状态详解 #

text
┌─────────────────────────────────────────────────────┐
│                    容器生命周期                      │
├─────────────────────────────────────────────────────┤
│                                                     │
│   Created ──→ Running ──→ Paused ──→ Running       │
│      │           │                                  │
│      │           ↓                                  │
│      │       Stopped ──→ Running                    │
│      │           │                                  │
│      └───────────┴──→ Removed                      │
│                                                     │
└─────────────────────────────────────────────────────┘
状态 说明
Created 容器已创建,但未启动
Running 容器正在运行
Paused 容器已暂停
Stopped 容器已停止
Removed 容器已删除

docker run 命令详解 #

常用参数 #

参数 说明
-d 后台运行容器
-i 保持STDIN打开
-t 分配伪终端
-p 端口映射
-P 随机端口映射
-v 挂载卷
--name 指定容器名称
--rm 容器退出后自动删除
-e 设置环境变量
--restart 重启策略
--network 指定网络
-h 设置主机名

重启策略 #

bash
# 不自动重启(默认)
docker run --restart=no nginx

# 总是重启
docker run --restart=always nginx

# 退出状态非0时重启
docker run --restart=on-failure nginx

# 总是重启,并设置重启延迟
docker run --restart=always --restart-delay=10s nginx

资源限制 #

bash
# 限制内存
docker run -m 512m nginx

# 限制CPU
docker run --cpus=1.5 nginx

# 限制CPU份额
docker run --cpu-shares=512 nginx

# 组合使用
docker run -d \
  --name limited-container \
  -m 512m \
  --cpus=1 \
  nginx

小结 #

本节学习了Docker容器的基本操作,包括运行容器、查看容器状态、进入容器、查看日志等。这些是使用Docker的基础技能。

下一步 #

接下来,让我们学习 Docker架构与核心概念,深入了解Docker的工作原理。