镜像命令

  1. docker images # 查看所有本地主机上的镜像, 可以使用docker image ls 代替
  2. docker search # 搜索镜像
  3. docker pull # 下载镜像 docker image pull
  4. docker rmi # 删除镜像 docker image rm

docker images 查看所有本地的主机的镜像

  1. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest bf756fb1ae65 4 months ago 13.3kB
  4. mysql 5.7 b84d68d0a7db 6 days ago 448MB
  5. # 解释
  6. #REPOSITORY # 镜像的仓库源
  7. #TAG # 镜像的标签(版本) ---lastest 表示最新版本
  8. #IMAGE ID # 镜像的id
  9. #CREATED # 镜像的创建时间
  10. #SIZE # 镜像的大小
  11. # 可选项
  12. Options:
  13. -a, --all Show all images (default hides intermediate images) #列出所有镜像
  14. -q, --quiet Only show numeric IDs # 只显示镜像的id
  15. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker images -a #列出所有镜像详细信息
  16. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker images -aq #列出所有镜像的id
  17. d5f28a0bb0d0
  18. f19c56ce92a8
  19. 1b6b1fe7261e
  20. 1b6b1fe7261e

docker search 搜索镜像

  1. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker search mysql
  2. # --filter=STARS=3000 #过滤,搜索出来的镜像收藏STARS数量大于3000的
  3. Options:
  4. -f, --filter filter Filter output based on conditions provided
  5. --format string Pretty-print search using a Go template
  6. --limit int Max number of search results (default 25)
  7. --no-trunc Don't truncate output
  8. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker search mysql --filter=STARS=3000
  9. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  10. mysql MySQL IS ... 9520 [OK]
  11. mariadb MariaDB IS ... 3456 [OK]

docker pull 下载镜像

  1. # 下载镜像 docker pull 镜像名[:tag]
  2. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker pull tomcat:8
  3. 8: Pulling from library/tomcat #如果不写tag,默认就是latest
  4. 90fe46dd8199: Already exists #分层下载: docker image 的核心 联合文件系统
  5. 35a4f1977689: Already exists
  6. bbc37f14aded: Already exists
  7. 74e27dc593d4: Already exists
  8. 93a01fbfad7f: Already exists
  9. 1478df405869: Pull complete
  10. 64f0dd11682b: Pull complete
  11. 68ff4e050d11: Pull complete
  12. f576086003cf: Pull complete
  13. 3b72593ce10e: Pull complete
  14. Digest: sha256:0c6234e7ec9d10ab32c06423ab829b32e3183ba5bf2620ee66de866df # 签名防伪
  15. Status: Downloaded newer image for tomcat:8
  16. docker.io/library/tomcat:8 #真实地址
  17. #等价于
  18. docker pull tomcat:8
  19. docker pull docker.io/library/tomcat:8

docker rmi 删除镜像

  1. docker rmi -f 镜像id #删除指定id的镜像
  2. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker rmi -f f19c56ce92a8
  3. docker rmi -f $(docker images -aq) #删除全部的镜像
  4. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker stop $(docker ps -a -q)

容器命令

镜像下载

  1. #docker中下载centos
  2. docker pull centos
  1. docker run 镜像ID # 新建容器并启动
  2. docker ps # 列出所有运行的容器 docker container list
  3. docker rm # 容器id #删除指定容器
  4. docker start 容器id # 启动容器
  5. docker restart 容器id # 重启容器
  6. docker stop 容器id # 停止当前正在运行的容器
  7. docker kill 容器id # 强制停止当前容器

