Docker 实操命令大全

Docker 实操命令大全

基础命令

查看 Docker 版本信息

bash 复制代码
docker version          # 查看 Docker 客户端和服务器版本
docker info            # 查看详细的 Docker 系统信息

查看帮助信息

bash 复制代码
docker --help          # 查看所有 Docker 命令
docker <command> --help # 查看特定命令的帮助信息

镜像管理

搜索镜像

bash 复制代码
docker search <镜像名>              # 在 Docker Hub 上搜索镜像
docker search nginx                # 搜索 nginx 镜像
docker search --limit 5 nginx      # 限制搜索结果数量

拉取镜像

bash 复制代码
docker pull <镜像名>                # 拉取最新版本的镜像
docker pull nginx                   # 拉取 nginx 最新版本
docker pull nginx:1.21              # 拉取指定版本的镜像
docker pull ubuntu:20.04            # 拉取 Ubuntu 20.04

查看镜像列表

bash 复制代码
docker images                       # 查看所有本地镜像
docker image ls                     # 同上(新语法)
docker images -a                    # 显示所有镜像(包括中间层)
docker images --filter "dangling=true"  # 显示悬空镜像
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"  # 自定义格式

查看镜像详细信息

bash 复制代码
docker inspect <镜像名或ID>         # 查看镜像详细信息
docker inspect nginx                # 查看 nginx 镜像详情
docker history <镜像名>             # 查看镜像构建历史
docker history nginx                # 查看 nginx 构建历史

删除镜像

bash 复制代码
docker rmi <镜像名或ID>             # 删除镜像
docker rmi nginx                    # 删除 nginx 镜像
docker rmi -f <镜像ID>              # 强制删除镜像
docker image prune                  # 删除未使用的镜像
docker image prune -a               # 删除所有未使用的镜像

构建镜像

bash 复制代码
docker build -t <镜像名> <路径>     # 构建镜像
docker build -t myapp:1.0 .         # 使用当前目录的 Dockerfile 构建
docker build -t myapp:1.0 -f Dockerfile.dev .  # 指定 Dockerfile
docker build --no-cache -t myapp:1.0 .  # 不使用缓存构建

导出和导入镜像

bash 复制代码
docker save -o <文件名>.tar <镜像名>    # 导出镜像为 tar 文件
docker save -o nginx.tar nginx          # 导出 nginx 镜像
docker load -i <文件名>.tar             # 从 tar 文件导入镜像
docker load -i nginx.tar                # 导入 nginx 镜像

标记镜像

bash 复制代码
docker tag <源镜像> <新镜像名>      # 为镜像打标签
docker tag nginx mynginx:latest    # 标记镜像

容器管理

创建和启动容器

bash 复制代码
docker run <镜像名>                 # 创建并启动容器
docker run nginx                    # 运行 nginx 容器
docker run -d nginx                 # 后台运行容器(detached)
docker run -it ubuntu /bin/bash     # 交互式运行容器
docker run -p 8080:80 nginx         # 端口映射(主机:容器)
docker run -v /host:/container nginx  # 挂载数据卷
docker run --name mynginx nginx     # 指定容器名称
docker run --rm nginx               # 容器退出后自动删除
docker run -e KEY=value nginx       # 设置环境变量

查看容器列表

bash 复制代码
docker ps                           # 查看运行中的容器
docker ps -a                        # 查看所有容器(包括已停止)
docker ps -l                        # 查看最近创建的容器
docker ps -q                        # 只显示容器 ID
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"  # 自定义格式

启动和停止容器

bash 复制代码
docker start <容器名或ID>           # 启动已停止的容器
docker start mynginx                # 启动容器
docker stop <容器名或ID>            # 停止运行中的容器
docker stop mynginx                 # 停止容器
docker restart <容器名或ID>         # 重启容器
docker restart mynginx              # 重启容器
docker pause <容器名或ID>          # 暂停容器
docker unpause <容器名或ID>         # 恢复暂停的容器

进入容器

bash 复制代码
docker exec -it <容器名或ID> <命令>  # 在运行中的容器中执行命令
docker exec -it mynginx /bin/bash   # 进入容器的 bash shell
docker exec -it mynginx sh          # 进入容器的 sh shell
docker exec mynginx ls -la          # 在容器中执行命令
docker attach <容器名或ID>          # 附加到容器的主进程(不推荐)

