Skip to content

容器管理

容器是 Docker 的运行单元,本节介绍容器的创建、运行、停止、删除、进入、日志等完整操作。

创建和运行容器

docker run — 创建并运行

docker run 是最常用的命令,一步完成镜像拉取(如需)、容器创建和启动:

bash
# 基本用法
docker run nginx

# 后台运行
docker run -d nginx

# 指定名称
docker run -d --name web nginx

# 端口映射
docker run -d -p 8080:80 nginx

# 多端口映射
docker run -d -p 8080:80 -p 443:443 nginx

# 交互式运行
docker run -it ubuntu bash

# 运行后自动删除(适用于临时任务)
docker run --rm ubuntu echo "hello"

# 设置环境变量
docker run -d -e MY_VAR=hello nginx

# 挂载目录
docker run -d -v /host/path:/container/path nginx

# 设置重启策略
docker run -d --restart always nginx

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

docker create — 仅创建不启动

bash
# 创建容器但不启动
docker create --name mycontainer nginx

# 之后手动启动
docker start mycontainer

查看容器

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

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

# 只显示容器 ID
docker ps -q

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

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

# 查看容器资源使用情况(实时)
docker stats

# 查看指定容器资源使用
docker stats my-nginx --no-stream

docker ps 输出说明:

CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS        PORTS                  NAMES
3f4c8b9a1234   nginx     "/docker-entrypoint.…"   2 hours ago    Up 2 hours    0.0.0.0:80->80/tcp     web

启动、停止、重启容器

bash
# 停止容器(发送 SIGTERM,等待优雅退出)
docker stop my-nginx

# 强制停止(发送 SIGKILL)
docker kill my-nginx

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

# 重启容器
docker restart my-nginx

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

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

进入容器

docker exec — 在运行中的容器执行命令

bash
# 进入容器的交互式 bash
docker exec -it my-nginx bash

# 如果没有 bash,尝试 sh
docker exec -it my-nginx sh

# 执行单个命令
docker exec my-nginx nginx -t

# 以指定用户执行
docker exec -u root my-nginx bash

# 设置环境变量
docker exec -e MY_VAR=test my-nginx env

docker attach — 附加到容器主进程

bash
# 附加到容器的主进程
docker attach my-nginx
# 注意:退出时按 Ctrl+P, Ctrl+Q 可以退出而不停止容器
# 直接按 Ctrl+C 或输入 exit 会停止容器

推荐使用 docker exec 而非 docker attach,因为 exec 不会因为退出而停止容器。

查看容器日志

bash
# 查看容器日志
docker logs my-nginx

# 实时跟踪日志(类似 tail -f)
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
docker logs --since 1h my-nginx

# 组合使用
docker logs -f --tail 50 my-nginx

删除容器

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

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

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

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

文件复制

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

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

查看容器内的进程

bash
# 查看容器内运行的进程
docker top my-nginx

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

重启策略

--restart 选项控制容器异常退出后的重启行为:

策略说明
no不自动重启(默认)
on-failure退出码非 0 时重启
on-failure:3退出码非 0 时最多重启 3 次
always总是重启(包括 Docker 重启后)
unless-stopped总是重启,除非手动 stop
bash
# 设置生产环境推荐的重启策略
docker run -d --restart unless-stopped nginx

# 更新已有容器的重启策略
docker update --restart always my-nginx

容器资源限制

bash
# 限制内存(超出则 OOM Kill)
docker run -d --memory 512m nginx

# 限制内存+交换
docker run -d --memory 512m --memory-swap 1g nginx

# 限制 CPU(1.5 表示最多使用 1.5 个 CPU 核心)
docker run -d --cpus 1.5 nginx

# 限制 CPU 份额(相对权重,默认 1024)
docker run -d --cpu-shares 512 nginx

# 查看容器资源限制
docker inspect my-nginx | grep -i memory

实用技巧

批量操作

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

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

# 清理已退出的容器
docker container prune -f

一次性临时容器

bash
# 运行完自动删除(--rm)
docker run --rm node:alpine node -e "console.log('hello')"
docker run --rm python:alpine python -c "print('hello')"
docker run --rm alpine curl https://api.github.com

容器名称的好习惯

始终给容器起有意义的名称,便于管理:

bash
docker run -d --name prod-nginx nginx
docker run -d --name dev-mysql mysql:8.0
docker run -d --name test-redis redis:alpine

总结

容器管理的核心命令:

操作命令
创建并运行docker run
查看容器docker ps
停止容器docker stop
启动容器docker start
重启容器docker restart
进入容器docker exec -it
查看日志docker logs
删除容器docker rm
查看详情docker inspect
查看状态docker stats
文件复制docker cp

下一节将介绍容器的完整生命周期。