新建容器并启动

  1. docker run [可选参数] image | docker container run [可选参数] image
  2. #参书说明
  3. --name="Name" # 容器名字 tomcat01 tomcat02 用来区分容器
  4. -d # 后台方式运行
  5. -it # 使用交互方式运行,进入容器查看内容
  6. -p # 指定容器的端口 -p 8080(宿主机):8080(容器)
  7. -p ip:主机端口:容器端口
  8. -p 主机端口:容器端口(常用)
  9. -p 容器端口
  10. -P(大写) # 随机指定端口
  11. # 测试、启动并进入容器
  12. [root@iz2zeak7sgj6i7hrb2g
  13. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -it centos /bin/bash
  14. [root@241b5abce65e /]# ls
  15. bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
  16. [root@241b5abce65e /]# exit #从容器退回主机
  17. exit

列出所有运行的容器

  1. docker ps 命令 # 列出当前正在运行的容器
  2. -a, --all # 列出当前正在运行的容器 + 带出历史运行过的容器
  3. -n=?, --last int # 列出最近创建的?个容器 ?为1则只列出最近创建的一个容器,为2则列出2个
  4. -q, --quiet # 只列出容器的编号

退出容器

  1. exit #容器直接退出
  2. ctrl + P + Q #容器不停止退出 ---注意:这个很有用的操作

删除容器

  1. docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -rf
  2. docker rm -f $(docker ps -aq) # 删除所有的容器
  3. docker ps -a -q|xargs docker rm # 删除所有的容器

启动和停止容器的操作

  1. docker start 容器id #启动容器
  2. docker restart 容器id #重启容器
  3. docker stop 容器id #停止当前正在运行的容器
  4. docker kill 容器id #强制停止当前容器

进入当前正在运行的容器

  1. # 方式一:进入当前容器后开启一个新的终端,可以在里面操作
  2. docker exec -it 容器ID /bin/bash
  3. # 方式二:进入容器正在执行的终端
  4. docker attach 容器ID

从容器内拷贝到主机

  1. docker cp 容器id:容器内路径 主机目的路径
  2. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 56a5583b25b4 centos "/bin/bash" 7seconds ago Up 6 seconds
  5. #1. 进入docker容器内部
  6. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker exec -it 56a5583b25b4 /bin/bash
  7. [root@55321bcae33d /]# ls
  8. bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
  9. #新建一个文件
  10. [root@55321bcae33d /]# echo "hello" > java.java
  11. [root@55321bcae33d /]# cat hello.java
  12. hello
  13. [root@55321bcae33d /]# exit
  14. exit
  15. #hello.java拷贝到home文件加下
  16. [root@iz2zeak7sgj6i7hrb2g862z /]# docker cp 56a5583b25b4:/hello.java /home
  17. [root@iz2zeak7sgj6i7hrb2g862z /]# cd /home
  18. [root@iz2zeak7sgj6i7hrb2g862z home]# ls -l #可以看见java.java存在
  19. total 8
  20. -rw-r--r-- 1 root root 0 May 19 22:09 haust.java
  21. -rw-r--r-- 1 root root 6 May 22 11:12 java.java
  22. drwx------ 3 www www 4096 May 8 12:14 www

其他命令

帮助命令

  1. docker version # 显示docker的版本信息
  2. docker info # 显示docker的系统信息,包括镜像和容器的数量
  3. dokcer 命令 -- help # 帮助命令

后台启动命令

  1. # 命令 docker run -d 镜像名
  2. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -d centos
  3. a8f922c255859622ac45ce3a535b7a0e8253329be4756ed6e32265d2dd2fac6c
  4. [root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. # 问题docker ps. 发现centos 停止了
  7. # 常见的坑,docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
  8. # nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

  1. docker logs --help
  2. Options:
  3. --details Show extra details provided to logs
  4. * -f, --follow Follow log output
  5. --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
  6. * --tail string Number of lines to show from the end of the logs (default "all")
  7. * -t, --timestamps Show timestamps
  8. --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
  9. ~ docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done" #模拟日志
  10. #显示日志
  11. -tf #显示日志信息(一直更新)
  12. --tail number #需要显示日志条数
  13. docker logs -t --tail n 容器id #查看n行日志
  14. docker logs -ft 容器id #跟着日志

查看容器中进程信息ps

  1. # 命令 docker top 容器id

查看镜像的元数据

  1. # 命令
  2. docker inspect 容器id
  3. #测试
  4. ~ docker inspect 55321bcae33d
  5. [
  6. {
  7. "Id": "55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066",
  8. "Created": "2020-05-15T05:22:05.515909071Z",
  9. "Path": "/bin/sh",
  10. "Args": [
  11. "-c",
  12. "while true;do echo 6666;sleep 1;done"
  13. ],
  14. "State": {
  15. "Status": "running",
  16. "Running": true,
  17. "Paused": false,
  18. "Restarting": false,
  19. "OOMKilled": false,
  20. "Dead": false,
  21. "Pid": 22973,
  22. "ExitCode": 0,
  23. "Error": "",
  24. "StartedAt": "2020-05-15T05:22:06.165904633Z",
  25. "FinishedAt": "0001-01-01T00:00:00Z"
  26. },
  27. "Image": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee",
  28. "ResolvConfPath": "/var/lib/docker/containers/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066/resolv.conf",
  29. "HostnamePath": "/var/lib/docker/containers/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066/hostname",
  30. "HostsPath": "/var/lib/docker/containers/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066/hosts",
  31. "LogPath": "/var/lib/docker/containers/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066-json.log",
  32. "Name": "/bold_bell",
  33. "RestartCount": 0,
  34. "Driver": "overlay2",
  35. "Platform": "linux",
  36. "MountLabel": "",
  37. "ProcessLabel": "",
  38. "AppArmorProfile": "docker-default",
  39. "ExecIDs": null,
  40. "HostConfig": {
  41. "Binds": null,
  42. "ContainerIDFile": "",
  43. "LogConfig": {
  44. "Type": "json-file",
  45. "Config": {}
  46. },
  47. "NetworkMode": "default",
  48. "PortBindings": {},
  49. "RestartPolicy": {
  50. "Name": "no",
  51. "MaximumRetryCount": 0
  52. },
  53. "AutoRemove": false,
  54. "VolumeDriver": "",
  55. "VolumesFrom": null,
  56. "CapAdd": null,
  57. "CapDrop": null,
  58. "Capabilities": null,
  59. "Dns": [],
  60. "DnsOptions": [],
  61. "DnsSearch": [],
  62. "ExtraHosts": null,
  63. "GroupAdd": null,
  64. "IpcMode": "private",
  65. "Cgroup": "",
  66. "Links": null,
  67. "OomScoreAdj": 0,
  68. "PidMode": "",
  69. "Privileged": false,
  70. "PublishAllPorts": false,
  71. "ReadonlyRootfs": false,
  72. "SecurityOpt": null,
  73. "UTSMode": "",
  74. "UsernsMode": "",
  75. "ShmSize": 67108864,
  76. "Runtime": "runc",
  77. "ConsoleSize": [
  78. 0,
  79. 0
  80. ],
  81. "Isolation": "",
  82. "CpuShares": 0,
  83. "Memory": 0,
  84. "NanoCpus": 0,
  85. "CgroupParent": "",
  86. "BlkioWeight": 0,
  87. "BlkioWeightDevice": [],
  88. "BlkioDeviceReadBps": null,
  89. "BlkioDeviceWriteBps": null,
  90. "BlkioDeviceReadIOps": null,
  91. "BlkioDeviceWriteIOps": null,
  92. "CpuPeriod": 0,
  93. "CpuQuota": 0,
  94. "CpuRealtimePeriod": 0,
  95. "CpuRealtimeRuntime": 0,
  96. "CpusetCpus": "",
  97. "CpusetMems": "",
  98. "Devices": [],
  99. "DeviceCgroupRules": null,
  100. "DeviceRequests": null,
  101. "KernelMemory": 0,
  102. "KernelMemoryTCP": 0,
  103. "MemoryReservation": 0,
  104. "MemorySwap": 0,
  105. "MemorySwappiness": null,
  106. "OomKillDisable": false,
  107. "PidsLimit": null,
  108. "Ulimits": null,
  109. "CpuCount": 0,
  110. "CpuPercent": 0,
  111. "IOMaximumIOps": 0,
  112. "IOMaximumBandwidth": 0,
  113. "MaskedPaths": [
  114. "/proc/asound",
  115. "/proc/acpi",
  116. "/proc/kcore",
  117. "/proc/keys",
  118. "/proc/latency_stats",
  119. "/proc/timer_list",
  120. "/proc/timer_stats",
  121. "/proc/sched_debug",
  122. "/proc/scsi",
  123. "/sys/firmware"
  124. ],
  125. "ReadonlyPaths": [
  126. "/proc/bus",
  127. "/proc/fs",
  128. "/proc/irq",
  129. "/proc/sys",
  130. "/proc/sysrq-trigger"
  131. ]
  132. },
  133. "GraphDriver": {
  134. "Data": {
  135. "LowerDir": "/var/lib/docker/overlay2/1f347949ba49c4dbee70cea9ff3af39a14e602bc8fac8331c46347bf6708757a-init/diff:/var/lib/docker/overlay2/5afcd8220c51854a847a36f52775b4ed0acb16fe6cfaec3bd2e5df59863835ba/diff",
  136. "MergedDir": "/var/lib/docker/overlay2/1f347949ba49c4dbee70cea9ff3af39a14e602bc8fac8331c46347bf6708757a/merged",
  137. "UpperDir": "/var/lib/docker/overlay2/1f347949ba49c4dbee70cea9ff3af39a14e602bc8fac8331c46347bf6708757a/diff",
  138. "WorkDir": "/var/lib/docker/overlay2/1f347949ba49c4dbee70cea9ff3af39a14e602bc8fac8331c46347bf6708757a/work"
  139. },
  140. "Name": "overlay2"
  141. },
  142. "Mounts": [],
  143. "Config": {
  144. "Hostname": "55321bcae33d",
  145. "Domainname": "",
  146. "User": "",
  147. "AttachStdin": false,
  148. "AttachStdout": false,
  149. "AttachStderr": false,
  150. "Tty": false,
  151. "OpenStdin": false,
  152. "StdinOnce": false,
  153. "Env": [
  154. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  155. ],
  156. "Cmd": [
  157. "/bin/sh",
  158. "-c",
  159. "while true;do echo 6666;sleep 1;done"
  160. ],
  161. "Image": "centos",
  162. "Volumes": null,
  163. "WorkingDir": "",
  164. "Entrypoint": null,
  165. "OnBuild": null,
  166. "Labels": {
  167. "org.label-schema.build-date": "20200114",
  168. "org.label-schema.license": "GPLv2",
  169. "org.label-schema.name": "CentOS Base Image",
  170. "org.label-schema.schema-version": "1.0",
  171. "org.label-schema.vendor": "CentOS",
  172. "org.opencontainers.image.created": "2020-01-14 00:00:00-08:00",
  173. "org.opencontainers.image.licenses": "GPL-2.0-only",
  174. "org.opencontainers.image.title": "CentOS Base Image",
  175. "org.opencontainers.image.vendor": "CentOS"
  176. }
  177. },
  178. "NetworkSettings": {
  179. "Bridge": "",
  180. "SandboxID": "63ed0c837f35c12453bae9661859f37a08541a0749afb86e881869bf6fd9031b",
  181. "HairpinMode": false,
  182. "LinkLocalIPv6Address": "",
  183. "LinkLocalIPv6PrefixLen": 0,
  184. "Ports": {},
  185. "SandboxKey": "/var/run/docker/netns/63ed0c837f35",
  186. "SecondaryIPAddresses": null,
  187. "SecondaryIPv6Addresses": null,
  188. "EndpointID": "b129d9a5a2cbb92722a2101244bd81a9e3d8af034e83f338c13790a1a94552a1",
  189. "Gateway": "172.17.0.1",
  190. "GlobalIPv6Address": "",
  191. "GlobalIPv6PrefixLen": 0,
  192. "IPAddress": "172.17.0.4",
  193. "IPPrefixLen": 16,
  194. "IPv6Gateway": "",
  195. "MacAddress": "02:42:ac:11:00:04",
  196. "Networks": {
  197. "bridge": {
  198. "IPAMConfig": null,
  199. "Links": null,
  200. "Aliases": null,
  201. "NetworkID": "ad5ada6a106f5ba3dda9ce4bc1475a4bb593bf5f7fbead72196e66515e8ac36a",
  202. "EndpointID": "b129d9a5a2cbb92722a2101244bd81a9e3d8af034e83f338c13790a1a94552a1",
  203. "Gateway": "172.17.0.1",
  204. "IPAddress": "172.17.0.4",
  205. "IPPrefixLen": 16,
  206. "IPv6Gateway": "",
  207. "GlobalIPv6Address": "",
  208. "GlobalIPv6PrefixLen": 0,
  209. "MacAddress": "02:42:ac:11:00:04",
  210. "DriverOpts": null
  211. }
  212. }
  213. }
  214. }
  215. ]

命令大全

  1. attach Attach local standard input, output, and error streams to a running container #当前shell下 attach连接指定运行的镜像
  2. build Build an image from a Dockerfile # 通过Dockerfile定制镜像
  3. commit Create a new image from a container's changes #提交当前容器为新的镜像
  4. cp Copy files/folders between a container and the local filesystem #拷贝文件
  5. create Create a new container #创建一个新的容器
  6. diff Inspect changes to files or directories on a container's filesystem #查看docker容器的变化
  7. events Get real time events from the server # 从服务获取容器实时时间
  8. exec Run a command in a running container # 在运行中的容器上运行命令
  9. export Export a container's filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import]
  10. history Show the history of an image # 展示一个镜像形成历史
  11. images List images #列出系统当前的镜像
  12. import Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
  13. info Display system-wide information # 显示全系统信息
  14. inspect Return low-level information on Docker objects #查看容器详细信息
  15. kill Kill one or more running containers # kill指定docker容器
  16. load Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
  17. login Log in to a Docker registry #
  18. logout Log out from a Docker registry
  19. logs Fetch the logs of a container
  20. pause Pause all processes within one or more containers
  21. port List port mappings or a specific mapping for the container
  22. ps List containers
  23. pull Pull an image or a repository from a registry
  24. push Push an image or a repository to a registry
  25. rename Rename a container
  26. restart Restart one or more containers
  27. rm Remove one or more containers
  28. rmi Remove one or more images
  29. run Run a command in a new container
  30. save Save one or more images to a tar archive (streamed to STDOUT by default)
  31. search Search the Docker Hub for images
  32. start Start one or more stopped containers
  33. stats Display a live stream of container(s) resource usage statistics
  34. stop Stop one or more running containers
  35. tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  36. top Display the running processes of a container
  37. unpause Unpause all processes within one or more containers
  38. update Update configuration of one or more containers
  39. version Show the Docker version information
  40. wait Block until one or more containers stop, then print their exit codes