前言

上一章节,简单介绍了在CentOS下的Docker的安装过程,以及运行了一个官方提供的Hello,World镜像运行了第一个Docker。就像上一章中,验证Docker是否安装成功,我们执行的是docker info命令。运行镜像时,执行的是docker run imagesName。所以学习一个工具,主要还是学习如何利用本身工具提供的一些命令进行相应的操作。所以本章节,主要来介绍下Docker的常用命令。

Docker命令清单

docker提供了查看其所有支持的命令清单,只需运行

  1. docker

  1. docker help

从提示中可以看出,Docker命令是很多的,可以管理Docker,有操作镜像容器等等。对于常用的可能就是操作镜像容器了。所以这里主要列举下对于镜像容器常用的一些命令操作,同时也会列举下一些其他常用的命令。对于某个命令想知道其详细的参数选项时,可依照此模式进行查看。

  1. docker COMMAND --help

如,查看run的详细信息

  1. docker run --help

列举的run的详细选项及其用法说明(真的很多呀!)

  1. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  2. Run a command in a new container
  3. Options:
  4. --add-host list Add a custom host-to-IP mapping (host:ip)
  5. -a, --attach list Attach to STDIN, STDOUT or STDERR
  6. --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
  7. --blkio-weight-device list Block IO weight (relative device weight) (default [])
  8. --cap-add list Add Linux capabilities
  9. --cap-drop list Drop Linux capabilities
  10. --cgroup-parent string Optional parent cgroup for the container
  11. --cidfile string Write the container ID to the file
  12. --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
  13. --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
  14. --cpu-rt-period int Limit CPU real-time period in microseconds
  15. --cpu-rt-runtime int Limit CPU real-time runtime in microseconds
  16. -c, --cpu-shares int CPU shares (relative weight)
  17. --cpus decimal Number of CPUs
  18. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
  19. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
  20. -d, --detach Run container in background and print container ID
  21. --detach-keys string Override the key sequence for detaching a container
  22. --device list Add a host device to the container
  23. --device-cgroup-rule list Add a rule to the cgroup allowed devices list
  24. --device-read-bps list Limit read rate (bytes per second) from a device (default [])
  25. --device-read-iops list Limit read rate (IO per second) from a device (default [])
  26. --device-write-bps list Limit write rate (bytes per second) to a device (default [])
  27. --device-write-iops list Limit write rate (IO per second) to a device (default [])
  28. --disable-content-trust Skip image verification (default true)
  29. --dns list Set custom DNS servers
  30. --dns-option list Set DNS options
  31. --dns-search list Set custom DNS search domains
  32. --entrypoint string Overwrite the default ENTRYPOINT of the image
  33. -e, --env list Set environment variables
  34. --env-file list Read in a file of environment variables
  35. --expose list Expose a port or a range of ports
  36. --group-add list Add additional groups to join
  37. --health-cmd string Command to run to check health
  38. --health-interval duration Time between running the check (ms|s|m|h) (default 0s)
  39. --health-retries int Consecutive failures needed to report unhealthy
  40. --health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
  41. --health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
  42. --help Print usage
  43. -h, --hostname string Container host name
  44. --init Run an init inside the container that forwards signals and reaps processes
  45. -i, --interactive Keep STDIN open even if not attached
  46. --ip string IPv4 address (e.g., 172.30.100.104)
  47. --ip6 string IPv6 address (e.g., 2001:db8::33)
  48. --ipc string IPC mode to use
  49. --isolation string Container isolation technology
  50. --kernel-memory bytes Kernel memory limit
  51. -l, --label list Set meta data on a container
  52. --label-file list Read in a line delimited file of labels
  53. --link list Add link to another container
  54. --link-local-ip list Container IPv4/IPv6 link-local addresses
  55. --log-driver string Logging driver for the container
  56. --log-opt list Log driver options
  57. --mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
  58. -m, --memory bytes Memory limit
  59. --memory-reservation bytes Memory soft limit
  60. --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
  61. --memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
  62. --mount mount Attach a filesystem mount to the container
  63. --name string Assign a name to the container
  64. --network string Connect a container to a network (default "default")
  65. --network-alias list Add network-scoped alias for the container
  66. --no-healthcheck Disable any container-specified HEALTHCHECK
  67. --oom-kill-disable Disable OOM Killer
  68. --oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
  69. --pid string PID namespace to use
  70. --pids-limit int Tune container pids limit (set -1 for unlimited)
  71. --privileged Give extended privileges to this container
  72. -p, --publish list Publish a container's port(s) to the host
  73. -P, --publish-all Publish all exposed ports to random ports
  74. --read-only Mount the container's root filesystem as read only
  75. --restart string Restart policy to apply when a container exits (default "no")
  76. --rm Automatically remove the container when it exits
  77. --runtime string Runtime to use for this container
  78. --security-opt list Security Options
  79. --shm-size bytes Size of /dev/shm
  80. --sig-proxy Proxy received signals to the process (default true)
  81. --stop-signal string Signal to stop a container (default "SIGTERM")
  82. --stop-timeout int Timeout (in seconds) to stop a container
  83. --storage-opt list Storage driver options for the container
  84. --sysctl map Sysctl options (default map[])
  85. --tmpfs list Mount a tmpfs directory
  86. -t, --tty Allocate a pseudo-TTY
  87. --ulimit ulimit Ulimit options (default [])
  88. -u, --user string Username or UID (format: <name|uid>[:<group|gid>])
  89. --userns string User namespace to use
  90. --uts string UTS namespace to use
  91. -v, --volume list Bind mount a volume
  92. --volume-driver string Optional volume driver for the container
  93. --volumes-from list Mount volumes from the specified container(s)
  94. -w, --workdir string Working directory inside the container

