docker 的一些基础信息功能以及日志

关于json

1 jq

1.1安装

yum install -y jq

1.2简单介绍

Example:

        $ echo '{"foo": 0}' | jq .
        {
                "foo": 0
        }

Some of the options include:
  -c               compact instead of pretty-printed output;                                           #紧凑而不是漂亮的打印输出
  -n               use `null` as the single input value;                                               #使用“null”作为单个输入值
  -e               set the exit status code based on the output;                                       #基于所述输出设置所述退出状态代码;
  -s               read (slurp) all inputs into an array; apply filter to it;                          #将所有输入读取(slurp)到一个数组中;对其应用过滤器;
  -r               output raw strings, not JSON texts;                                                 #输出原始字符串,而不是JSON文本;
  -R               read raw strings, not JSON texts;                                                   #读取原始字符串,而不是JSON文本;
  -C               colorize JSON;                                                                      #将JSON着色;
  -M               monochrome (don't colorize JSON);                                                   #单色(不要将JSON着色);
  -S               sort keys of objects on output;                                                     #对输出对象的键进行排序;
  --tab            use tabs for indentation;                                                           #使用制表符进行缩进;
  --arg a v        set variable $a to value <v>;                                                       #将变量$a设置为值<v>;
  --argjson a v    set variable $a to JSON value <v>;
  --slurpfile a f  set variable $a to an array of JSON texts read from <f>;
  --rawfile a f    set variable $a to a string consisting of the contents of <f>;
  --args           remaining arguments are string arguments, not files;                                #剩下的参数是字符串参数,而不是文件;
  --jsonargs       remaining arguments are JSON arguments, not files;                                  #剩下的参数是JSON参数,而不是文件;
  --               terminates argument processing;

Named arguments are also available as $ARGS.named[], while                                            
positional arguments are available as $ARGS.positional[].

See the manpage for more options.

1.3简单使用

1.3.1如何使用 JQ 命令组织 JSON 数据
cat employee.json
{"workers":{"name": "John Brooks","id": "003"}}


jq '.' employee.json 
{
  "workers": {
    "name": "John Brooks",
    "id": "003"
  }
}

1.3.2如何使用 JQ 命令访问属性
jq '.workers' employee.json 
{
  "name": "John Brooks",
  "id": "003"
}

jq '.workers.name' employee.json 
"John Brooks"

1.3.3如何使用 JQ 命令访问数组项
我们还可以使用 .[] 运算符访问和输出 JSON 文件中数组中存在的元素。对于这个例子,我们将修改我们的 JSON 文件,添加下面内容:

[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
cat employee1.json
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]

 jq .[] employee1.json 
{
  "name": "John Brooks",
  "id": "003"
}
{
  "name": "Randy Park",
  "id": "053"
}
{
  "name": "Todd Gray",
  "id": "009"
}



要仅输出第二个数组,我们可以通过以下方式修改上述命令:
jq .[1] employee1.json 
{
  "name": "Randy Park",
  "id": "053"
}
请记住,数组从索引 0 开始的。


我们还可以使用 .字段 运算符访问数组中存在的属性。例如,如果我们想访问第三个数组中的 name 属性,那么我们将运行以下命令
 jq '.[2].name' employee1.json 
"Todd Gray"

类似地,要访问数组中的所有名称属性,我们可以执行以下命令:
jq '.[].name' employee.json 
"John Brooks"
"Randy Park"
"Todd Gray"




2 docker inspect 对jq的使用

2.1使用步骤

2.1.1把数据变成json格式
docker inspect wordpress  |jq
2.1.2得要想要的数据
 docker inspect wordpress  |jq .[].Id
"sha256:c3c92cc3dcb1a903fed0374a837f38d716ae104d0e4c9705bddb53a76419534d"

2.2 docker inspect nginx --format '{{.Id}}'
docker inspect  $(容器id) --format '{{.State.Pid}}, {{.Id}}, {{.Name}}, {{.GraphDriver}}' 
2.3 万能grep
docker inspect nginx   |grep  -i id 
        "Id": "sha256:080ed0ed8312deca92e9a769b518cdfa20f5278359bd156f3469dd8fa532db6b",

3日志

####3.1获取某个时间以后的日志
docker logs --since 2023-04-04T00:00:00
####3.2获取实时的日志
docker logs -f 
####3.3获取最后300行的日志
docker logs -f  --tail=300  
####3.3获取某个时间段的日志
docker logs --since 2023-04-04T00:00:00  --until 2023-04-05T00:00:00
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • docker inspect 获取容器/镜像的元数据 你可以直接使用docker inspect name(容器名...
    哪个鹿阅读 4,229评论 0 2
  • 想要改进这个备忘单吗?参见[贡献](#贡献)部分! 目录 [为何选择Docker](#why-docker) [先...
    iOSDevLog阅读 2,137评论 0 3
  • Tip1 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubuntu): ...
    Java高级新技术阅读 243评论 0 0
  • 一. 什么是Docker 在docker的官方之什么是docker中提到了一句话:“当今各大组织或者团体的创新都源...
    dddye阅读 1,199评论 0 1
  • 1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubuntu): $I...
    yshenhn阅读 490评论 0 1