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. [[email protected] ~]$ 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
时间: 2024-10-08 12:12:36

docker基础及命令的相关文章

Docker实战(一):Docker基础命令

Docker基础命令 下面简单介绍一下Docker常用的一些基础命令 # 在ubuntu中安装docker $ sudo apt-get install docker.io # 查看docker的版本信息 $ docker version # 查看安装docker的信息 $ docker info # 查看本机Docker中存在哪些镜像 $ docker images # 检索image $ docker search ubuntu:14.04 # 在docker中获取ubuntu镜像 $ do

Docker基础命令使用

Docker基础命令 本文主要基于上一片篇docker安装,来演示命令使用方法,docker安装. 1.Docker Hub搜索镜像 docker search 关键字 选项:--automated=false:仅显示自动创建的镜像(通过Github或BitBucket仓库自动构建的镜像,直接从仓库复制一份,生成dockerfile并自动生成标记) --no-trunc=false:输出信息不截断显示 -s,--stars=x:指定仅显示评价位指定星级以上的镜像 1)搜索centos docke

1. Docker基础命令

本文简要介绍Docker的基础命令,目的在于快速入门Dokcer,Docker的完整命令可以参考Docker官方手册. Docker的子命令非常多,可以使用 docker [help|--help|-H|-h] 查看Docker完整子命令 $ docker Usage: docker COMMAND ... Commands: attach Attach local standard input, output, and error streams to a running container

docker基础命令,常用操作

docker基础命令 使用docker镜像 获取镜像 从docker registry获取镜像的命令是docker pull.命令格式是: docker pull [选项][docker registry地址] 仓库名:标签 docker register地址:地址的格式一般是 域名:端口,默认地址是docker hub 仓库名:仓库名是两段格式,用户名/软件名,如果不写用户,默认docker hub用户名是library,也就是官方镜像 列出服务器所有的镜像文件 # 列出所有的imaga文件

Docker基础命令详解——镜像及容器操作

Docker基础命令详解--镜像及容器操作 前言 ? 上篇文章介绍了有关Docker的基础与Linux下docker的安装,本文主要讲解安装docker后的基础使用方法以及命令的介绍,主要是docker镜像操作及容器操作命令. ? 当然,docker的相关命令非常多,可以使用docker help命令查看对应目录以及相关提示命令. Docker镜像操作命令 [[email protected] ~]# which docker /usr/bin/docker 1.镜像搜索:docker sear

Docker 基础命令 简要入门

所有docker都可以通过man查看帮助:man docker [ps|top|run|...] 1.列出正在运行的镜像docker ps不给参数,返回正在运行的容器-a 列出所有容器-l 列出最新创建的容器 2.启动容器docker run ID [命令] [参数]-i #为容器始终打开标准输入-t #为创建的容器提供一个伪tty终端 3.查看已建立的容器的详细信息docker inspect [ID|容器名] 4.自定义容器名docker run --name=自定义容器名 ID [命令]

docker基础命令详解

Commands: attach    Attach to a running container build     Build an image from a Dockerfile commit    Create a new image from a container's changes cp        Copy files/folders from a container's filesystem to the host path create    Create a new co

docker讲解和命令汇总

docker 什么是docker? Docker是Docker.inc公司开源的一个基于LXC技术之上构建的Container容器引擎,源代码托管在GitHub上,基于Go语言并遵从Apache2.0协议开源(可以商业). Docker项目的目标是实现轻量级的操作系统虚拟化解决方案. Docker是通过内核虚拟化技术(namespaces及cgroups等)来提供容器的资源隔离与安全保障等.由于Docker通过操作系统层的虚拟化实现隔离,所以Docker容器在运行时,不需要类似虚拟机VM额外的操

Docker 学习笔记【2】 Docker 基础操作实,Docker仓库、数据卷,网络基础学习

Docker 学习笔记[3] Docker 仓库实操,创建私有仓库,实操数据卷.数据卷容器,实操 网络基础  ---------高级网络配置和部分实战案例学习 ============================================================= Docker 学习笔记[1]Docker 相关概念,基本操作--------实操记录开始 ============================================================= 被