镜像常用命令

  • 搜索镜像,利用search命令。
  1. docker search jdk
  • 拉取镜像,利用pull命令。
  1. 用法:docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  2. docker pull jboss/base-jdk:8

  • 查看已下载镜像列表,利用images命令
  1. docker images jboss/base-jdk:8

可查看所有已下载的镜像:

  1. REPOSITORY TAG IMAGE ID CREATED SIZE
  2. hello-world latest 2cb0d9787c4d 2 weeks ago 1.85kB
  • 镜像拷贝,同时重命名,利用tag命令
  1. # 比如,想创建拷贝一个镜像`hello-wrold`,同时命名为`lqdev.cn/hello-world:1`
  2. docker tag hello-world lqdev.cn/hello-world:1

此时查看镜像列表,就会发现多了一个镜像了:

  1. [root@xx ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest 2cb0d9787c4d 2 weeks ago 1.85kB
  4. lqdev.cn/hello-world 1 2cb0d9787c4d 2 weeks ago 1.85kB
  • 基于Dockerfile创建一个新的镜像,利用build命令(对于Dockerfile,在下一章节会详细进行说明的,这里就不过多介绍了。)
  1. # 使用当前目录下的Dockerfile,同时镜像命名(`-t`,指tag)为:lqdev.cn/first:1
  2. docker build -t lqdev.cn/first:1
  • 删除镜像,利用rmi命令(这里需要注意,当镜像有容器在使用时,是无法删除的,需要先删除容器再来删除镜像。)
  1. docker rmi jboss/base-jdk:8
  2. 或者根据images_id删除
  3. docker rmi b123d943e165

容器常用命令

  • 运行容器,利用run命令。
  1. docker run hello-world

运行命令是最常用的命令了,这里其常用选项进行列举说明下

  1. -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
  2. -d: 后台运行容器,并返回容器ID
  3. -i: 以交互模式运行容器,通常与 -t 同时使用;
  4. -p: 端口映射,格式为:主机(宿主)端口:容器端口
  5. -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
  6. --name="nginx-lb": 为容器指定一个名称;
  7. --dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
  8. --dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
  9. -h "mars": 指定容器的hostname
  10. -e username="ritchie": 设置环境变量;
  11. --env-file=[]: 从指定文件读入环境变量;
  12. --cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
  13. -m :设置容器使用内存最大值;
  14. --net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
  15. --link=[]: 添加链接到另一个容器;
  16. --expose=[]: 开放一个端口或一组端口;

比如,我们后台运行redis实例,同时指定其宿主端口为16379

  1. docker run -p 16379:6379 -d redis:3.2
  • 容器列表,使用ps命令,可以列举出当前运行的容器,需要所有容器时,加入-a选项即可。
  1. docker ps -a

此时,可看见所有的容器信息:

  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 3ba5b7475423 lqdev.cn/hello-world:1 "/hello" 7 seconds ago Exited (0) 6 seconds ago distracted_goldwasser
  3. 02f1c3cc2a31 hello-world "/hello" 20 hours ago Exited (0) 20 hours ago vibrant_ritchie
  • 停止容器,利用stop命令。
  1. # docker stop 容器id
  2. docker stop 3ba5b7475423
  • 启动已停止容器,利用start命令。
  1. # docker start 容器id
  2. docker start 3ba5b7475423
  • 重启容器,利用restart命令。
  1. # docker restart 容器id
  2. docker restart 3ba5b7475423
  • 强制停止容器,利用kill命令。
  1. # docker kill 容器id
  2. docker kill 3ba5b7475423
  • 删除容器,利用rm命令(只能删除已经停止的容器,若需要删除正在运行的容器,可加入-f参数选项)
  1. # docker rm 容器ID
  2. docker rm 3ba5b7475423
  • 进入容器,在一些场景下,比如想查看redis的客户端redis-cli时,这个时候就需要进入容器了。进入容器有很多中,这里就exec进行讲解下,其他的比如attach不熟悉,大家可自行搜索下。
  1. # docker exec -it 容器ID 参数
  2. docker exec -it 3ba5b7475423 redis-cli
  3. 参数说明:
  4. -d:分离模式: 在后台运行
  5. -i:即使没有附加也保持STDIN 打开
  6. -t:分配一个伪终端

此时就可以看见已经进入到客户端了,进行相应操作了。

  1. [root@xxx ~]# docker exec -it 3ba5b7475423 redis-cli
  2. 127.0.0.1:6379> keys *
  3. (empty list or set)
  4. 127.0.0.1:6379> set name okong
  5. OK
  6. 127.0.0.1:6379> keys *
  7. 1) "name"
  8. 127.0.0.1:6379> get name
  9. "okong"
  10. 127.0.0.1:6379>
  • 容器中创建一个镜像。在制作一些私有镜像时,常常是依赖于一个基础镜像后,然后进入容器中进行相关系统环境的配置,或者相应的优化。但若容器一删除,之前的修改都会没有了。故在这些场景下,可直接从修改后的容器中创建一个自己的私有镜像,这样里面的一些环境和相关优化项还是保留的。这个主要会在构建私有镜像章节时具体展开。
  1. # docker commit [options] 容器id name:tag
  2. docker commit 3ba5b7475423 lqdev.cn/redis:1
  3. 参数说明:
  4. -a:提交的镜像作者
  5. -c:使用Dockerfile指令来创建镜像
  6. -m:提交时的说明文字
  7. -p:在commit时,将容器暂停

