当前位置:   article > 正文

Docker 容器技术入门_oomkilldisable

oomkilldisable

目录

一、Dockers 名词术语

1. Docker 镜像

2. Docker 仓库

3. Docker 容器

二、Docker的部署

三、Docker 的数据存储

1. 数据卷

2. 容器卷

四、Docker 容器的管理

1. Docker 容器的创建和删除

2. 进入与退出容器:

3. docker 容器的运行

4. Docker 容器常用的管理命令

(1)docker start/stop/restart/kill

(2)docker run 

(3)docker rm

(4)docker create

(5)docker exec

(6)docker ps

 (7)docker inspect

(8)docker logs

 (9)docker port

(10)docker commit

 (11)docker cp

(12)docker login/logout

 (13)docker pull/push

 (14)docker images

(15)docker rmi

 (16)docker tag

(17)docker build

(18)docker history

(19)docker info

(20)docker version


一、Dockers 名词术语

1. Docker 镜像

Docker镜像是Docker容器运行时的只读模板,每一个镜像由一系列的层组成。当改变一个Docker镜像时,比如升级某个程序到其新版本,一个新的层就会被创建,而不用替换整个原先的镜像或者重新建立镜像。docker 的镜像概念类似虚拟机的镜像。是一个只读的模板,一个独立的文件系统,包括运行容器所需的数据,可以用来创建新的容器。

2. Docker 仓库

Docker仓库就是用来保存Docker镜像的,可以i理解为代码管理控制中的代码仓库。

3. Docker 容器

Docker容器和文件夹很相似,一个Docker容器包含了某个应用运行所需要的所有环境,每一个Docker容器都是从Docker镜像创建的。可以对Docker容器执行运行、开始、停止、移动和删除等操作。每一个Docker容器都是独立和安全的应用平台,Docker容器时Docker的运行部分。可以把容器看作一个简易版的linux环境(包含root用户权限,进程空间,用户空间和网络空间等)和运行在其中的应用程序。

 

二、Docker的部署

1. 检查系统的内核版本

  1. [root@zy-host ~]# uname -r
  2. 3.10.0-1127.19.1.el7.x86_64

2. 安装系统必要的软件包

