背景说明

Docker —format 参数提供了基于 Go模板 的日志格式化输出辅助功能,并提供了一些内置的增强函数,例如如下命令

  1. [root@template ~]# docker version
  2. Client: Docker Engine - Community
  3. Version: 20.10.14
  4. API version: 1.41
  5. Go version: go1.16.15
  6. Git commit: a224086
  7. Built: Thu Mar 24 01:49:57 2022
  8. OS/Arch: linux/amd64
  9. Context: default
  10. Experimental: true
  11. Server: Docker Engine - Community
  12. Engine:
  13. Version: 20.10.14
  14. API version: 1.41 (minimum version 1.12)
  15. Go version: go1.16.15
  16. Git commit: 87a90dc
  17. Built: Thu Mar 24 01:48:24 2022
  18. OS/Arch: linux/amd64
  19. Experimental: false
  20. containerd:
  21. Version: 1.5.11
  22. GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
  23. runc:
  24. Version: 1.0.3
  25. GitCommit: v1.0.3-0-gf46b6ba
  26. docker-init:
  27. Version: 0.19.0
  28. GitCommit: de40ad0
  29. [root@template ~]#

则可以使用--format进行定义JSON输出

  1. [root@template ~]# docker version --format='{{json .}}'
  2. {"Client":{"Platform":{"Name":"Docker Engine - Community"},"Version":"20.10.14","ApiVersion":"1.41","DefaultAPIVersion":"1.41","GitCommit":"a224086","GoVersion":"go1.16.15","Os":"linux","Arch":"amd64","BuildTime":"Thu Mar 24 01:49:57 2022","Context":"default","Experimental":true},"Server":{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"20.10.14","Details":{"ApiVersion":"1.41","Arch":"amd64","BuildTime":"Thu Mar 24 01:48:24 2022","Experimental":"false","GitCommit":"87a90dc","GoVersion":"go1.16.15","KernelVersion":"3.10.0-957.el7.x86_64","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.5.11","Details":{"GitCommit":"3df54a852345ae127d1fa3092b95168e4a88e2f8"}},{"Name":"runc","Version":"1.0.3","Details":{"GitCommit":"v1.0.3-0-gf46b6ba"}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":"de40ad0"}}],"Version":"20.10.14","ApiVersion":"1.41","MinAPIVersion":"1.12","GitCommit":"87a90dc","GoVersion":"go1.16.15","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-957.el7.x86_64","BuildTime":"2022-03-24T01:48:24.000000000+00:00"}}
  3. [root@template ~]# docker version --format='{{json .}}' | jq .
  4. {
  5. "Client": {
  6. "Platform": {
  7. "Name": "Docker Engine - Community"
  8. },
  9. "Version": "20.10.14",
  10. "ApiVersion": "1.41",
  11. "DefaultAPIVersion": "1.41",
  12. "GitCommit": "a224086",
  13. "GoVersion": "go1.16.15",
  14. "Os": "linux",
  15. "Arch": "amd64",
  16. "BuildTime": "Thu Mar 24 01:49:57 2022",
  17. "Context": "default",
  18. "Experimental": true
  19. },
  20. "Server": {
  21. "Platform": {
  22. "Name": "Docker Engine - Community"
  23. },
  24. "Components": [
  25. {
  26. "Name": "Engine",
  27. "Version": "20.10.14",
  28. "Details": {
  29. "ApiVersion": "1.41",
  30. "Arch": "amd64",
  31. "BuildTime": "Thu Mar 24 01:48:24 2022",
  32. "Experimental": "false",
  33. "GitCommit": "87a90dc",
  34. "GoVersion": "go1.16.15",
  35. "KernelVersion": "3.10.0-957.el7.x86_64",
  36. "MinAPIVersion": "1.12",
  37. "Os": "linux"
  38. }
  39. },
  40. {
  41. "Name": "containerd",
  42. "Version": "1.5.11",
  43. "Details": {
  44. "GitCommit": "3df54a852345ae127d1fa3092b95168e4a88e2f8"
  45. }
  46. },
  47. {
  48. "Name": "runc",
  49. "Version": "1.0.3",
  50. "Details": {
  51. "GitCommit": "v1.0.3-0-gf46b6ba"
  52. }
  53. },
  54. {
  55. "Name": "docker-init",
  56. "Version": "0.19.0",
  57. "Details": {
  58. "GitCommit": "de40ad0"
  59. }
  60. }
  61. ],
  62. "Version": "20.10.14",
  63. "ApiVersion": "1.41",
  64. "MinAPIVersion": "1.12",
  65. "GitCommit": "87a90dc",
  66. "GoVersion": "go1.16.15",
  67. "Os": "linux",
  68. "Arch": "amd64",
  69. "KernelVersion": "3.10.0-957.el7.x86_64",
  70. "BuildTime": "2022-03-24T01:48:24.000000000+00:00"
  71. }
  72. }
  73. [root@template ~]#

仅输出版本信息

  1. [root@template ~]# docker version --format='{{ .Client.Version }}'
  2. 20.10.14
  3. [root@template ~]#

解决方案

镜像输出

  1. [root@template docker]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. busybox latest beae173ccac6 3 months ago 1.24MB
  4. nginx latest 605c77e624dd 3 months ago 141MB
  5. [root@template docker]#
  6. [root@template docker]# docker images --format '{{ json . }}'| jq .
  7. {
  8. "Containers": "N/A",
  9. "CreatedAt": "2021-12-31 03:19:41 +0800 CST",
  10. "CreatedSince": "3 months ago",
  11. "Digest": "<none>",
  12. "ID": "beae173ccac6",
  13. "Repository": "busybox",
  14. "SharedSize": "N/A",
  15. "Size": "1.24MB",
  16. "Tag": "latest",
  17. "UniqueSize": "N/A",
  18. "VirtualSize": "1.24MB"
  19. }
  20. {
  21. "Containers": "N/A",
  22. "CreatedAt": "2021-12-30 03:28:29 +0800 CST",
  23. "CreatedSince": "3 months ago",
  24. "Digest": "<none>",
  25. "ID": "605c77e624dd",
  26. "Repository": "nginx",
  27. "SharedSize": "N/A",
  28. "Size": "141MB",
  29. "Tag": "latest",
  30. "UniqueSize": "N/A",
  31. "VirtualSize": "141.5MB"
  32. }

这里针对常用需求进行格式拼装

  1. "table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}"

命令执行验证

  1. [root@template docker]# docker images --format "table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}"
  2. REPOSITORY:TAG IMAGE ID CREATED AT
  3. busybox:latest beae173ccac6 2021-12-31 03:19:41 +0800 CST
  4. nginx:latest 605c77e624dd 2021-12-30 03:28:29 +0800 CST
  5. [root@template docker]#

容器输出

首先运行一个容器

  1. [root@template ~]# docker container run -d nginx
  2. d91aa5eac508bcd9ba44371dce3415bda6f3d9baaf15112cac611791ab877a9f

查看容器实例

  1. [root@template ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. d91aa5eac508 nginx "/docker-entrypoint.…" 11 seconds ago Up 10 seconds 80/tcp blissful_darwin
  4. [root@template ~]# docker ps --format '{{json .}}' |jq .
  5. {
  6. "Command": "\"/docker-entrypoint.…\"",
  7. "CreatedAt": "2022-04-09 05:49:13 +0800 CST",
  8. "ID": "d91aa5eac508",
  9. "Image": "nginx",
  10. "Labels": "maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>",
  11. "LocalVolumes": "0",
  12. "Mounts": "",
  13. "Names": "blissful_darwin",
  14. "Networks": "bridge",
  15. "Ports": "80/tcp",
  16. "RunningFor": "11 minutes ago",
  17. "Size": "1.09kB (virtual 141MB)",
  18. "State": "running",
  19. "Status": "Up 11 minutes"
  20. }
  21. [root@template ~]#

这里针对常用需求进行格式拼装

  1. "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Networks}}\t{{.Status}}\t{{.Ports}}"

命令执行验证

  1. [root@template ~]# docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Networks}}\t{{.Status}}\t{{.Ports}}"
  2. CONTAINER ID NAMES IMAGE NETWORKS STATUS PORTS
  3. d91aa5eac508 blissful_darwin nginx bridge Up 15 minutes 80/tcp
  4. [root@template ~]#

容器主机

  1. [root@template ~]# docker inspect --format "{{println}}{{.Name}} {{.Config.Image}} {{.Config.Hostname}} {{.NetworkSettings.IPAddress}} {{.NetworkSettings.Ports}} {{.Args}}{{println}}" $(docker ps -q)
  2. /blissful_darwin nginx d91aa5eac508 172.17.0.2 map[80/tcp:[]] [nginx -g daemon off;]
  3. [root@template ~]#

别名汇总

  1. alias docker-img='docker images --format "table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}"'
  2. alias docker-ps='docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Networks}}\t{{.Status}}\t{{.Ports}}"'
  3. alias docker-ip='docker inspect --format "{{println}}{{.Name}} {{.Config.Image}} {{.Config.Hostname}} {{.NetworkSettings.IPAddress}} {{.NetworkSettings.Ports}} {{.Args}}{{println}}" $(docker ps -q)'