其他常用命令

  • 查看日志,利用logs命令。
  1. # docker logs [OPTIONS] 容器ID
  2. docker logs -f 3ba5b7475423
  3. 参数说明:
  4. -f : 跟踪日志输出
  5. --since :显示某个开始时间的所有日志
  6. -t : 显示时间戳
  7. --tail :仅列出最新N条容器日志

  • 宿主和容器之间相互拷贝文件,利用cp命令。

简单来说就是:

  1. # docker cp 容器名:要拷贝的文件在容器里面的路径 要拷贝到宿主机的相应路径
  2. # docker cp 要拷贝的文件路径 容器名:要拷贝到容器里面对应的路径
  3. docker cp 3ba5b7475423:/opt/a.json /opt
  4. docker cp /opt/a.json 3ba5b7475423:/opt

总结

本章节主要是介绍了下Docker的一些常用命令的说明。文中未列举的命令,大家可直接使用命令docker command --help查看其命令说明或者自行谷歌下。熟悉了这些常用命令后,下一章节,主要会介绍下Dockerfile文件的语法及简单示例。

最后

若文中有错误或者遗漏之处,还望指出,共同进步!

参考资料

  1. https://docs.docker.com/engine/reference/commandline/docker/
  2. http://www.runoob.com/docker/docker-command-manual.html

老生常谈

  • 个人QQ:499452441
  • 微信公众号:lqdevOps

个人博客:http://blog.lqdev.cn

原文地址:http://blog.lqdev.cn/2018/07/31/docker/docker-three/

我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。

