Docker API不能应用json过滤器 [英] Docker API can’t apply json filters

查看:335
本文介绍了Docker API不能应用json过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 https:// docs。 docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-tasks ,过滤器只能用于获取运行具有特定服务名称的容器。由于某些原因,我将获得所有任务的完整列表,无论其名称或所需状态如何。我找不到使用带有Docker API的JSON请求的curl的正确示例。
我使用以下命令:



A)


curl -X GET -HContent-Type:application / json-d{filters:[{service:demo,desired-state:running}]}' https:// HOSTNAME:2376 / tasks --cert〜/ .docker / cert.pem --key〜/ .docker /key.pem --cacert〜/ .docker / ca.pem


返回一切



B)
试图通过 Docker远程API过滤器退出工作


curl https:// HOSTNAME:2376 / containers / json?all = 1& filters = {%22status%22:[%22exited%22]} --cert〜/ .docker /cert.pem --key〜/ .docker / key.pem --cacert〜/ .docker / ca.pem


一个返回卷曲: (60)同行证书发行人不被承认,所以我猜卷曲请求是错误的。



我已经问过Docker论坛,他们帮了一会儿。我很惊讶,互联网上没有适当的文档,关于如何使用Docker API卷曲,或者是如此明显,我不明白什么?

解决方案

我应该以这个事实为前提,我从来没有看到卷曲错误地报告证书错误,实际上还有一些其他的问题,但我会相信你的说法,这是实际上不是证书问题。



我以为先前认为你对过滤器的论证是不正确的,因为
根据 API参考过滤器参数是...


过滤器的JSON编码值(a map [string] [] string)在容器列表中进行处理。


我不太清楚如何解释地图[字符串] []串,所以我在我的Docker客户端和服务器之间设置一个日志代理,并运行 docker ps -f status = exited ,产生以下请求:

  GET /v1.24/containers/json?filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D HTTP /如果我们将参数解码为过滤器 

$ c>,我们看到它是:

  {status:{退出:true}} 

而您正在传递:

 code> {status:[exited]} 

,而且我假设这是问题的根源...但是当试图验证这个问题时,我遇到了一个好奇的问题。我不能以书面方式运行您的 curl 命令行,因为curl试图执行一些globbing行为由于大括号:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (3)列中的[globbing]嵌套括号

如果我正确引用你的参数过滤: / p>

  $ python -c'import urllib; print urllib.quote({status:[exited]})'
%7B%22状态%22%3A%5B%22有效%22%5D%7D

似乎工作正常:

  $ curl http:// localhost:2376 / containers / json'?filters =%7B%22status%22%3A%5B%22exited%22%5D%7D'
[{Id :...

如果我使用您的原始表达式并通过 -g (aka - globoff )以禁用大括号扩展:

  $ curl -g http:// localhost:2376 / containers / json'?filters = {%22status%22:[%22exited%22]}'
[ {Id:...

我想强调的一件事是坚持使用docker客户端和服务器之间的代理。如果你发现自己在问:如何使用这个API?,一个很好的答案就是看看Docker客户端在同样的情况下正在做什么。


According to the https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-tasks, filter can be only used to get running containers with a particular service name. For some reason, I am getting a full list of all tasks regardless of their names or desired states. I can't find any proper examples of using curl with JSON requests with Docker API. I'm using the following command:

A)

curl -X GET -H "Content-Type: application/json" -d '{"filters":[{ "service":"demo", "desired-state":"running" }]}' https://HOSTNAME:2376/tasks --cert ~/.docker/cert.pem --key ~/.docker/key.pem --cacert ~/.docker/ca.pem

Returns everything

B) trying to get something working from Docker Remote API Filter Exited

curl https://HOSTNAME:2376/containers/json?all=1&filters={%22status%22:[%22exited%22]} --cert ~/.docker/cert.pem --key ~/.docker/key.pem --cacert ~/.docker/ca.pem

This one returns "curl: (60) Peer's Certificate issuer is not recognized.", so I guess that curl request is malformed.

I have asked on Docker forums and they helped a little. I'm amazed that there are no proper documentation anywhere on the internet on how to use Docker API with curl or is it so obvious and I don't understand something?

解决方案

I should prefix this with the fact that I have never seen curl erroneously report a certificate error when in fact there was some sort of other issue in play, but I will trust your assertion that this is in fact not a certificate problem.

I thought at first that your argument to filters was incorrect, because according to the API reference, the filters parameter is...

a JSON encoded value of the filters (a map[string][]string) to process on the containers list.

I wasn't exactly sure how to interpret map[string][]string, so I set up a logging proxy between my Docker client and server and ran docker ps -f status=exited, which produced the following request:

GET /v1.24/containers/json?filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D HTTP/1.1\r

If we decode the argument to filters, we see that it is:

{"status":{"exited":true}}

Whereas you are passing:

{"status":["exited"]}

So that's different, obviously, and I was assuming that was the source of the problem...but when trying to verify that, I ran into a curious problem. I can't even run your curl command line as written, because curl tries to perform some globbing behavior due to the braces:

$ curl http://localhost:2376/containers/json'?filters={%22status%22:[%22exited%22]}'
curl: (3) [globbing] nested brace in column 67

If I correctly quote your arguments to filter:

$ python -c 'import urllib; print urllib.quote("""{"status":["exited"]}""")'
%7B%22status%22%3A%5B%22exited%22%5D%7D

It seems to work just fine:

$ curl http://localhost:2376/containers/json'?filters=%7B%22status%22%3A%5B%22exited%22%5D%7D'
[{"Id":...

I can get the same behavior if I use your original expression and pass -g (aka --globoff) to disable the brace expansion:

$ curl -g http://localhost:2376/containers/json'?filters={%22status%22:[%22exited%22]}'
[{"Id":...

One thing I would like to emphasize is the utility of sticking a proxy between the docker client and server. If you ever find yourself asking, "how do I use this API?", an excellent answer is to see exactly what the Docker client is doing in the same situation.

这篇关于Docker API不能应用json过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