docker常用命令

Docker 是一个广泛使用的容器化平台,下面是一些常用的 Docker 命令,按不同的功能分类。

1. 镜像相关命令

  • 搜索镜像

    bash 复制代码
    docker search <镜像名>

    例如:搜索 nginx 镜像。

    bash 复制代码
    docker search nginx
  • 拉取镜像

    bash 复制代码
    docker pull <镜像名>

    例如:拉取官方的 nginx 镜像。

    bash 复制代码
    docker pull nginx
  • 列出本地镜像

    bash 复制代码
    docker images
  • 删除镜像

    bash 复制代码
    docker rmi <镜像ID或镜像名>

    例如:删除指定镜像。

    bash 复制代码
    docker rmi nginx
  • 查看镜像详细信息

    bash 复制代码
    docker inspect <镜像ID或镜像名>
  • 创建镜像

    bash 复制代码
    docker build -t <镜像名:标签> <Dockerfile所在路径>

    例如:从当前目录的 Dockerfile 构建镜像。

    bash 复制代码
    docker build -t myimage:latest .

2. 容器相关命令

  • 运行容器

    bash 复制代码
    docker run -d --name <容器名> <镜像名>

    例如:后台运行 nginx 容器。

    bash 复制代码
    docker run -d --name mynginx nginx
  • 进入容器

    bash 复制代码
    docker exec -it <容器ID或容器名> /bin/bash

    例如:进入名为 mynginx 的容器。

    bash 复制代码
    docker exec -it mynginx /bin/bash
  • 停止容器

    bash 复制代码
    docker stop <容器ID或容器名>

    例如:停止容器 mynginx

    bash 复制代码
    docker stop mynginx
  • 启动容器

    bash 复制代码
    docker start <容器ID或容器名>
  • 重启容器

    bash 复制代码
    docker restart <容器ID或容器名>
  • 删除容器

    bash 复制代码
    docker rm <容器ID或容器名>

    例如:删除名为 mynginx 的容器。

    bash 复制代码
    docker rm mynginx
  • 查看正在运行的容器

    bash 复制代码
    docker ps
  • 查看所有容器(包括停止的)

    bash 复制代码
    docker ps -a
  • 查看容器日志

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

    例如:查看名为 mynginx 的容器日志。

    bash 复制代码
    docker logs mynginx

3. 网络相关命令

  • 列出所有网络

    bash 复制代码
    docker network ls
  • 创建网络

    bash 复制代码
    docker network create <网络名>

    例如:创建名为 mynetwork 的网络。

    bash 复制代码
    docker network create mynetwork
  • 连接容器到指定网络

    bash 复制代码
    docker network connect <网络名> <容器名或ID>
  • 断开容器与网络连接

    bash 复制代码
    docker network disconnect <网络名> <容器名或ID>

4. 容器与数据卷

  • 创建数据卷

    bash 复制代码
    docker volume create <数据卷名>
  • 挂载数据卷到容器

    bash 复制代码
    docker run -d -v <数据卷名>:<容器内路径> <镜像名>
  • 查看所有数据卷

    bash 复制代码
    docker volume ls
  • 删除数据卷

    bash 复制代码
    docker volume rm <数据卷名>

5. 其他常用命令

  • 查看容器资源使用情况(CPU、内存等)

    bash 复制代码
    docker stats
  • 导出容器为镜像

    bash 复制代码
    docker commit <容器ID或容器名> <新镜像名:标签>
  • 导出容器为 tar 文件

    bash 复制代码
    docker export <容器ID或容器名> > <文件名>.tar
  • 导入 tar 文件为镜像

    bash 复制代码
    docker import <文件名>.tar
  • 清理未使用的镜像、容器、数据卷、网络

    bash 复制代码
    docker system prune

这些命令是 Docker 的基本操作,涵盖了镜像管理、容器管理、网络管理以及存储管理,能够帮助你高效地使用 Docker。

相关推荐
阿拉斯加大闸蟹3 分钟前
基于RDMA 通信的可负载均衡高性能服务架构
运维·架构·负载均衡
不是吧这都有重名39 分钟前
为什么ubuntu大文件拷贝会先快后慢?
linux·运维·ubuntu
sunshine-sm1 小时前
CentOS Steam 9安装 Redis
linux·运维·服务器·redis·centos
Davie_Liu1 小时前
【Docker Buildx】docker buildx本地构建多架构镜像,拉取镜像时的网络延迟问题(已解决)
网络·docker·buildx
_Walli_1 小时前
k8s集群搭建(三)-------- Dashboard UI
云原生·容器·kubernetes
棒棒的唐2 小时前
armbian平台ubuntu环境下telnet安装及启动,给pantherX2增加一个应急通道
linux·运维·armbian·telnetd
纳切威2 小时前
CentOS 7部署Zabbix5.0
linux·运维·centos·zabbix
sunshine-sm2 小时前
CentOS Steam 9安装 MySQL 8
linux·运维·服务器·数据库·mysql·centos·centos stream
bug攻城狮2 小时前
CentOS 7 快速检查软件包是否已安装的5种方法
linux·运维·centos
男孩李2 小时前
浅谈代理流程自动化 (APA)
运维·人工智能·自动化