Docker | 第三章:Docker常用命令的更多相关文章

  1. Linux学习笔记 一 第三章 Linux常用命令

    第三章Linux常用命令 一.文件处理命令 1.命令格式 2.目录处理命令:ls 3.目录处理命令:mkdir 4.文件处理命令: touch

  2. 第三章 Docker 入门

    第三章 docker 入门 3.1 确保docker已经就绪 首先查看docker程序是否存在,功能是否正常 [#3#cloudsoar@cloudsoar-virtual-machine ~]$su ...

  3. docker系列四之docker镜像与容器的常用命令

    docker镜像与容器的常用命令 一.概述   docker的镜像于容器是docker中两个至关重要的概念,首先给各位读者解释一下笔者对于这两个概念的理解.镜像,我们从字面意思上看,镜子里成像,我们人 ...

  4. docker学习笔记二:常用命令

    docker学习笔记二:常用命令 查看docker常用命令 docker --help 返回结果如下: 其中常用的命令如下: 1.image相关操作 展示所有的image: 删除image: rmi ...

  5. Docker(三)Docker常用命令

    Docker常用命令 帮助命令 # 显示 Docker 版本信息 docker version # 显示系统信息,包括镜像和容器的数量 docker info # 查看帮助文档 帮助文档地址:http ...

  6. Docker容器(四)——常用命令

    (1).基本使用方法 查看所有镜像.docker images [root@youxi1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ...

  7. Docker系列教程05 容器常用命令

    https://mp.weixin.qq.com/s?__biz=MzI4ODQ3NjE2OA==&mid=2247483890&idx=1&sn=2721f08624e6de ...

  8. Java篇:Docker的介绍安装 和常用命令

    文章目录 为什么 出现docker Docker的简介 容器(Container) 镜像(Image) 仓库(Repository) Docker的安装 查看容器 删除镜像 删除容器 部署应用 以my ...

  9. Docker小白到实战之常用命令演示,通俗易懂

    前言 上一篇大概认识了Docker,主要是从概念.架构.优点及流程方面进行阐述,并进行安装和体验: 接下来就开始进行实操学习,在演示过程中会针对关键的知识点进行归纳和总结,这里先从常用命令说起,来吧, ...

随机推荐

  1. Python模块-shelve模块

    shelve模块也是用来序列化的,可以持久化任何pickle可支持的python数据格式,比pickle好用,也是python专属,可以dump多次数据,也可以直接修改数据 序列化 # -*- cod ...

  2. 转载:Android Studio调试功能使用总结

    这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码行,在行号的区域后面单击鼠标左键即可. 2.开启调试会话 ...

  3. 谷歌浏览器的input自动填充出现黄色背景解决方案(在已经输入内容之后)

    当你之前提交过表单,再次获取input焦点时,会有一个记录之前填写过的文本的下拉列表式的自动填充效果且带有黄色背景, 这个填充功能本身是没什么问题的,但是谷歌浏览器给了个莫名其妙的黄色背景,用css样 ...

  4. 在linux系统个人目录下安装新版python

    一.下载所需的python版本文件 在linux个人目录下 下载https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz 解压tar.xz ...

  5. Spring入门第二十二课

    重用切面表达式 我们有的时候在切面里面有多个函数,大部分函数的切入点都是一样的,所以我们可以声明切入点表达式,来重用. package logan.study.aop.impl; public int ...

  6. img src 直接显示图片字符串,微信例子

    <div class="weui-cell__hd"><img src="data:image/png;base64,iVBORw0KGgoAAAANS ...

  7. IOS 获取系统通讯录

    进入正题  获取系统通讯录,不想多讲,留下链接http://my.oschina.net/joanfen/blog/140146 通常做法: 首先创建一个ABAddressBookRef类的对象add ...

  8. 【Linux入门】

    文件系统结构:倒树状: 文件命名规则: Windows  8.3的命名规则:文件名8位以内,后缀名3位以内 linux中隐藏文件的方式:在文件名称前面加.  eg: 1.txt===> .1.t ...

  9. PureUI(扩展版本)

    喜欢一个UI(pure,官网)不怎么更新(可能官方认为不需要更新).我自己做了扩展和修正,整个库下载地址:http://files.cnblogs.com/files/RainbowInTheSky/ ...

  10. Baidu - Echarts 地图实例测试,并绘制平滑圆弧路径

    百度Echarts实例地址: http://echarts.baidu.com/examples.html 同事想做一个地图,地图上的几个点通过动态的线连接起来.但是在实例里没找到类似的. 然后仔细分 ...