docker基础及命令

1.启动docker

  1. sudo systemctl start docker
  2. sudo systemctl restart docker
  3. sudo systemctl stop docker
  4. sudo systemctl status docker
2.查看docker信息
  1. sudo docker info
  1. Containers: 1
  2. Running: 0
  3. Paused: 0
  4. Stopped: 1
  5. Images: 2
  6. Server Version: 17.03.1-ce
  7. Storage Driver: devicemapper
  8. Pool Name: vgdocker-thinpool
  9. Pool Blocksize: 524.3 kB
  10. Base Device Size: 10.74 GB
  11. Backing Filesystem: xfs
  12. Data file:
  13. Metadata file:
  14. Data Space Used: 2.708 GB
  15. Data Space Total: 20.4 GB
  16. Data Space Available: 17.69 GB
  17. Metadata Space Used: 1.917 MB
  18. Metadata Space Total: 213.9 MB
  19. Metadata Space Available: 212 MB
  20. Thin Pool Minimum Free Space: 2.039 GB
  21. Udev Sync Supported: true
  22. Deferred Removal Enabled: true
  23. Deferred Deletion Enabled: true
  24. Deferred Deleted Device Count: 0
  25. Library Version: 1.02.135-RHEL7 (2016-11-16)
  26. Logging Driver: json-file
  27. Cgroup Driver: cgroupfs
  28. Plugins:
  29. Volume: local
  30. Network: bridge host macvlan null overlay
  31. Swarm: inactive
  32. Runtimes: runc
  33. Default Runtime: runc
  34. Init Binary: docker-init
  35. containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
  36. runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
  37. init version: 949e6fa
  38. Security Options:
  39. seccomp
  40. Profile: default
  41. Kernel Version: 3.10.0-123.el7.x86_64
  42. Operating System: CentOS Linux 7 (Core)
  43. OSType: linux
  44. Architecture: x86_64
  45. CPUs: 2
  46. Total Memory: 3.69 GiB
  47. Name: wgl
  48. ID: 2GXU:7CL7:ETAC:BTYX:M4V6:JSMV:3CIJ:4OZC:KKKX:D5IV:DVBX:T3KV
  49. Docker Root Dir: /var/lib/docker
  50. Debug Mode (client): false
  51. Debug Mode (server): false
  52. Registry: https://index.docker.io/v1/
  53. WARNING: bridge-nf-call-iptables is disabled
  54. WARNING: bridge-nf-call-ip6tables is disabled
  55. Experimental: false
  56. Insecure Registries:
  57. 127.0.0.0/8
  58. Live Restore Enabled: false
3.运行第一个容器
  1. sudo docker run --name wgl_centos -i -t centos /bin/bash
  2. sudo docker rename oldername newname
 
      -i  保证容器中stdin是开启的,尽管我们并没有附着到容器中,是交互式shell的半边天;-t  是另一半边天,他告诉docker为要创建的容器分配一个伪tty终端。如要在命令行下创建一个我们能与之经行交互的容器,而不是一个运行后台服务的容器,这两个是基本的了。

4.查看宿主机容器列表
  1. sudo docker ps -a #显示全部容器
  2. sudo docker ps -l #列出最后一个运行的容器,也可以--format进一步控制显示
  3. sudo docker ps -n x #显示最后x个容器。
5.启动,停止容器
  1. sudo docker start(restart,stop) 容器ID(容器name)
6,附着到容器上
  1. sudo docker attach 容器
例如
sudo docker start dreamy_knuth    先启动(启动后会沿用run命令时指定的参数运行)
sudo docker attach dreamy_knuth 后附着到容器,【及进入交互界面】
相当于
sudo docker start dreamy_knuth 
sudo docker exec -i -t dreamy_knuth  /bin.bash(可用于打开一个守护式容器)
 
7,创建守护式容器
  1. sudo docker run --name wgl_centos_daemon -d centos /bin/sh -c "while true;do echo hello world;sleep 3;done"
在后台运行,只返回容器ID。
-d 在后台运行
 
8.获取守护式容器的日志
  1. sudo docker logs 容器 #输出最后几条后返回
  2. sudo docker logs -f 容器 #监控日志
  3. sudo docker logs --tail 10 容器 #获取容器日志最后10条
  4. sudo docker logs --tail 0 -f 容器 #跟踪最新日志
  5. sudo docker logs -ft 容器 #监控日志+时间戳
 
9.dockers日志驱动
  1. sudo docker run --log-driver="syslog" --name wgl_centos_daemon -d centos /bin/sh -c "while true;do echo hello world;sleep 3;done"
