Master Docker: Essential Commands for Developers and Ops
This guide compiles the most commonly used Docker commands, organized by functionality—including installation, image management, container handling, network and volume operations, logging, debugging, and system cleanup—to help developers and operations engineers efficiently manage Docker environments.
Docker 常用命令大全
Docker is an open‑source container engine widely used for developing, deploying, and running distributed applications. Mastering common Docker commands is essential for developers and operations staff.
1. 安装与版本管理
1.1 检查 Docker 版本
docker --versionDisplays the installed Docker version.
1.2 检查 Docker 服务状态
docker infoShows detailed system information to help you understand Docker’s runtime status.
1.3 启动/停止 Docker 服务 (Linux)
# 启动 Docker 服务
sudo systemctl start docker
# 停止 Docker 服务
sudo systemctl stop docker
# 重启 Docker 服务
sudo systemctl restart docker2. 镜像管理
2.1 拉取镜像
docker pull <镜像名>:<标签>Example: pull the latest nginx image.
docker pull nginx:latest2.2 查看本地镜像
docker imagesLists all local Docker images.
2.3 搜索镜像
docker search <关键词>Example: search for a Redis image.
docker search redis2.4 删除镜像
docker rmi <镜像ID或名称>Deletes the specified image; if the image is used by a container, remove the container first.
2.5 构建镜像
docker build -t <镜像名>:<标签> .Builds an image using a Dockerfile.
2.6 导出和导入镜像
# 导出镜像
docker save -o <文件名>.tar <镜像名>
# 导入镜像
docker load -i <文件名>.tar3. 容器管理
3.1 启动容器
docker run <镜像名>Example: start a container from the nginx image.
docker run nginx3.2 启动并后台运行容器
docker run -d <镜像名>Example: run Redis in detached mode.
docker run -d redis3.3 查看正在运行的容器
docker psLists all currently running containers.
3.4 查看所有容器(包括已停止的)
docker ps -a3.5 删除容器
docker rm <容器ID或名称>Deletes a container; stop it first if it is running.
3.6 停止容器
docker stop <容器ID或名称>Stops a running container.
3.7 进入容器
docker exec -it <容器ID或名称> /bin/bashOpens an interactive shell inside the container.
3.8 查看容器日志
docker logs <容器ID或名称>Shows the container’s standard output logs.
3.9 容器导出和导入
# 导出容器为 tar 文件
docker export <容器ID> -o <文件名>.tar
# 从 tar 文件导入
docker import <文件名>.tar <镜像名>4. 网络管理
4.1 查看网络列表
docker network ls4.2 创建网络
docker network create <网络名>4.3 删除网络
docker network rm <网络名>4.4 将容器连接到网络
docker network connect <网络名> <容器名>4.5 将容器从网络断开
docker network disconnect <网络名> <容器名>4.6 查看网络详情
docker network inspect <网络名>5. 数据卷管理
5.1 查看数据卷
docker volume ls5.2 创建数据卷
docker volume create <数据卷名>5.3 删除数据卷
docker volume rm <数据卷名>5.4 查看数据卷的详细信息
docker volume inspect <数据卷名>5.5 挂载数据卷到容器
docker run -v <数据卷名>:/path/in/container <镜像名>6. 日志与调试
6.1 查看容器的日志
docker logs <容器ID或名称>6.2 实时查看容器日志
docker logs -f <容器ID或名称>6.3 查看容器的资源使用情况
docker stats <容器ID或名称>6.4 查看容器的详细信息
docker inspect <容器ID或名称>7. 系统管理与清理
7.1 查看 Docker 系统信息
docker info7.2 清理未使用的镜像、容器、数据卷和网络
docker system prune -fThe -f flag skips the confirmation prompt.
7.3 清理未使用的镜像
docker image prune -a -f7.4 清理未使用的容器
docker container prune -f7.5 清理未使用的数据卷
docker volume prune -f7.6 清理未使用的网络
docker network prune -f8. 常用组合命令
8.1 清理所有未使用的资源(镜像、容器、卷、网络)
docker system prune -af --volumes8.2 停止并删除所有容器
docker stop $(docker ps -q) && docker rm $(docker ps -a -q)8.3 删除所有未使用的镜像、网络和卷
docker image prune -a -f && docker volume prune -f && docker network prune -f结语
By mastering these common Docker commands, you can manage Docker environments more efficiently, streamline workflows, and improve development and operations productivity. As projects grow, proper management of containers and images becomes crucial. We hope this summary helps you enhance your Docker skills.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