查看容器信息

bash 复制代码
docker inspect <容器名或ID>         # 查看容器详细信息
docker inspect mynginx              # 查看容器详情
docker inspect --format='{{.NetworkSettings.IPAddress}}' mynginx  # 查看 IP 地址
docker logs <容器名或ID>            # 查看容器日志
docker logs mynginx                 # 查看日志
docker logs -f mynginx              # 实时跟踪日志(类似 tail -f)
docker logs --tail 100 mynginx      # 查看最后 100 行日志
docker top <容器名或ID>             # 查看容器内运行的进程
docker stats <容器名或ID>           # 查看容器资源使用情况
docker stats                        # 查看所有容器的资源使用

删除容器

bash 复制代码
docker rm <容器名或ID>              # 删除已停止的容器
docker rm mynginx                   # 删除容器
docker rm -f <容器名或ID>           # 强制删除运行中的容器
docker rm -f mynginx                # 强制删除
docker container prune              # 删除所有已停止的容器

复制文件

bash 复制代码
docker cp <容器名>:<容器路径> <主机路径>    # 从容器复制到主机
docker cp mynginx:/etc/nginx/nginx.conf ./  # 复制配置文件
docker cp <主机路径> <容器名>:<容器路径>    # 从主机复制到容器
docker cp ./nginx.conf mynginx:/etc/nginx/  # 复制文件到容器

导出和导入容器

bash 复制代码
docker export -o <文件名>.tar <容器名>  # 导出容器为 tar 文件
docker export -o mycontainer.tar mynginx
docker import <文件名>.tar <镜像名>     # 从 tar 文件导入为镜像
docker import mycontainer.tar myimage:1.0

网络管理

查看网络

bash 复制代码
docker network ls                   # 查看所有网络
docker network inspect <网络名>     # 查看网络详细信息
docker network inspect bridge       # 查看 bridge 网络详情

创建网络

bash 复制代码
docker network create <网络名>      # 创建网络
docker network create mynetwork    # 创建自定义网络
docker network create --driver bridge mynetwork  # 指定驱动类型
docker network create --subnet=172.20.0.0/16 mynetwork  # 指定子网

连接和断开网络

bash 复制代码
docker network connect <网络名> <容器名>    # 将容器连接到网络
docker network connect mynetwork mynginx
docker network disconnect <网络名> <容器名> # 断开容器与网络
docker network disconnect mynetwork mynginx

删除网络

bash 复制代码
docker network rm <网络名>          # 删除网络
docker network rm mynetwork         # 删除网络
docker network prune                # 删除未使用的网络

在创建容器时指定网络

bash 复制代码
docker run --network <网络名> <镜像名>     # 使用指定网络运行容器
docker run --network mynetwork nginx
docker run --network host nginx            # 使用主机网络模式
docker run --network none nginx            # 不使用网络

数据卷管理

查看数据卷

bash 复制代码
docker volume ls                    # 查看所有数据卷
docker volume inspect <卷名>        # 查看数据卷详细信息
docker volume inspect myvolume      # 查看卷详情

创建数据卷

bash 复制代码
docker volume create <卷名>         # 创建数据卷
docker volume create myvolume       # 创建卷
docker volume create --driver local myvolume  # 指定驱动类型

删除数据卷

bash 复制代码
docker volume rm <卷名>             # 删除数据卷
docker volume rm myvolume           # 删除卷
docker volume prune                 # 删除未使用的数据卷

使用数据卷

bash 复制代码
# 创建容器时挂载数据卷
docker run -v <卷名>:<容器路径> <镜像名>
docker run -v myvolume:/data nginx

# 创建容器时挂载主机目录
docker run -v <主机路径>:<容器路径> <镜像名>
docker run -v /host/data:/container/data nginx

# 使用 --mount 参数(推荐,更灵活)
docker run --mount type=volume,source=myvolume,target=/data nginx
docker run --mount type=bind,source=/host/data,target=/container/data nginx

Docker Compose

基本命令

