欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker常用命令

程序员文章站 2024-03-13 22:51:46
...

       在本机安装完Docker后,该如何使用常用的操作,这和我们操作git命令是差不多的;因为本来的Docker的部分概念还是和git是差不多的。下面说一下在使用Docker时候,怎么来使用命令操作Docker。

     常用命令

      可以通过 docker --help 查看有哪些相关的命令;就如同我们在linux中使用的man命令;

fengweideMacBook-Pro:Desktop jerry.feng$ docker --help

Usage:	docker [OPTIONS] COMMAND   //语法

A self-sufficient runtime for containers

Options:  //options选项
      --config string      Location of client config files (default "/Users/jerry.feng/.docker")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/Users/jerry.feng/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/Users/jerry.feng/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/Users/jerry.feng/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands: //docker管理命令
  checkpoint  Manage checkpoints
  config      Manage Docker configs
  container   Manage containers
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:    //当前 shell 下 attach 连接指定运行镜像
  attach      Attach local standard input, output, and error streams to a running container   //容器命令
  build       Build an image from a Dockerfile  //通过Dockerfile定制镜像
  commit      Create a new image from a container's changes //提交当前容器为新的镜像
  cp          Copy files/folders between a container and the local filesystem //从容器中拷贝指定文件或者目录到宿主机中
  create      Create a new container //创建一个新的容器,同 run,但不启动容器
  deploy      Deploy a new stack or update an existing stack //部署一个新的堆栈
  diff        Inspect changes to files or directories on a container's filesystem //查看 docker 容器变化
  events      Get real time events from the server //从 docker 服务获取容器实时事件
  exec        Run a command in a running container //在已存在的容器上运行命令
  export      Export a container's filesystem as a tar archive //导出容器的内容流作为一个 tar 归档文件[对应 import ]
  history     Show the history of an image //展示一个镜像形成历史
  images      List images //列出系统当前镜像
  import      Import the contents from a tarball to create a filesystem image //从tar包中的内容创建一个新的文件系统映像[对应 export]
  info        Display system-wide information //显示当前系统容器信息
  inspect     Return low-level information on Docker objects //查看容器详细信息
  kill        Kill one or more running containers //kill 指定 docker 容器
  load        Load an image from a tar archive or STDIN //从一个 tar 包中加载一个镜像[对应 save]
  login       Log in to a Docker registry // 注册或者登陆一个 docker 源服务器
  logout      Log out from a Docker registry // 从当前 Docker registry 退出
  logs        Fetch the logs of a container //  输出当前容器日志信息
  pause       Pause all processes within one or more containers //暂停一个或多个容器内的所有进程
  port        List port mappings or a specific mapping for the container //查看映射端口对应的容器内部源端口
  ps          List containers //容器列表
  pull        Pull an image or a repository from a registry //从docker镜像源服务器拉取指定镜像或者库镜像
  push        Push an image or a repository to a registry //推送指定镜像或者库镜像至docker源服务器
  rename      Rename a container //给容器重命名
  restart     Restart one or more containers //重启容器
  rm          Remove one or more containers //删除一个或者多个容器
  rmi         Remove one or more images //删除一个或多个镜像
  run         Run a command in a new container //创建一个新的容器并运行一个命令
  save        Save one or more images to a tar archive (streamed to STDOUT by default) //保存一个镜像为一个 tar 包[对应 load]
  search      Search the Docker Hub for images //在 docker hub 中搜索镜像
  start       Start one or more stopped containers //启动容器
  stats       Display a live stream of container(s) resource usage statistics //动态显示容器的资源消耗情况
  stop        Stop one or more running containers //停止容器
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE //给源中镜像打标签
  top         Display the running processes of a container //查看容器中运行的进程信息
  unpause     Unpause all processes within one or more containers //取消暂停容器
  update      Update configuration of one or more containers //更新一个或多个容器配置信息
  version     Show the Docker version information //Docker版本信息
  wait        Block until one or more containers stop, then print their exit codes//截取容器停止时的退出状态值

 我们需要知道哪个命令怎么使用就可以通过 docker [command] --help查看,下面通过两个例子来介绍怎么查看命令的使用和如何使用docker的命令;

例如:search 搜索镜像命令

fengweideMacBook-Pro:Desktop jerry.feng$ docker search --help

Usage:	docker search [OPTIONS] TERM  //语法

Search the Docker Hub for images  //从Docker Hub搜索(相当于我们gitHub代码托管平台)

Options:
  -f, --filter filter   Filter output based on conditions provided//根据条件过滤输出
      --format string   Pretty-print search using a Go template // 使用模板搜索输出
      --limit int       Max number of search results (default 25)//搜索结果分页,默认25条
      --no-trunc        Don't truncate output //别节选,显示完整镜像信息

例如我们搜索mysql 只显示5条数据

fengweideMacBook-Pro:Desktop jerry.feng$ docker search --limit 5 mysql
NAME                         DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                        MySQL is a widely used, open-source relation…   6682                [OK]                
mysql/mysql-server           Optimized MySQL Server Docker images. Create…   487                                     [OK]
zabbix/zabbix-server-mysql   Zabbix Server with MySQL database support       109                                     [OK]
mysql/mysql-cluster          Experimental MySQL Cluster Docker images. Cr…   32                                      
circleci/mysql               MySQL is a widely used, open-source relation…   6                                       
fengweideMacBook-Pro:Desktop jerry.feng$ 

例如我们想展示描述信息更详细的查询

fengweideMacBook-Pro:Desktop jerry.feng$ docker search --no-trunc mysql
NAME                                                   DESCRIPTION                                                                                           STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relational database management system (RDBMS).                    6682                [OK]                
mariadb                                                MariaDB is a community-developed fork of MySQL intended to remain free under the GNU GPL.             2119                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Created, maintained and supported by the MySQL team at Oracle   487                                     [OK]
percona                                                Percona Server is a fork of the MySQL relational database management system created by Percona.       355                 [OK]                
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support                                                             109                              

如果我们使用images 查看本机的镜像文件,我们就可以使用 docker images --help 查看如何使用

fengweideMacBook-Pro:Desktop jerry.feng$ docker images --help

Usage:	docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)//列出本地所有镜像含中间镜像层
      --digests         Show digests //显示摘要信息
  -f, --filter filter   Filter output based on conditions provided //根据条件过滤输出
      --format string   Pretty-print images using a Go template//使用模板搜索输出
      --no-trunc        Don't truncate output //不节选输出
  -q, --quiet           Only show numeric IDs //只显示镜像id

 例:查看本机所有镜像

fengweideMacBook-Pro:Desktop jerry.feng$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
oracle/database     12.2.0.1-ee         b18b630873d3        5 days ago          7.19GB
<none>              <none>              b4c5ccdcc54e        5 days ago          7.19GB
<none>              <none>              6e3c71ddb344        5 days ago          3.57GB
<none>              <none>              f6ff001e7b8a        5 days ago          3.57GB
<none>              <none>              cfd17bc2cfe6        5 days ago          117MB
<none>              <none>              71ddcae6a6df        5 days ago          117MB
<none>              <none>              ff4f2c513ddd        5 days ago          117MB
oraclelinux         7-slim              80f19a81a58d        11 days ago         117MB

后面的命令都是这个使用套路;使用到哪种功能,如果不知道如何使用,就通过help命令查看命令的使用,这里就不一一赘述。