[root@zy-host ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

 3. 安装 docker

[root@zy-host ~]# yum install docker -y

4. 启动 docker 服务

  1. [root@zy-host ~]# systemctl start docker
  2. [root@zy-host ~]# ps -ef|grep docker
  3. root 26566 1 0 21:19 ? 00:00:00 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json --selinux-enabled --log-driver=journald --signature-verification=false --storage-driver overlay2
  4. root 26571 26566 0 21:19 ? 00:00:00 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc --runtime-args --systemd-cgroup=true
  5. root 26653 26199 0 21:19 pts/1 00:00:00 grep --color=auto docker

5. 测试 docker 安装是否成功

从结果可以看出,能正常拉取镜像文件。docker 部署完成。

  1. [root@zy-host ~]# docker pull centos:latest
  2. Trying to pull repository docker.io/library/centos ...
  3. latest: Pulling from docker.io/library/centos
  4. a1d0c7532777: Pull complete
  5. Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
  6. Status: Downloaded newer image for docker.io/centos:latest

三、Docker 的数据存储

Docker 管理数据的方式有以下两种:

(1)数据卷的方式

(2)容器卷的方式

1. 数据卷

数据卷是一个或多个容器专门指定绕过Union File System 的目录,为持续性或共享数据提供一些有用的功能。

(1)数据卷可以在容器间共享和重用。

(2)数据卷的数据的改变是直接修改的。

(3)数据卷数据的改变不会被包括在容器中。

(4)数据卷是持续性的,直到没有容器使用它们。

容器使用数据卷的参数说明:

  1. -v /Dir_name #直接将数据目录挂载到容器 /Dir_name 目录
  2. -v src:dst #将物理机目录挂载到容器目录

 实例一:

  1. [root@zy-host ~]# docker run -it --name test_docker -v /data centos
  2. [root@408c6cfdf7a9 /]# ls -l /data
  3. total 0

新开一个shell窗口:

  1. [root@zy-host ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 408c6cfdf7a9 centos "/bin/bash" About a minute ago Up About a minute test_docker
  4. [root@zy-host ~]# docker inspect 408c6cfdf7a9
  5. [
  6. {
  7. "Id": "408c6cfdf7a90c4c92df52a00c259f492defa9f4a450ff3a0e0d167c1dcc0f7f",
  8. "Created": "2023-01-01T13:34:19.116466096Z",
  9. "Path": "/bin/bash",
  10. "Args": [],
  11. "State": {
  12. "Status": "running",
  13. "Running": true,
  14. "Paused": false,
  15. "Restarting": false,
  16. "OOMKilled": false,
  17. "Dead": false,
  18. "Pid": 26723,
  19. "ExitCode": 0,
  20. "Error": "",
  21. "StartedAt": "2023-01-01T13:34:19.371748554Z",
  22. "FinishedAt": "0001-01-01T00:00:00Z"
  23. },
  24. "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
  25. "ResolvConfPath": "/var/lib/docker/containers/408c6cfdf7a90c4c92df52a00c259f492defa9f4a450ff3a0e0d167c1dcc0f7f/resolv.conf",
  26. "HostnamePath": "/var/lib/docker/containers/408c6cfdf7a90c4c92df52a00c259f492defa9f4a450ff3a0e0d167c1dcc0f7f/hostname",
  27. "HostsPath": "/var/lib/docker/containers/408c6cfdf7a90c4c92df52a00c259f492defa9f4a450ff3a0e0d167c1dcc0f7f/hosts",
  28. "LogPath": "",
  29. "Name": "/test_docker",
  30. "RestartCount": 0,
  31. "Driver": "overlay2",
  32. "MountLabel": "",
  33. "ProcessLabel": "",
  34. "AppArmorProfile": "",
  35. "ExecIDs": null,
  36. "HostConfig": {
  37. "Binds": null,
  38. "ContainerIDFile": "",
  39. "LogConfig": {
  40. "Type": "journald",
  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": "docker-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": "overlay2",
  109. "Data": {
  110. "LowerDir": "/var/lib/docker/overlay2/b41a537e114f796d554c8de1be1eb6c5f7a407125efcf21224f3894129b0296e-init/diff:/var/lib/docker/overlay2/1160e211438f4e10a35f852fde8757300b931ea570b6ccbe612312f2723cd285/diff",
  111. "MergedDir": "/var/lib/docker/overlay2/b41a537e114f796d554c8de1be1eb6c5f7a407125efcf21224f3894129b0296e/merged",
  112. "UpperDir": "/var/lib/docker/overlay2/b41a537e114f796d554c8de1be1eb6c5f7a407125efcf21224f3894129b0296e/diff",
  113. "WorkDir": "/var/lib/docker/overlay2/b41a537e114f796d554c8de1be1eb6c5f7a407125efcf21224f3894129b0296e/work"
  114. }
  115. },
  116. "Mounts": [
  117. {
  118. "Type": "volume",
  119. "Name": "83024fdc26b3ef564d00d24f4618f70ede09ec01fb796066653f87f25e53067f",
  120. "Source": "/var/lib/docker/volumes/83024fdc26b3ef564d00d24f4618f70ede09ec01fb796066653f87f25e53067f/_data", #挂载的源目录路径
  121. "Destination": "/data", #将存储目录直接挂载到窗口 /data目录
  122. "Driver": "local",
  123. "Mode": "",
  124. "RW": true,
  125. "Propagation": ""
  126. }
  127. ],
  128. "Config": {
  129. "Hostname": "408c6cfdf7a9",
  130. "Domainname": "",
  131. "User": "",
  132. "AttachStdin": true,
  133. "AttachStdout": true,
  134. "AttachStderr": true,
  135. "Tty": true,
  136. "OpenStdin": true,
  137. "StdinOnce": true,
  138. "Env": [
  139. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  140. ],
  141. "Cmd": [
  142. "/bin/bash"
  143. ],
  144. "Image": "centos",
  145. "Volumes": {
  146. "/data": {}
  147. },
  148. "WorkingDir": "",
  149. "Entrypoint": null,
  150. "OnBuild": null,
  151. "Labels": {
  152. "org.label-schema.build-date": "20210915",
  153. "org.label-schema.license": "GPLv2",
  154. "org.label-schema.name": "CentOS Base Image",
  155. "org.label-schema.schema-version": "1.0",
  156. "org.label-schema.vendor": "CentOS"
  157. }
  158. },
  159. "NetworkSettings": {
  160. "Bridge": "",
  161. "SandboxID": "8053f79bd06f429cfc4728d5c49944292fbd2071c06ab650f69b54dcf22da3ba",
  162. "HairpinMode": false,
  163. "LinkLocalIPv6Address": "",
  164. "LinkLocalIPv6PrefixLen": 0,
  165. "Ports": {},
  166. "SandboxKey": "/var/run/docker/netns/8053f79bd06f",
  167. "SecondaryIPAddresses": null,
  168. "SecondaryIPv6Addresses": null,
  169. "EndpointID": "5721b2a49b96006bf1df575d9afbb3a2d6871c38ae460f782d940ec4fbf9d117",
  170. "Gateway": "172.17.0.1",
  171. "GlobalIPv6Address": "",
  172. "GlobalIPv6PrefixLen": 0,
  173. "IPAddress": "172.17.0.2",
  174. "IPPrefixLen": 16,
  175. "IPv6Gateway": "",
  176. "MacAddress": "02:42:ac:11:00:02",
  177. "Networks": {
  178. "bridge": {
  179. "IPAMConfig": null,
  180. "Links": null,
  181. "Aliases": null,
  182. "NetworkID": "4a7e8cd0fdde1b0b9e50ffef6d865d5e80f320c88b34c1f5d288bb04126a7f5e",
  183. "EndpointID": "5721b2a49b96006bf1df575d9afbb3a2d6871c38ae460f782d940ec4fbf9d117",
  184. "Gateway": "172.17.0.1",
  185. "IPAddress": "172.17.0.2",
  186. "IPPrefixLen": 16,
  187. "IPv6Gateway": "",
  188. "GlobalIPv6Address": "",
  189. "GlobalIPv6PrefixLen": 0,
  190. "MacAddress": "02:42:ac:11:00:02"
  191. }
  192. }
  193. }
  194. }
  195. ]
  1. [root@zy-host ~]# cd /var/lib/docker/volumes/83024fdc26b3ef564d00d24f4618f70ede09ec01fb796066653f87f25e53067f/_data
  2. [root@zy-host _data]# ls

从结果发现,挂载的源目录下的确没有任何数据

下面在源目录下创建数据,然后去容器的目录查看是否有数据产生,操作如下:

  1. [root@zy-host _data]# pwd
  2. /var/lib/docker/volumes/83024fdc26b3ef564d00d24f4618f70ede09ec01fb796066653f87f25e53067f/_data
  3. [root@zy-host _data]# mkdir test
  4. [root@zy-host _data]# ll
  5. total 4
  6. drwxr-xr-x 2 root root 4096 Jan 1 21:53 test
  1. [root@408c6cfdf7a9 /]# pwd
  2. /
  3. [root@408c6cfdf7a9 /]# hostname
  4. 408c6cfdf7a9
  5. [root@408c6cfdf7a9 /]# ls -l /data
  6. total 4
  7. drwxr-xr-x 2 root root 4096 Jan 1 13:53 test

容器挂载的目录已有数据产生。

实例二:

前者是物理机目录,后者是容器目录。

  1. [root@zy-host _data]# docker run -it -v /data1:/mnt centos
  2. [root@44d62f93e82e /]# cd /mnt
  3. [root@44d62f93e82e mnt]# ls -l
  4. total 0

在物理机目录 data1 创建文件 

  1. [root@zy-host ~]# cd /data1/
  2. [root@zy-host data1]# echo "hello">test.txt
  3. [root@zy-host data1]# ll
  4. total 4
  5. -rw-r--r-- 1 root root 6 Jan 1 21:58 test.txt
  6. [root@zy-host data1]# cat test.txt
  7. hello

检查容器目录是否有文件和内容:

  1. [root@44d62f93e82e mnt]# ls -l
  2. total 4
  3. -rw-r--r-- 1 root root 6 Jan 1 13:58 test.txt
  4. [root@44d62f93e82e mnt]# cat test.txt
  5. hello
  6. [root@44d62f93e82e mnt]# pwd
  7. /mnt
  8. [root@44d62f93e82e mnt]# hostname
  9. 44d62f93e82e

2. 容器卷

容器卷指定的是直接挂载其他容器的数据目录,使用的参数如下:

--volumes-from  #使用其他容器的目录

实例:

  1. [root@zy-host data1]# docker run -d --name mydocker -v /data centos
  2. 161e866bbe4c1e10df63c28e00f81995be0784a477315b8f9073af7f580e0dec
  3. [root@zy-host data1]# docker run -it --name mynfs --volumes-from mydocker centos
  4. [root@dd8f9e68dc7e /]# ls -l /data
  5. total 0

此时进入mydocker 容器/data 目录写入数据进行测试:

  1. [root@zy-host ~]# cd /var/lib/docker/volumes/5c4e8fca1cac444def95cdc129e1826e14e73d1b4ecb315541386782fc34e307/_data/
  2. [root@zy-host _data]# ls
  3. [root@zy-host _data]# echo "welcome to here">file
  4. [root@zy-host _data]# ll
  5. total 4
  6. -rw-r--r-- 1 root root 16 Jan 1 22:11 file

再查看刚才容器中是否有数据:

  1. [root@dd8f9e68dc7e /]# cd /data/
  2. [root@dd8f9e68dc7e data]# ls -l
  3. total 4
  4. -rw-r--r-- 1 root root 16 Jan 1 14:11 file
  5. [root@dd8f9e68dc7e data]# cat file
  6. welcome to here

四、Docker 容器的管理

1. Docker 容器的创建和删除

创建容器方法一:

没有指定容器的名称,自动命名,状态是自动退出:

  1. [root@zy-host ~]# docker run centos /bin/echo "nihao" #创建容器
  2. nihao
  3. [root@zy-host ~]# docker ps -a #查看所有容器
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. de3d25c3fd65 centos "/bin/echo nihao" 7 seconds ago Exited (0) 6 seconds ago boring_austin
  6. dd8f9e68dc7e centos "/bin/bash" 10 minutes ago Up 10 minutes mynfs
  7. 161e866bbe4c centos "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago mydocker
  8. 44d62f93e82e centos "/bin/bash" 22 minutes ago Up 21 minutes stoic_swanson
  9. 408c6cfdf7a9 centos "/bin/bash" 44 minutes ago Up 44 minutes test_docker

创建容器方法二:

创建一个自定义名称的容器。

  1. [root@zy-host ~]# docker run --name zy -t -i centos /bin/bash
  2. [root@d77e6cd81fcf /]# ps -ef
  3. UID PID PPID C STIME TTY TIME CMD
  4. root 1 0 0 14:22 ? 00:00:00 /bin/bash
  5. root 14 1 0 14:22 ? 00:00:00 ps -ef
  6. [root@zy-host ~]# docker ps -a
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. d77e6cd81fcf centos "/bin/bash" About a minute ago Exited (0) 50 seconds ago zy
  9. de3d25c3fd65 centos "/bin/echo nihao" 5 minutes ago Exited (0) 5 minutes ago boring_austin
  10. dd8f9e68dc7e centos "/bin/bash" 15 minutes ago Up 15 minutes mynfs
  11. 161e866bbe4c centos "/bin/bash" 15 minutes ago Exited (0) 15 minutes ago mydocker
  12. 44d62f93e82e centos "/bin/bash" 27 minutes ago Up 27 minutes stoic_swanson
  13. 408c6cfdf7a9 centos "/bin/bash" 49 minutes ago Up 49 minutes test_docker

删除容器:

  1. [root@zy-host ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. d77e6cd81fcf centos "/bin/bash" About a minute ago Exited (0) 50 seconds ago zy
  4. de3d25c3fd65 centos "/bin/echo nihao" 5 minutes ago Exited (0) 5 minutes ago boring_austin
  5. dd8f9e68dc7e centos "/bin/bash" 15 minutes ago Up 15 minutes mynfs
  6. 161e866bbe4c centos "/bin/bash" 15 minutes ago Exited (0) 15 minutes ago mydocker
  7. 44d62f93e82e centos "/bin/bash" 27 minutes ago Up 27 minutes stoic_swanson
  8. 408c6cfdf7a9 centos "/bin/bash" 49 minutes ago Up 49 minutes test_docker
  9. [root@zy-host ~]# docker rm de3d25c3fd65 #删除一个停止的容器
  10. de3d25c3fd65
  11. [root@zy-host ~]# docker rm -f dd8f9e68dc7e #删除一个正在运行的容器
  12. dd8f9e68dc7e
  13. [root@zy-host ~]# docker ps -a
  14. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  15. d77e6cd81fcf centos "/bin/bash" 5 minutes ago Exited (0) 4 minutes ago zy
  16. 161e866bbe4c centos "/bin/bash" 19 minutes ago Exited (0) 19 minutes ago mydocker
  17. 44d62f93e82e centos "/bin/bash" 31 minutes ago Up 31 minutes stoic_swanson
  18. 408c6cfdf7a9 centos "/bin/bash" 53 minutes ago Up 53 minutes test_docker

2. 进入与退出容器:

启动和进入:

  1. [root@zy-host ~]# docker start 408c6cfdf7a9
  2. 408c6cfdf7a9
  3. [root@zy-host ~]# docker attach 408c6cfdf7a9
  4. [root@408c6cfdf7a9 /]#

退出:exit

  1. [root@408c6cfdf7a9 /]# exit
  2. exit

3. docker 容器的运行

创建docker容器之后,需要将容器运行起来,运行一个容器的操作命令如下:

-d  表示启动到后台运行

-P 随机端口映射

-p 指定端口映射

  1. [root@zy-host ~]# docker run -d -P nginx
  2. 8fc624241bdce84094a0d6ce1ff4952ba1d1a112e591e5215fbfc8307f07c720
  3. [root@zy-host ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 8fc624241bdc nginx "/docker-entrypoin..." About a minute ago Up About a minute 0.0.0.0:32769->80/tcp infallible_austin
  6. 44d62f93e82e centos "/bin/bash" 45 minutes ago Up 45 minutes stoic_swanson

实例操作:

  1. [root@zy-host ~]# docker run -d -p 81:80 nginx
  2. cba9790a8dd91c73ed5f5eba40f33d432fa3ae1e8402cc519231de8846f6da13
  3. [root@zy-host ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. cba9790a8dd9 nginx "/docker-entrypoin..." 4 seconds ago Up 3 seconds 0.0.0.0:81->80/tcp vibrant_wiles
  6. 8fc624241bdc nginx "/docker-entrypoin..." 3 minutes ago Up 3 minutes 0.0.0.0:32769->80/tcp infallible_austin
  7. 44d62f93e82e centos "/bin/bash" 48 minutes ago Up 48 minutes stoic_swanson

4. Docker 容器常用的管理命令

输入docker命令查看帮助信息:

  1. [root@zy-host ~]# docker
  2. Usage: docker COMMAND
  3. A self-sufficient runtime for containers
  4. Options:
  5. --config string Location of client config files (default "/root/.docker")
  6. -D, --debug Enable debug mode
  7. --help Print usage
  8. -H, --host list Daemon socket(s) to connect to (default [])
  9. -l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
  10. --tls Use TLS; implied by --tlsverify
  11. --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
  12. --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
  13. --tlskey string Path to TLS key file (default "/root/.docker/key.pem")
  14. --tlsverify Use TLS and verify the remote
  15. -v, --version Print version information and quit
  16. Management Commands:
  17. container Manage containers
  18. image Manage images
  19. network Manage networks
  20. node Manage Swarm nodes
  21. plugin Manage plugins
  22. secret Manage Docker secrets
  23. service Manage services
  24. stack Manage Docker stacks
  25. swarm Manage Swarm
  26. system Manage Docker
  27. volume Manage volumes
  28. Commands:
  29. attach Attach to a running container
  30. build Build an image from a Dockerfile
  31. commit Create a new image from a container's changes
  32. cp Copy files/folders between a container and the local filesystem
  33. create Create a new container
  34. diff Inspect changes on a container's filesystem
  35. events Get real time events from the server
  36. exec Run a command in a running container
  37. export Export a container's filesystem as a tar archive
  38. history Show the history of an image
  39. images List images
  40. import Import the contents from a tarball to create a filesystem image
  41. info Display system-wide information
  42. inspect Return low-level information on Docker objects
  43. kill Kill one or more running containers
  44. load Load an image from a tar archive or STDIN
  45. login Log in to a Docker registry
  46. logout Log out from a Docker registry
  47. logs Fetch the logs of a container
  48. pause Pause all processes within one or more containers
  49. port List port mappings or a specific mapping for the container
  50. ps List containers
  51. pull Pull an image or a repository from a registry
  52. push Push an image or a repository to a registry
  53. rename Rename a container
  54. restart Restart one or more containers
  55. rm Remove one or more containers
  56. rmi Remove one or more images
  57. run Run a command in a new container
  58. save Save one or more images to a tar archive (streamed to STDOUT by default)
  59. search Search the Docker Hub for images
  60. start Start one or more stopped containers
  61. stats Display a live stream of container(s) resource usage statistics
  62. stop Stop one or more running containers
  63. tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  64. top Display the running processes of a container
  65. unpause Unpause all processes within one or more containers
  66. update Update configuration of one or more containers
  67. version Show the Docker version information
  68. wait Block until one or more containers stop, then print their exit codes

(1)docker start/stop/restart/kill

启动/停止/重启/杀掉 容器 

-s  表示向容器发送信号

  1. [root@zy-host ~]# docker start zy
  2. zy
  3. [root@zy-host ~]# docker stop zy
  4. zy
  5. [root@zy-host ~]# docker restart zy
  6. zy
  7. [root@zy-host ~]# docker kill -s kill zy
  8. zy

(2)docker run 

功能:创建并启动一个新的容器

常用参数如下:

-d:后台运行容器,并返回容器ID

-i:以交互模式运行容器,常与参数 -t 同时使用

-t:给容器重新分配一个伪终端

--name:给容器指定一个名称

-m:指定容器使用内存的最大值

-net:指定容器使用的网络类型

--link:链接到另一容器

实例操作:

后台启动并运行一个名为nginx的容器,运行前回自动去Docker镜像站点下载最新的镜像文件。

  1. [root@zy-host ~]# docker run -d --name nginx nginx:latest
  2. d52cc861584327762513d105bb6b6e5e15a401d900186645582a3596f0ce9842

 以交互模式运行容器,然后在容器内执行 /bin/bash 命令

  1. [root@zy-host ~]# docker run -it nginx:latest /bin/bash
  2. root@2277469b159c:/#

(3)docker rm

功能:删除容器

常用参数:

-f:强制删除一个运行中的容器

-l:删除指定的链接

-v:删除与容器关联的卷

  1. [root@zy-host ~]# docker rm -f upbeat_wing
  2. upbeat_wing
  3. #删除容器,并删除容器挂载的数据卷
  4. [root@zy-host ~]# docker rm -v mydocker
  5. mydocker

(4)docker create

功能:创建一个新的容器,但不启动它。

  1. [root@zy-host ~]# docker create --name myserver nginx:latest
  2. 25db53db75e170211e9384e09a166a0283254f93ea845e21f47e0921e7cb07a1

(5)docker exec

功能:在运行的容器中执行命令

常用参数:

-d:在后台运行

-i:保持STDIN打开

-t:分配一个伪终端

  1. [root@d77e6cd81fcf scripts]# pwd
  2. /server/scripts
  3. [root@d77e6cd81fcf scripts]# ls -l
  4. total 4
  5. -rwxr-xr-x 1 root root 20 Jan 2 02:10 docker.sh
  6. [root@d77e6cd81fcf scripts]# cat docker.sh
  7. echo "hello world!"
  8. [root@zy-host ~]# docker exec -it zy /bin/sh /server/scripts/docker.sh
  9. hello world!

(6)docker ps

功能:列出容器(正在运行)

常用参数:

-a:列出所有容器,包括停止的

-f:根据条件过滤显示内容

-l:列出最近创建的容器

-n:列出最近创建的指定个数的容器

-q:只显示容器ID

-s:显示总文件大小

  1. [root@zy-host ~]# docker ps -n 2
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 25db53db75e1 nginx:latest "/docker-entrypoin..." 22 minutes ago Created myserver
  4. d52cc8615843 nginx:latest "/docker-entrypoin..." 11 hours ago Up 11 hours 80/tcp nginx

 (7)docker inspect

功能:获取容器的元素据

常用参数:

-f:指定返回值格式或模板文件

-s:显示总文件大小

--type:为指定类型返回JSON

(8)docker logs

功能:获取容器的日志

常用参数:

-f:跟踪日志输出

-t:显示时间戳

--tail:只显示最新n条容器日志

--since:显示某个开始时间的所有日志

[root@zy-host ~]# docker logs -f zy

 (9)docker port

功能:显示指定容器的端口映射信息

  1. [root@zy-host ~]# docker port vibrant_wiles
  2. 80/tcp -> 0.0.0.0:81

(10)docker commit

功能:用已存在的容器重新创建一个新的镜像

常用参数:

-a:提交的镜像作者

-c:使用Dockerfile指令来创建镜像

-m:提交时附上说明文字

-p:在提交时,将容器暂停

  1. #将容器d77e6cd81fcf重新生成一个名为newdocker_images的新镜像
  2. [root@zy-host ~]# docker commit -a "zhangyin" -m "add a new images" d77e6cd81fcf newdocker_images:v1.0.0
  3. sha256:5b67fc4b93344797195259733bf25dce31adfaa1f47dfecfbc2100030bf56a22

 (11)docker cp

功能:用于容器与物理主机之间复制文件

拷贝物理机文件到容器的 /web目录

[root@zy-host data1]# docker cp /data1/test.txt d77e6cd81fcf:/web/

进入容器查看是否复制过去:

  1. [root@d77e6cd81fcf /]# cat /web/test.txt
  2. hello

复制过去并改名:

[root@zy-host data1]# docker cp /data1/test.txt d77e6cd81fcf:/web/test1.txt

复制容器目录到物理主机:

[root@zy-host data1]# docker cp d77e6cd81fcf:/web /data/

(12)docker login/logout

功能:用于登录与登出容器镜像仓库

  • docker login:登录到一个Docker 镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker hub。
  • docker logout:登出一个Docker镜像仓库,如果未指定镜像仓库地址,则默认为官方仓库。

常用参数:

-u:登录的用户名

-p:登录的密码 

[root@zy-host ~]# docker login -u username -p password

 (13)docker pull/push

docker pull:从镜像仓库中拉取或更新指定镜像

docker push:将本地的镜像上传到镜像仓库(要先登录到镜像仓库)

  1. [root@zy-host ~]# docker pull nginx
  2. Using default tag: latest
  3. Trying to pull repository docker.io/library/nginx ...
  4. latest: Pulling from docker.io/library/nginx
  5. Digest: sha256:0047b729188a15da49380d9506d65959cce6d40291ccfb4e039f5dc7efd33286
  6. Status: Image is up to date for docker.io/nginx:latest

 (14)docker images

功能:显示系统本地容器镜像文件

常用参数:

-a:列出所有的镜像(含中间映像层,默认是过滤掉中间映像层)

--digests:显示镜像的摘要信息

-f:显示满足条件的镜像

--format:指定返回值的模板文件

--no-trunc:显示完整的镜像信息

-q:只显示镜像ID

  1. [root@zy-host ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. newdocker_images v1.0.0 5b67fc4b9334 24 minutes ago 233 MB
  4. <none> <none> 99fd2c8058f6 25 minutes ago 233 MB
  5. docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
  6. docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB
  7. #显示摘要信息
  8. [root@zy-host ~]# docker images --digests
  9. REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
  10. newdocker_images v1.0.0 <none> 5b67fc4b9334 25 minutes ago 233 MB
  11. <none> <none> <none> 99fd2c8058f6 26 minutes ago 233 MB
  12. docker.io/nginx latest sha256:0047b729188a15da49380d9506d65959cce6d40291ccfb4e039f5dc7efd33286 1403e55ab369 11 days ago 142 MB
  13. docker.io/centos latest sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 5d0da3dc9764 15 months ago 231 MB

(15)docker rmi

功能:删除镜像

常用参数:

-f:强制删除

  1. [root@zy-host ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. newdocker_images v1.0.0 5b67fc4b9334 30 minutes ago 233 MB
  4. <none> <none> 99fd2c8058f6 31 minutes ago 233 MB
  5. docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
  6. docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB
  7. [root@zy-host ~]# docker rmi 99fd2c8058f6
  8. Deleted: sha256:99fd2c8058f6594b90569b3c8af43859244a85f650a5b7ee8d0e970fa986081b
  9. [root@zy-host ~]# docker images
  10. REPOSITORY TAG IMAGE ID CREATED SIZE
  11. newdocker_images v1.0.0 5b67fc4b9334 30 minutes ago 233 MB
  12. docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
  13. docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB

 (16)docker tag

功能:标记本地镜像

  1. [root@zy-host ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. newdocker_images v1.0.0 5b67fc4b9334 31 minutes ago 233 MB
  4. docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
  5. docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB
  6. [root@zy-host ~]# docker tag newdocker_images:v1.0.0 newdocker_images:v2.0.0
  7. [root@zy-host ~]# docker images
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. newdocker_images v1.0.0 5b67fc4b9334 33 minutes ago 233 MB
  10. newdocker_images v2.0.0 5b67fc4b9334 33 minutes ago 233 MB
  11. docker.io/nginx latest 1403e55ab369 11 days ago 142 MB
  12. docker.io/centos latest 5d0da3dc9764 15 months ago 231 MB

两个镜像的ID是一样的,类似于Linux文件中与文件的硬链接。

(17)docker build

功能:使用 Dockerfile 创建镜像

常用参数如下:

-f:指定要使用的 Dockerfile 路径

--label=[] :设置镜像使用的元数据

-m:设置最大内存值

--memory-swap:设置swap的最大值为内存+swap,“-1”表示不限swap

--no-cache:创建镜像的过程不适用缓存

--pull:尝试去更新镜像的新版本

-q:安静模式,成功后只输出镜像ID

--rm:设置镜像成功后删除中间容器

--ulimit:Ulimit配置

[root@zy-host ~]# docker build https://github.com/nginxinc/docker-nginx/

(18)docker history

功能:查看指定镜像的创建历史

常用参数:

-H:以可读的格式打印镜像大小和日期,默认为true

--no-trunc:显示完整的提交记录

-q:仅列出提交记录ID

  1. [root@zy-host ~]# docker history newdocker_images:v2.0.0
  2. IMAGE CREATED CREATED BY SIZE COMMENT
  3. 5b67fc4b9334 38 minutes ago /bin/bash 1.67 MB add a new images
  4. 5d0da3dc9764 15 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
  5. <missing> 15 months ago /bin/sh -c #(nop) LABEL org.label-schema.... 0 B
  6. <missing> 15 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0b... 231 MB

(19)docker info

功能:显示Docker系统信息,包括镜像和容器数。

  1. [root@zy-host ~]# docker info
  2. Containers: 7
  3. Running: 5
  4. Paused: 0
  5. Stopped: 2
  6. Images: 3
  7. Server Version: 1.13.1
  8. Storage Driver: overlay2
  9. Backing Filesystem: extfs
  10. Supports d_type: true
  11. Native Overlay Diff: true
  12. Logging Driver: journald
  13. Cgroup Driver: systemd
  14. Plugins:
  15. Volume: local
  16. Network: bridge host macvlan null overlay
  17. Swarm: inactive
  18. Runtimes: docker-runc runc
  19. Default Runtime: docker-runc
  20. Init Binary: /usr/libexec/docker/docker-init-current
  21. containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
  22. runc version: 8891bca22c049cd2dcf13ba2438c0bac8d7f3343 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
  23. init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
  24. Security Options:
  25. seccomp
  26. WARNING: You're not using the default seccomp profile
  27. Profile: /etc/docker/seccomp.json
  28. Kernel Version: 3.10.0-1127.19.1.el7.x86_64
  29. Operating System: CentOS Linux 7 (Core)
  30. OSType: linux
  31. Architecture: x86_64
  32. Number of Docker Hooks: 3
  33. CPUs: 1
  34. Total Memory: 1.795 GiB
  35. Name: zy-host
  36. ID: 6NL7:3E47:B4TG:4DB4:EJT4:QJ75:H7MC:JBDR:5BX5:UOMT:EM2W:ZQVQ
  37. Docker Root Dir: /var/lib/docker
  38. Debug Mode (client): false
  39. Debug Mode (server): false
  40. Registry: https://index.docker.io/v1/
  41. WARNING: bridge-nf-call-iptables is disabled
  42. WARNING: bridge-nf-call-ip6tables is disabled
  43. Experimental: false
  44. Insecure Registries:
  45. 127.0.0.0/8
  46. Live Restore Enabled: false
  47. Registries: docker.io (secure)

(20)docker version

功能:显示Docker的版本信息

  1. [root@zy-host ~]# docker version
  2. Client:
  3. Version: 1.13.1
  4. API version: 1.26
  5. Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
  6. Go version: go1.10.3
  7. Git commit: 7d71120/1.13.1
  8. Built: Wed Mar 2 15:25:43 2022
  9. OS/Arch: linux/amd64
  10. Server:
  11. Version: 1.13.1
  12. API version: 1.26 (minimum version 1.12)
  13. Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
  14. Go version: go1.10.3
  15. Git commit: 7d71120/1.13.1
  16. Built: Wed Mar 2 15:25:43 2022
  17. OS/Arch: linux/amd64
  18. Experimental: false

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号