bash 复制代码
docker-compose up                   # 启动所有服务
docker-compose up -d                # 后台启动所有服务
docker-compose down                 # 停止并删除所有服务
docker-compose start                # 启动服务
docker-compose stop                 # 停止服务
docker-compose restart              # 重启服务
docker-compose pause                # 暂停服务
docker-compose unpause              # 恢复服务

查看和管理

bash 复制代码
docker-compose ps                   # 查看服务状态
docker-compose logs                 # 查看所有服务日志
docker-compose logs <服务名>        # 查看特定服务日志
docker-compose logs -f              # 实时跟踪日志
docker-compose exec <服务名> <命令> # 在服务中执行命令
docker-compose exec web bash        # 进入 web 服务的 bash

构建和更新

bash 复制代码
docker-compose build                # 构建所有服务的镜像
docker-compose build <服务名>       # 构建特定服务的镜像
docker-compose up --build           # 重新构建并启动
docker-compose pull                 # 拉取所有服务的镜像
docker-compose up -d --force-recreate  # 强制重新创建容器

扩展服务

bash 复制代码
docker-compose scale <服务名>=<数量>  # 扩展服务实例数量
docker-compose scale web=3          # 将 web 服务扩展到 3 个实例

日志与监控

查看日志

bash 复制代码
docker logs <容器名>                # 查看容器日志
docker logs -f <容器名>             # 实时跟踪日志
docker logs --tail 50 <容器名>      # 查看最后 50 行
docker logs --since 10m <容器名>    # 查看最近 10 分钟的日志
docker logs --until 2023-01-01 <容器名>  # 查看指定时间之前的日志

监控资源

bash 复制代码
docker stats                        # 实时监控所有容器的资源使用
docker stats <容器名>               # 监控特定容器
docker stats --no-stream            # 显示一次性的统计信息
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"

查看事件

bash 复制代码
docker events                       # 实时查看 Docker 事件
docker events --since 1h            # 查看最近 1 小时的事件
docker events --filter container=mynginx  # 过滤特定容器的事件

清理与维护

清理未使用的资源

bash 复制代码
docker system prune                 # 清理所有未使用的资源
docker system prune -a              # 清理所有未使用的资源(包括镜像)
docker system prune -a --volumes    # 同时清理数据卷
docker system df                    # 查看 Docker 磁盘使用情况

清理特定资源

bash 复制代码
docker container prune              # 删除所有已停止的容器
docker image prune                  # 删除未使用的镜像
docker image prune -a               # 删除所有未使用的镜像
docker volume prune                 # 删除未使用的数据卷
docker network prune                # 删除未使用的网络

查看系统信息

bash 复制代码
docker system info                  # 查看 Docker 系统信息
docker system df                    # 查看磁盘使用情况
docker system events                # 查看系统事件

实用技巧

批量操作

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

# 删除所有已停止的容器
docker rm $(docker ps -aq)

# 删除所有镜像
docker rmi $(docker images -q)

# 删除所有悬空镜像
docker rmi $(docker images -f "dangling=true" -q)

查看容器 IP 地址

bash 复制代码
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <容器名>
docker inspect <容器名> | grep IPAddress

查看容器端口映射

bash 复制代码
docker port <容器名>                # 查看容器的端口映射
docker port mynginx                 # 查看端口映射

查看容器资源限制

bash 复制代码
docker inspect <容器名> | grep -i memory
docker inspect <容器名> | grep -i cpu

设置容器资源限制

bash 复制代码
docker run -m 512m nginx            # 限制内存为 512MB
docker run --cpus="1.5" nginx       # 限制 CPU 为 1.5 核
docker run --cpuset-cpus="0-3" nginx  # 限制使用 CPU 0-3

健康检查

bash 复制代码
docker inspect --format='{{json .State.Health}}' <容器名>  # 查看健康状态

导出和导入完整容器

bash 复制代码
# 导出容器(包括所有层)
docker save -o container.tar <镜像名>

# 导入容器
docker load -i container.tar

查看 Dockerfile 构建过程

bash 复制代码
docker build --progress=plain -t myapp .  # 显示详细构建过程
docker build --no-cache -t myapp .        # 不使用缓存构建

多阶段构建优化

