一种简单的方式掌握命令行

docker是典型的C/S架构,客户端使用命令行和Rest接口来操作服务端。
命令行格式为:
docker [options] command [arg...]
docker 可选的操作 命令 可选的参数

  • 查看docker所有命令,docker --help
  • 查看具体的某个命令,docker command --help

示例:

  1. ## 查看帮助命令
  2. docker --help
  3. ## 命令行格式
  4. Usage: docker [OPTIONS] COMMAND [arg...]
  5. docker [ --help | -v | --version ]
  6. A self-sufficient runtime for containers.
  7. # 操作
  8. Options:
  9. --config=~/.docker 客户端配置文件 Location of client config files
  10. -D, --debug 启用debug模式 Enable debug mode
  11. -H, --host=[] 服务端域名 Daemon socket(s) to connect to
  12. -h, --help 使用帮助 Print usage
  13. -l, --log-level=info 设置日志级别 Set the logging level
  14. --tls 启用Transport Layer Security传输层安全性协议 Use TLS; implied by --tlsverify
  15. --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA
  16. --tlscert=~/.docker/cert.pem Path to TLS certificate file
  17. --tlskey=~/.docker/key.pem Path to TLS key file
  18. --tlsverify Use TLS and verify the remote
  19. -v, --version 打印版本信息 Print version information and quit
  20. Commands:
  21. attach 进入一个运行中的容器 Attach to a running container
  22. build Dockerfile构建一个镜像 Build an image from a Dockerfile
  23. commit 由容器的修改创建一个新的镜像,就是把当前所有操作保存为一个镜像 Create a new image from a container's changes
  24. cp 在容器和本地文件系统之间拷贝文件/目录 Copy files/folders between a container and the local filesystem
  25. create 创建一个新的容器 Create a new container
  26. diff 检查容器文件系统上的更改 Inspect changes on a container's filesystem
  27. events server获取实时的事件 Get real time events from the server
  28. exec 在一个运行中的容器上运行一条命令 Run a command in a running container
  29. export 将容器的文件系统导出为tar存档 Export a container's filesystem as a tar archive
  30. history 展示镜像的历史 Show the history of an image
  31. images 列出镜像 List images
  32. import 从tarball导入内容以创建文件系统映像 Import the contents from a tarball to create a filesystem image
  33. info 显示系统范围的信息 Display system-wide information
  34. inspect 返回容器、图像或任务的低级信息 Return low-level information on a container, image or task
  35. kill 杀死一个或多个运行中的容器 Kill one or more running containers
  36. load 从tar存档或STDIN加载镜像 Load an image from a tar archive or STDIN
  37. login 登陆docker镜像仓库 Log in to a Docker registry.
  38. logout 登出docker镜像仓库 Log out from a Docker registry.
  39. logs 拉取容器的日志 Fetch the logs of a container
  40. network 管理docker网络 Manage Docker networks
  41. node 管理Docker Swarm节点 Manage Docker Swarm nodes
  42. pause 暂停一个或多个容器中的所有进程 Pause all processes within one or more containers
  43. port 列出容器的端口映射或特定映射 List port mappings or a specific mapping for the container
  44. ps 列出容器 List containers
  45. pull 拉取一个镜像或仓库从注册仓库 Pull an image or a repository from a registry
  46. push 推送一个镜像或仓库到注册仓库 Push an image or a repository to a registry
  47. rename 重命名一个容器 Rename a container
  48. restart 重启一个容器 Restart a container
  49. rm 删除一个或多个容器 Remove one or more containers
  50. rmi 删除一个或多个镜像 Remove one or more images
  51. run 在一个新的容器一条运行命令 Run a command in a new container
  52. save 保存一个或多个镜像作为一个tar类型的归档文件 Save one or more images to a tar archive (streamed to STDOUT by default)
  53. search 从镜像仓库搜索一个镜像 Search the Docker Hub for images
  54. service 管理docker服务端 Manage Docker services
  55. start 启动一个或多个停止的容器 Start one or more stopped containers
  56. stats 显示容器资源使用统计信息的实时流 Display a live stream of container(s) resource usage statistics
  57. stop 停止一个或多个运行中的容器 Stop one or more running containers
  58. swarm 管理docker swarm Manage Docker Swarm
  59. tag 给一个仓库中的镜像打标签 Tag an image into a repository
  60. top 显示容器中运行的进程 Display the running processes of a container
  61. unpause 取消暂停一个或多个容器中的所有进程 Unpause all processes within one or more containers
  62. update 给一个或多个容器 升级配置Update configuration of one or more containers
  63. version 展示docker的版本信息 Show the Docker version information
  64. volume 管理docker volumes Manage Docker volumes
  65. wait 阻止,直到容器停止,然后打印其退出代码 Block until a container stops, then print its exit code
  66. ## 查看具体某个命令的帮助文档
  67. Run 'docker COMMAND --help' for more information on a command.
  68. # 示例
  69. docker run --help
  70. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  71. Run a command in a new container
  72. Options:
  73. --add-host value Add a custom host-to-IP mapping (host:ip) (default [])
  74. -a, --attach value Attach to STDIN, STDOUT or STDERR (default [])
  75. --blkio-weight value Block IO (relative weight), between 10 and 1000
  76. --blkio-weight-device value Block IO weight (relative device weight) (default [])
  77. --cap-add value Add Linux capabilities (default [])
  78. --cap-drop value Drop Linux capabilities (default [])
  79. --cgroup-parent string Optional parent cgroup for the container
  80. --cidfile string Write the container ID to the file
  81. --cpu-percent int CPU percent (Windows only)
  82. --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
  83. --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
  84. -c, --cpu-shares int CPU shares (relative weight)
  85. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
  86. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
  87. -d, --detach Run container in background and print container ID
  88. --detach-keys string Override the key sequence for detaching a container
  89. --device value Add a host device to the container (default [])
  90. --device-read-bps value Limit read rate (bytes per second) from a device (default [])
  91. --device-read-iops value Limit read rate (IO per second) from a device (default [])
  92. --device-write-bps value Limit write rate (bytes per second) to a device (default [])
  93. --device-write-iops value Limit write rate (IO per second) to a device (default [])
  94. --disable-content-trust Skip image verification (default true)
  95. --dns value Set custom DNS servers (default [])
  96. --dns-opt value Set DNS options (default [])
  97. --dns-search value Set custom DNS search domains (default [])
  98. --entrypoint string Overwrite the default ENTRYPOINT of the image
  99. -e, --env value Set environment variables (default [])
  100. --env-file value Read in a file of environment variables (default [])
  101. --expose value Expose a port or a range of ports (default [])
  102. --group-add value Add additional groups to join (default [])
  103. --health-cmd string Command to run to check health
  104. --health-interval duration Time between running the check
  105. --health-retries int Consecutive failures needed to report unhealthy
  106. --health-timeout duration Maximum time to allow one check to run
  107. --help Print usage
  108. -h, --hostname string Container host name
  109. -i, --interactive Keep STDIN open even if not attached
  110. --io-maxbandwidth string Maximum IO bandwidth limit for the system drive (Windows only)
  111. --io-maxiops uint Maximum IOps limit for the system drive (Windows only)
  112. --ip string Container IPv4 address (e.g. 172.30.100.104)
  113. --ip6 string Container IPv6 address (e.g. 2001:db8::33)
  114. --ipc string IPC namespace to use
  115. --isolation string Container isolation technology
  116. --kernel-memory string Kernel memory limit
  117. -l, --label value Set meta data on a container (default [])
  118. --label-file value Read in a line delimited file of labels (default [])
  119. --link value Add link to another container (default [])
  120. --link-local-ip value Container IPv4/IPv6 link-local addresses (default [])
  121. --log-driver string Logging driver for the container
  122. --log-opt value Log driver options (default [])
  123. --mac-address string Container MAC address (e.g. 92:d0:c6:0a:29:33)
  124. -m, --memory string Memory limit
  125. --memory-reservation string Memory soft limit
  126. --memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
  127. --memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
  128. --name string Assign a name to the container
  129. --network string Connect a container to a network (default "default")
  130. --network-alias value Add network-scoped alias for the container (default [])
  131. --no-healthcheck Disable any container-specified HEALTHCHECK
  132. --oom-kill-disable Disable OOM Killer
  133. --oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
  134. --pid string PID namespace to use
  135. --pids-limit int Tune container pids limit (set -1 for unlimited)
  136. --privileged Give extended privileges to this container
  137. -p, --publish value Publish a container's port(s) to the host (default [])
  138. -P, --publish-all Publish all exposed ports to random ports
  139. --read-only Mount the container's root filesystem as read only
  140. --restart string Restart policy to apply when a container exits (default "no")
  141. --rm Automatically remove the container when it exits
  142. --runtime string Runtime to use for this container
  143. --security-opt value Security Options (default [])
  144. --shm-size string Size of /dev/shm, default value is 64MB
  145. --sig-proxy Proxy received signals to the process (default true)
  146. --stop-signal string Signal to stop a container, SIGTERM by default (default "SIGTERM")
  147. --storage-opt value Storage driver options for the container (default [])
  148. --sysctl value Sysctl options (default map[])
  149. --tmpfs value Mount a tmpfs directory (default [])
  150. -t, --tty Allocate a pseudo-TTY
  151. --ulimit value Ulimit options (default [])
  152. -u, --user string Username or UID (format: <name|uid>[:<group|gid>])
  153. --userns string User namespace to use
  154. --uts string UTS namespace to use
  155. -v, --volume value Bind mount a volume (default [])
  156. --volume-driver string Optional volume driver for the container
  157. --volumes-from value Mount volumes from the specified container(s) (default [])
  158. -w, --workdir string Working directory inside the container

你应该对以上命令有个概念和熟悉过程,然后掌握命令行的规则
docker [options] command [arg…]

  • 查看帮助文档 docker —help
  • 查看具体某个命令的帮助文档 docker command —help

这些才是最重要的!

命令行学习忠告

  1. 用大于学,多尝试
  2. 循序渐进,不要想着一次性都背过会用,学会基础的,再慢慢深入,不断复盘

Docker三个最重要的概念,掌握相关的命令行即可满足日常需要。

  • 镜像
  • 容器
  • 仓库
  • 另外增加网络连接

接下来讲这三个基础操作,算作命令行入门!