Skip to content

Docker 容器操作

本章将详细介绍 Docker 容器的完整操作流程,包括创建、启动、停止、删除容器,以及容器的监控和日常管理。

容器生命周期

    docker create         docker start         docker pause
  ┌──────────────┐    ┌──────────────┐    ┌──────────────┐
  │   Created    │───►│   Running    │───►│   Paused     │
  └──────────────┘    └──────┬───────┘    └──────┬───────┘
                             │                    │
                             │ docker stop        │ docker unpause
                             ▼                    ▼
                      ┌──────────────┐    ┌──────────────┐
                      │   Stopped    │    │   Running    │
                      └──────┬───────┘    └──────────────┘
                             │ docker rm

                      ┌──────────────┐
                      │   Removed    │
                      └──────────────┘

创建和运行容器

docker run 基本用法

bash
# 最简单的运行方式
docker run nginx

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

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

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

# 交互式运行(-it 表示交互式终端)
docker run -it ubuntu:22.04 /bin/bash

常用 run 参数

参数说明示例
-d后台运行docker run -d nginx
-p端口映射-p 8080:80
-v数据卷挂载-v /host/path:/container/path
-e设置环境变量-e MYSQL_ROOT_PASSWORD=123456
--name指定容器名--name my-app
--rm退出时自动删除docker run --rm nginx
--restart重启策略--restart=always
-m内存限制-m 512m
--cpusCPU 限制--cpus 2
--network指定网络--network my-net

docker create

创建容器但不启动:

bash
docker create --name my-nginx -p 8080:80 nginx
docker start my-nginx

容器管理

查看容器

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

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

# 只显示容器 ID
docker ps -q

# 格式化输出
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}"

# 按条件过滤
docker ps -f "status=running"
docker ps -f "name=nginx"

启动、停止和重启

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

# 停止运行中的容器(发送 SIGTERM,10秒后 SIGKILL)
docker stop my-nginx

# 立即强制停止
docker kill my-nginx

# 重启容器
docker restart my-nginx

# 暂停容器(冻结进程)
docker pause my-nginx

# 恢复暂停的容器
docker unpause my-nginx

删除容器

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

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

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

# 删除所有容器
docker rm -f $(docker ps -aq)

容器交互

进入运行中的容器

bash
# 使用 exec 进入容器(推荐)
docker exec -it my-nginx /bin/bash

# 使用 sh(如果容器没有 bash)
docker exec -it my-nginx /bin/sh

# 以 root 用户进入
docker exec -it -u root my-nginx /bin/bash

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

查看容器日志

bash
# 查看全部日志
docker logs my-nginx

# 实时跟踪日志
docker logs -f my-nginx

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

# 显示时间戳
docker logs -t my-nginx

# 查看指定时间之后的日志
docker logs --since "2024-01-01T00:00:00" my-nginx

查看容器详情

bash
# 查看容器完整信息
docker inspect my-nginx

# 查看容器 IP 地址
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-nginx

# 查看容器端口映射
docker port my-nginx

容器资源监控

bash
# 实时查看容器资源使用
docker stats

# 查看指定容器
docker stats my-nginx

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

输出示例:

CONTAINER ID   NAME       CPU %   MEM USAGE / LIMIT   MEM %   NET I/O       BLOCK I/O
a1b2c3d4e5f6   my-nginx   0.00%   2.5MiB / 7.7GiB    0.03%   1.2kB / 0B    0B / 0B

容器与宿主机文件传输

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

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

# 复制目录
docker cp ./config/ my-nginx:/etc/app/config/

容器导出与导入

bash
# 导出容器为 tar 文件
docker export my-nginx > nginx-container.tar

# 从 tar 文件导入为镜像
docker import nginx-container.tar my-nginx-image:v1

容器重启策略

策略说明
no默认,不自动重启
on-failure[:max]非正常退出时重启,可设最大次数
always总是重启
unless-stopped总是重启,除非手动停止
bash
# 设置重启策略
docker run -d --restart=always --name my-nginx nginx

# 修改已有容器的重启策略
docker update --restart=unless-stopped my-nginx

实用技巧

批量操作

bash
# 停止所有运行中的容器
docker stop $(docker ps -q)

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

# 删除所有容器(包括运行中的)
docker rm -f $(docker ps -aq)

容器资源限制

bash
# 限制内存和 CPU
docker run -d -m 256m --cpus 0.5 --name limited-nginx nginx

# 修改已有容器的资源限制
docker update -m 512m --cpus 1 limited-nginx

本章小结

掌握容器操作是使用 Docker 的基础。通过本章学习,你应该能够熟练地创建、管理和监控 Docker 容器。

关键要点:

  • docker run 是最常用的命令,掌握其常用参数
  • 使用 docker exec 进入容器进行调试
  • docker logsdocker stats 是排查问题的利器
  • 合理设置重启策略和资源限制

延伸阅读