bash 复制代码
# 在 Dockerfile 中使用多阶段构建可以减少最终镜像大小
# 示例:
# FROM node:16 AS builder
# WORKDIR /app
# COPY . .
# RUN npm install && npm run build
# 
# FROM nginx:alpine
# COPY --from=builder /app/dist /usr/share/nginx/html

使用 .dockerignore

bash 复制代码
# 创建 .dockerignore 文件来排除不需要的文件
# 可以加快构建速度并减小镜像大小

查看镜像层信息

bash 复制代码
docker history <镜像名>             # 查看镜像构建历史
docker history --no-trunc <镜像名>  # 显示完整命令

容器间通信

bash 复制代码
# 使用容器名称进行通信(在同一网络中)
docker run --name app1 --network mynetwork myapp
docker run --name app2 --network mynetwork myapp
# app1 和 app2 可以通过容器名互相访问

环境变量管理

bash 复制代码
# 从文件读取环境变量
docker run --env-file .env nginx

# 传递多个环境变量
docker run -e VAR1=value1 -e VAR2=value2 nginx

容器自动重启策略

bash 复制代码
docker run --restart=always nginx   # 总是重启
docker run --restart=unless-stopped nginx  # 除非手动停止,否则重启
docker run --restart=on-failure:5 nginx    # 失败时重启,最多 5 次

常用命令组合

一键清理所有未使用资源

bash 复制代码
docker system prune -a --volumes --force

查看容器详细信息(格式化输出)

bash 复制代码
docker inspect --format='{{.Name}} - {{.NetworkSettings.IPAddress}}' $(docker ps -q)

批量查看容器日志

bash 复制代码
docker ps -q | xargs docker logs --tail 50

导出所有镜像

bash 复制代码
docker images --format "{{.Repository}}:{{.Tag}}" | xargs -I {} docker save {} -o {}.tar

故障排查

查看容器退出原因

bash 复制代码
docker inspect <容器名> | grep -A 10 "State"

查看容器启动失败原因

bash 复制代码
docker logs <容器名>
docker inspect <容器名>

进入已停止的容器

bash 复制代码
# 先启动容器,然后进入
docker start <容器名>
docker exec -it <容器名> /bin/bash

查看容器资源使用情况

bash 复制代码
docker stats --no-stream <容器名>
docker top <容器名>

总结

本文涵盖了 Docker 日常使用中最常用的命令,包括:

  • 镜像管理:搜索、拉取、构建、删除镜像
  • 容器管理:创建、启动、停止、删除容器
  • 网络管理:创建网络、连接容器
  • 数据卷管理:创建和管理数据卷
  • Docker Compose:多容器应用管理
  • 监控和日志:查看容器状态和日志
  • 清理维护:系统清理和优化

掌握这些命令,可以大大提高 Docker 的使用效率。建议结合实际项目多加练习,熟能生巧。


提示

  • 使用 docker <command> --help 可以查看任何命令的详细帮助
  • 定期清理未使用的资源可以节省磁盘空间
  • 使用 Docker Compose 管理多容器应用更加方便
  • 生产环境建议使用具体的镜像标签而不是 latest
相关推荐
Linux编程用C15 小时前
Docker+Vscode搭建(本地/远程)开发环境
vscode·后端·docker
林疏safe17 小时前
灯塔部署云服务器docker 部署方式,以及忘记密码如何查找
运维·服务器·docker
木卫二号Coding17 小时前
affine+docker+postgresql+备份数据库
数据库·docker·容器
檀越剑指大厂17 小时前
查看 Docker 镜像详情的几种常用方法
docker·容器·eureka
java_logo18 小时前
Webtop Docker 容器化部署指南:基于浏览器的Linux桌面环境
linux·docker·容器·webtop·webtop部署教程·docker部署webtop·linux桌面
技术小李...20 小时前
docker下mysql更改密码后WordPress提示无法连接数据库问题
运维·docker·容器
JPX-NO1 天前
windows下编程IDE使用docker搭建的rust开发环境(Linux)
ide·windows·docker·rust
快乐就去敲代码@!1 天前
Boot Cache Star ⭐(高性能两级缓存系统)
spring boot·redis·后端·缓存·docker·压力测试
爱学大树锯1 天前
在Docker环境中安装RabbitMQ延迟消息插件实战记录
docker·容器·rabbitmq