50 个常用 Docker 命令

1. Docker 基础命令

  • 查看 Docker 版本
bash 复制代码
docker --version
  • 查看 Docker 运行状态
bash 复制代码
systemctl status docker
  • 查看 Docker 信息
bash 复制代码
docker info
  • 查看帮助信息
bash 复制代码
docker help

2. 镜像管理

  • 拉取镜像
bash 复制代码
docker pull <镜像名>
  • 查看本地镜像
bash 复制代码
docker images
  • 删除镜像
bash 复制代码
docker rmi <镜像ID>
  • 删除未使用的镜像
bash 复制代码
docker image prune
  • 标记镜像
bash 复制代码
docker tag <源镜像> <目标镜像>
  • 推送镜像到 Docker Hub
bash 复制代码
docker push <镜像名>:<标签>
  • 查看镜像的详细信息
bash 复制代码
docker inspect <镜像ID>

3. 容器管理

  • 运行容器
bash 复制代码
docker run <镜像名>
  • 运行容器并进入交互模式
bash 复制代码
docker run -it <镜像名> /bin/bash
  • 查看正在运行的容器
bash 复制代码
docker ps
  • 查看所有容器(包括停止的)
bash 复制代码
docker ps -a
  • 停止容器
bash 复制代码
docker stop <容器ID>
  • 启动已停止的容器
bash 复制代码
docker start <容器ID>
  • 重启容器
bash 复制代码
docker restart <容器ID>
  • 删除容器
bash 复制代码
docker rm <容器ID>
  • 删除所有停止的容器
bash 复制代码
docker container prune
  • 进入正在运行的容器
bash 复制代码
docker exec -it <容器ID> /bin/bash
  • 查看容器日志
bash 复制代码
docker logs <容器ID>
  • 查看容器的资源使用情况
bash 复制代码
docker stats
  • 查看容器的环境变量
bash 复制代码
docker exec <容器ID> printenv
  • 复制文件到容器
bash 复制代码
docker cp <本地路径> <容器ID>:<容器路径>
  • 从容器复制文件到本地
bash 复制代码
docker cp <容器ID>:<容器路径> <本地路径>

4. 网络管理

  • 查看网络
bash 复制代码
docker network ls
  • 创建网络
bash 复制代码
docker network create <网络名>
  • 连接容器到网络
bash 复制代码
docker network connect <网络名> <容器ID>
  • 断开容器与网络的连接
bash 复制代码
docker network disconnect <网络名> <容器ID>

5. 卷管理

  • 查看卷
bash 复制代码
docker volume ls
  • 创建卷
bash 复制代码
docker volume create <卷名>
  • 删除卷
bash 复制代码
docker volume rm <卷名>

6. Dockerfile 和镜像构建

  • 创建 Dockerfile
bash 复制代码
touch Dockerfile
  • 构建镜像
bash 复制代码
docker build -t <镜像名>:<标签> .
  • 导出容器为 tar 文件
bash 复制代码
docker export <容器ID> > <文件名>.tar
  • 导入 tar 文件为镜像
bash 复制代码
cat <文件名>.tar | docker import - <镜像名>

7. Docker Compose

  • 使用 Docker Compose 启动服务
bash 复制代码
docker-compose up
  • 使用 Docker Compose 停止服务
bash 复制代码
docker-compose down
  • 查看 Docker Compose 服务状态
bash 复制代码
docker-compose ps
  • 构建 Docker Compose 服务
bash 复制代码
docker-compose build

8. 系统与日志管理

  • 查看 Docker 守护进程日志
bash 复制代码
journalctl -u docker.service
  • 查看 Docker 事件
bash 复制代码
docker events

9. 资源限制

  • 启动守护进程模式的容器
bash 复制代码
docker run -d <镜像名>
  • 使用特定端口运行容器
bash 复制代码
docker run -p <主机端口>:<容器端口> <镜像名>
  • 限制容器的 CPU 使用
bash 复制代码
docker run --cpus=<数量> <镜像名>
  • 限制容器的内存使用
bash 复制代码
docker run -m <内存限制> <镜像名>

我是晓凡,再小的帆也能远航

希望内容对您有所帮助

我们下期再见 ヾ(•ω•`)o (●'◡'●)

相关推荐
tritone4 小时前
我在阿贝云免费服务器上搭建RustDesk自建服务器(Self-Hosting)的真实体验【推荐】
运维·服务器
2301_800050994 小时前
DNS 服务器
linux·运维·笔记
慌糖4 小时前
自动化接口框架搭建分享-pytest第二部分
运维·自动化·pytest
Lin_Aries_04214 小时前
容器化简单的 Java 应用程序
java·linux·运维·开发语言·docker·容器·rpc
岁岁种桃花儿4 小时前
详解 Kubernetes 命令:kubectl exec -it nginx -- bash 及实战场景
运维·nginx·kubernetes
小牛马爱写博客4 小时前
DNS 服务器与 DHCP 服务器详解及配置指南
linux·运维·服务器·dns·dhcp
维尔切4 小时前
HAProxy 负载均衡器
linux·运维·数据库·负载均衡
VueVirtuoso4 小时前
前后端部署 + Nginx 配置 + Cloudflare 全攻略(通俗易懂版)
运维·nginx
QQ12958455045 小时前
服务器跨域问题CORS的解决
运维·服务器
小白银子5 小时前
零基础从头教学Linux(Day 42)
linux·运维·服务器·网络·nginx