Skip to content

Docker 命令速查表 ​

镜像命令 ​

bash
# 查看本地镜像
docker images
docker images -a                    # 含中间层
docker images -q                    # 只显示 ID
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"

# 搜索镜像
docker search nginx
docker search --filter is-official=true nginx

# 拉取镜像
docker pull nginx                   # 最新版
docker pull nginx:1.25-alpine       # 指定版本
docker pull --platform linux/arm64 nginx

# 查看镜像信息
docker inspect nginx
docker history nginx
docker history --no-trunc nginx

# 标签管理
docker tag nginx:latest myregistry/nginx:1.25

# 删除镜像
docker rmi nginx
docker rmi -f nginx                 # 强制删除
docker image prune                  # 删除悬空镜像
docker image prune -a               # 删除未使用的镜像

# 导出/导入
docker save -o nginx.tar nginx:latest
docker load -i nginx.tar

# 推送镜像
docker push myregistry/myapp:1.0

容器命令 ​

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 127.0.0.1:80:80 nginx  # 绑定 IP
docker run -it ubuntu bash          # 交互式
docker run --rm alpine echo "hello" # 运行后删除
docker run -d --restart unless-stopped nginx  # 自动重启
docker run -d -e MY_VAR=hello nginx # 环境变量
docker run -d -v /host:/container nginx  # 挂载目录
docker run -d --network mynet nginx # 指定网络
docker run -d --memory 512m --cpus 1 nginx  # 资源限制
docker run --init nginx             # 使用 init

# 查看容器
docker ps                           # 运行中
docker ps -a                        # 所有
docker ps -q                        # 只显示 ID
docker ps --filter "status=exited"  # 过滤
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

# 启停管理
docker start mycontainer
docker stop mycontainer             # 优雅停止(SIGTERM)
docker kill mycontainer             # 强制停止(SIGKILL)
docker restart mycontainer
docker pause mycontainer
docker unpause mycontainer

# 进入容器
docker exec -it mycontainer bash
docker exec -it mycontainer sh
docker exec mycontainer ls /app
docker exec -u root mycontainer bash  # 指定用户

# 日志
docker logs mycontainer
docker logs -f mycontainer          # 实时跟踪
docker logs --tail 100 mycontainer  # 最后 100 行
docker logs -t mycontainer          # 带时间戳
docker logs --since 1h mycontainer  # 1 小时内

# 查看详情
docker inspect mycontainer
docker stats mycontainer            # 资源使用(实时)
docker stats --no-stream mycontainer # 单次快照
docker top mycontainer              # 进程列表
docker port mycontainer             # 端口映射

# 文件操作
docker cp mycontainer:/path/file ./local
docker cp ./local mycontainer:/path/

# 删除容器
docker rm mycontainer
docker rm -f mycontainer            # 强制删除运行中的
docker container prune              # 清理已停止的

网络命令 ​

bash
docker network ls
docker network create mynet
docker network create --driver bridge --subnet 172.20.0.0/16 mynet
docker network inspect mynet
docker network connect mynet mycontainer
docker network disconnect mynet mycontainer
docker network rm mynet
docker network prune

数据卷命令 ​

bash
docker volume ls
docker volume create myvolume
docker volume inspect myvolume
docker volume rm myvolume
docker volume prune

Docker Compose 命令 ​

bash
docker compose up                   # 前台启动
docker compose up -d                # 后台启动
docker compose up -d --build        # 重新构建后启动
docker compose up -d --scale api=3  # 扩展实例
docker compose down                 # 停止并删除容器/网络
docker compose down -v              # 同上 + 删除卷
docker compose ps                   # 查看状态
docker compose logs -f              # 查看日志
docker compose logs -f api          # 查看指定服务日志
docker compose exec api sh          # 进入容器
docker compose run --rm api npm test # 运行一次性命令
docker compose build                # 构建镜像
docker compose build --no-cache     # 无缓存构建
docker compose pull                 # 拉取最新镜像
docker compose restart api          # 重启服务
docker compose stop                 # 停止服务
docker compose start                # 启动已停止的服务
docker compose config               # 验证配置

系统管理命令 ​

bash
docker info                         # 系统信息
docker version                      # 版本信息
docker system df                    # 磁盘使用
docker system df -v                 # 详细磁盘使用
docker system prune                 # 清理未使用资源
docker system prune -a              # 清理所有未使用资源
docker system prune -a -f           # 强制清理
docker events                       # 监听系统事件
docker events --filter type=container

# 登录仓库
docker login
docker login registry.example.com
docker logout

Docker Swarm 命令 ​

bash
# 集群管理
docker swarm init --advertise-addr <IP>
docker swarm join --token <TOKEN> <MANAGER-IP>:2377
docker swarm join-token worker
docker swarm join-token manager
docker swarm leave
docker swarm leave --force

# 节点管理
docker node ls
docker node inspect <NODE>
docker node update --availability drain <NODE>
docker node update --availability active <NODE>
docker node promote <NODE>
docker node demote <NODE>
docker node rm <NODE>

# 服务管理
docker service ls
docker service create --name web --replicas 3 -p 80:80 nginx
docker service ps web
docker service inspect web
docker service logs -f web
docker service update --image nginx:1.25 web
docker service scale web=5
docker service rollback web
docker service rm web

# Stack 管理
docker stack deploy -c docker-stack.yml myapp
docker stack ls
docker stack services myapp
docker stack ps myapp
docker stack rm myapp

# Secrets
docker secret create mysecret ./secret.txt
docker secret ls
docker secret inspect mysecret
docker secret rm mysecret

# Configs
docker config create myconfig ./config.yml
docker config ls
docker config rm myconfig

实用一行命令 ​

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

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

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

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

# 进入最后创建的容器
docker exec -it $(docker ps -lq) bash

# 查看容器 IP
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mycontainer

# 查看所有容器的 IP
docker inspect -f '{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

# 实时监控所有容器
watch -n 1 "docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'"