--log-driver=
syslog 将容器日志重新定向到Syslog,禁用dockers logs命令
json-file 默认,用dockers logs命令查看
none 禁用所有容器中的日志,禁用dockers logs命令
 
10.查看容器进程
  1. sudo docker top wgl_centos_daemon
11.docker统计信息
  1. sudo docker stats [容器1 容器2 容器3 ...]
 
12.在容器内部运行进程
 
后台式任务与交互式任务
 
后台式任务
  1. sudo docker exec -d 53fc4852ce08 touch /opt/wgl.log # -u 为新启动的进程指定属主
通过docker exec 后台命令,可维护,监控,管理正在运行的容器
 
进入容器(不论是守护式还是交互式)的交互式界面
  1. sudo docker exec -it 53fc4852ce08 /bin/bash
 
13,自动重启容器
  1. sudo docker run --restart=always --name wgl_centos_daemon -d centos /bin/sh -c "while true;do echo hello world;sleep 3;done"
--restart会检查日期的退出代码
always        不认退出代码是什么,都重启
on-failure    退出代码非0才重启,还可以设置重启次数:--restart=on-failure:5
 
14.深入容器,容器详情
  1. sudo docker inspect 容器
 
  1. [dwsoft@wgl ~]$ sudo docker inspect wgl_centos_daemon
  2. [
  3. {
  4. "Id": "53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a",
  5. "Created": "2017-05-28T05:35:00.155823247Z",
  6. "Path": "/bin/sh",
  7. "Args": [
  8. "-c",
  9. "while true;do echo hello world;sleep 3;done"
  10. ],
  11. "State": {
  12. "Status": "running",
  13. "Running": true,
  14. "Paused": false,
  15. "Restarting": false,
  16. "OOMKilled": false,
  17. "Dead": false,
  18. "Pid": 6450,
  19. "ExitCode": 0,
  20. "Error": "",
  21. "StartedAt": "2017-05-28T05:35:01.363485741Z",
  22. "FinishedAt": "0001-01-01T00:00:00Z"
  23. },
  24. "Image": "sha256:8140d0c64310d4e290bf3938757837dbb8f806acba0cb3f6a852558074345348",
  25. "ResolvConfPath": "/var/lib/docker/containers/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a/resolv.conf",
  26. "HostnamePath": "/var/lib/docker/containers/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a/hostname",
  27. "HostsPath": "/var/lib/docker/containers/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a/hosts",
  28. "LogPath": "/var/lib/docker/containers/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a
  29. 4389c91508dc3fbd50a-json.log", "Name": "/wgl_centos_daemon",
  30. "RestartCount": 0,
  31. "Driver": "devicemapper",
  32. "MountLabel": "",
  33. "ProcessLabel": "",
  34. "AppArmorProfile": "",
  35. "ExecIDs": null,
  36. "HostConfig": {
  37. "Binds": null,
  38. "ContainerIDFile": "",
  39. "LogConfig": {
  40. "Type": "json-file",
  41. "Config": {}
  42. },
  43. "NetworkMode": "default",
  44. "PortBindings": {},
  45. "RestartPolicy": {
  46. "Name": "no",
  47. "MaximumRetryCount": 0
  48. },
  49. "AutoRemove": false,
  50. "VolumeDriver": "",
  51. "VolumesFrom": null,
  52. "CapAdd": null,
  53. "CapDrop": null,
  54. "Dns": [],
  55. "DnsOptions": [],
  56. "DnsSearch": [],
  57. "ExtraHosts": null,
  58. "GroupAdd": null,
  59. "IpcMode": "",
  60. "Cgroup": "",
  61. "Links": null,
  62. "OomScoreAdj": 0,
  63. "PidMode": "",
  64. "Privileged": false,
  65. "PublishAllPorts": false,
  66. "ReadonlyRootfs": false,
  67. "SecurityOpt": null,
  68. "UTSMode": "",
  69. "UsernsMode": "",
  70. "ShmSize": 67108864,
  71. "Runtime": "runc",
  72. "ConsoleSize": [
  73. 0,
  74. 0
  75. ],
  76. "Isolation": "",
  77. "CpuShares": 0,
  78. "Memory": 0,
  79. "NanoCpus": 0,
  80. "CgroupParent": "",
  81. "BlkioWeight": 0,
  82. "BlkioWeightDevice": null,
  83. "BlkioDeviceReadBps": null,
  84. "BlkioDeviceWriteBps": null,
  85. "BlkioDeviceReadIOps": null,
  86. "BlkioDeviceWriteIOps": null,
  87. "CpuPeriod": 0,
  88. "CpuQuota": 0,
  89. "CpuRealtimePeriod": 0,
  90. "CpuRealtimeRuntime": 0,
  91. "CpusetCpus": "",
  92. "CpusetMems": "",
  93. "Devices": [],
  94. "DiskQuota": 0,
  95. "KernelMemory": 0,
  96. "MemoryReservation": 0,
  97. "MemorySwap": 0,
  98. "MemorySwappiness": -1,
  99. "OomKillDisable": false,
  100. "PidsLimit": 0,
  101. "Ulimits": null,
  102. "CpuCount": 0,
  103. "CpuPercent": 0,
  104. "IOMaximumIOps": 0,
  105. "IOMaximumBandwidth": 0
  106. },
  107. "GraphDriver": {
  108. "Name": "devicemapper",
  109. "Data": {
  110. "DeviceId": "76",
  111. "DeviceName": "docker-253:1-277138-721bd5780ec0a2800c9db53413e77636e96e787a4b330b9d665c2cf289c01c43",
  112. "DeviceSize": "10737418240"
  113. }
  114. },
  115. "Mounts": [],
  116. "Config": {
  117. "Hostname": "53fc4852ce08",
  118. "Domainname": "",
  119. "User": "",
  120. "AttachStdin": false,
  121. "AttachStdout": false,
  122. "AttachStderr": false,
  123. "Tty": false,
  124. "OpenStdin": false,
  125. "StdinOnce": false,
  126. "Env": [
  127. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  128. ],
  129. "Cmd": [
  130. "/bin/sh",
  131. "-c",
  132. "while true;do echo hello world;sleep 3;done"
  133. ],
  134. "Image": "centos",
  135. "Volumes": null,
  136. "WorkingDir": "",
  137. "Entrypoint": null,
  138. "OnBuild": null,
  139. "Labels": {
  140. "build-date": "20170510",
  141. "license": "GPLv2",
  142. "name": "CentOS Base Image",
  143. "vendor": "CentOS"
  144. }
  145. },
  146. "NetworkSettings": {
  147. "Bridge": "",
  148. "SandboxID": "dc56c0446f06b8b53b914574c34ec35c09fa38bc9d43d70ba5750aef60915597",
  149. "HairpinMode": false,
  150. "LinkLocalIPv6Address": "",
  151. "LinkLocalIPv6PrefixLen": 0,
  152. "Ports": {},
  153. "SandboxKey": "/var/run/docker/netns/dc56c0446f06",
  154. "SecondaryIPAddresses": null,
  155. "SecondaryIPv6Addresses": null,
  156. "EndpointID": "0da7299f69c100fce911e1c6b12f333e56ff30e3ff7a2f799ee595af89265fc8",
  157. "Gateway": "172.17.0.1",
  158. "GlobalIPv6Address": "",
  159. "GlobalIPv6PrefixLen": 0,
  160. "IPAddress": "172.17.0.2",
  161. "IPPrefixLen": 16,
  162. "IPv6Gateway": "",
  163. "MacAddress": "02:42:ac:11:00:02",
  164. "Networks": {
  165. "bridge": {
  166. "IPAMConfig": null,
  167. "Links": null,
  168. "Aliases": null,
  169. "NetworkID": "23e919387bcf6493c7bdcd91e31b8995132fb7f081cfbac7778e855c6a4f6d58",
  170. "EndpointID": "0da7299f69c100fce911e1c6b12f333e56ff30e3ff7a2f799ee595af89265fc8",
  171. "Gateway": "172.17.0.1",
  172. "IPAddress": "172.17.0.2",
  173. "IPPrefixLen": 16,
  174. "IPv6Gateway": "",
  175. "GlobalIPv6Address": "",
  176. "GlobalIPv6PrefixLen": 0,
  177. "MacAddress": "02:42:ac:11:00:02"
  178. }
  179. }
  180. }
  181. }
  182. ]
 
也可用-f或--format,查看选定的结果,可查看多个容器,多个结果。
 
 
15,删除容器
  1. sudo docker rm 容器
  2. sudo docker rm -f 容器 #删除正在运行的容器
  3. sudo docker rm `sudo docker ps -a -q` # -q只返回容器ID
 

 
posted @ 2017-05-28 14:28  青夜yuong  阅读(1220)  评论(0编辑  收藏  举报