Docker 速查手册

🔍 系统信息

bash 复制代码
docker --version                # 版本
docker version                  # 客户端+服务端详细版本
docker info                     # docker系统整体信息
docker <cmd> --help             # 命令帮助

📦 镜像 Image

bash 复制代码
docker search nginx                     # 搜索镜像
docker pull nginx                       # 拉取 latest
docker pull nginx:1.27                  # 指定tag
docker images                           # 列出本地镜像
docker image ls

docker build -t myapp:v1 .              # 构建镜像,Dockerfile在当前目录
docker build -t myapp:v1 -f ./Dockerfile.dev . # 指定Dockerfile文件

docker rmi <image-id|name:tag>          # 删除镜像
docker rmi -f <image-id>                # 强制删除

docker save -o nginx.tar nginx:1.27     # 导出镜像到tar
docker load -i nginx.tar                # 从tar导入镜像

docker history nginx:1.27               # 查看镜像分层

🚀 容器 Container

run 参数说明

bash 复制代码
-d                            # 后台运行
--name NAME                   # 指定容器名称
-p HOST:CONTAINER             # 端口映射
-v HOST_PATH:CONTAINER_PATH   # 挂载目录
-e KEY=VALUE                  # 设置环境变量
--restart=always              # 开机自启
--rm                          # 停止自动删除容器(测试用)
-it                           # 交互式终端

示例:后台启动nginx

bash 复制代码
docker run -d --name nginx-demo -p 8080:80 --restart=always nginx

# 交互式临时容器
docker run -it --rm ubuntu /bin/bash

docker ps                     # 运行中容器
docker ps -a                  # 全部容器(含已停止)

docker start <name/id>
docker stop <name/id>
docker restart <name/id>
docker kill <name/id>         # 强制杀死

docker rm <name/id>           # 删除已停止容器
docker rm -f <name/id>        # 强制删除运行中容器

# 进入容器
docker exec -it nginx-demo /bin/bash
docker exec -it nginx-demo sh  # 无bash时用sh

# 文件拷贝 宿主机 ↔ 容器
docker cp /host/file 容器ID:/container/path
docker cp 容器ID:/container/file /host/path

# 查看容器完整元信息 json
docker inspect <id>

# 获取容器IP
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <id>

# 查看端口映射
docker port <id>

📋 日志与资源监控

bash 复制代码
docker logs <container>            # 查看日志
docker logs -f --tail 200 <container> # 实时跟踪,末尾200行

docker stats                       # 所有容器cpu/mem/io实时统计
docker stats <container-id>        # 指定容器

💾 数据卷 Volume

bash 复制代码
docker volume create my-volume
docker volume ls
docker volume inspect my-volume

docker volume rm my-volume
docker volume prune        # 删除未被使用的卷

# 两种挂载
# -v 命名卷:docker托管数据卷
# -v /opt/data:/app 绑定挂载:直接映射宿主机目录

🌐 网络 Network

bash 复制代码
docker network ls
docker network create my-bridge
docker network inspect my-bridge

docker network connect my-bridge <container>
docker network disconnect my-bridge <container>

docker network prune   # 清理未使用网络

🧹 系统清理(高频)

bash 复制代码
docker system prune                 # 删除停止容器、无用镜像、无用网络,不删volume
docker system prune -a              # 清理所有未使用镜像
docker system prune -a --volumes    # ⚠️危险:清理未使用镜像+容器+网络+卷

🧩 Docker‑Compose

bash 复制代码
docker-compose up               # 前台启动
docker-compose up -d            # 后台启动
docker-compose up -d --build    # 重新build镜像并启动

docker-compose stop             # 停止,保留容器
docker-compose restart          # 重启
docker-compose down             # 停止并删除容器网络
docker-compose down -v          # down同时删除数据卷

docker-compose logs -f          # 实时查看日志
docker-compose ps               # 查看compose服务状态

开发常用 compose 模板示例 docker-compose.yml

yaml 复制代码
version: "3.8"

services:
  nginx:
    image: nginx:1.27
    container_name: demo-nginx
    restart: always
    ports:
      - "8080:80"
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d
      - ./nginx/html:/usr/share/nginx/html
    environment:
      - TZ=Asia/Shanghai

  redis:
    image: redis:7-alpine
    container_name: demo-redis
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    command: redis-server --appendonly yes

volumes:
  redis-data:

使用模板

bash 复制代码
# 在yml所在目录执行
docker-compose up -d
相关推荐
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
分布式存储与RustFS1 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
玉&心1 天前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing1 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
赵文宇(温玉)1 天前
CoreDNS的hosts插件的缺行配置导致k8s集群异常
容器·kubernetes·rancher
guo_wen_qiang1 天前
上传本地镜像到harbor中
docker·容器·持续部署
羑悻的小杀马特1 天前
Dockerfile 全景指南:从入门编写dockerfile到生产环境镜像优化实战,一步步带你从啃透制作指令到镜像制作落地!
docker·dockerfile·镜像制作
BianHuanShiZhe1 天前
docker常见命令
docker
正经教主1 天前
【FDE系列】阶段2:Day 43:Docker Compose — 多容器一键编排
人工智能·docker·fde
忆~遂愿1 天前
本地片库怎么在外面打开?Plex + cpolar 搭一套可远程访问的私人影音库
docker·容器