5

How to use httpid and jq within docker?

I want to get the ip only in json result like '34.10.12.40'

docker run -it --rm blacktop/httpie -b ifconfig.co/json
{
    "country": "United States",
    "country_eu": false,
    "country_iso": "US",
    "hostname": "lqwtx.com",
    "ip": "34.10.12.40",
    "ip_decimal": 39585,
    "latitude": 35,
    "longitude": 105
}

but I've tried some ways but does not work, like blow.

docker run -it --rm blacktop/httpie ash -c "http -b ifconfig.co/json | jq '.ip’”

docker run -it --rm blacktop/httpie -b ifconfig.co/json jq '.ip'
4

1 回答 1

6

这是您使用的图像的 dockerfile,请参见

FROM alpine:latest

RUN apk add --no-cache jq httpie

ENTRYPOINT [ "http" ]
CMD [ "--help" ]

从上面,你可以看到它为这个图像设置了一个entrypointwith http,所以你使用的所有命令docker run都将作为参数http,所以你当然会失败。

接下来是您使用它的方法:

docker run -it --rm --entrypoint=/bin/sh blacktop/httpie -c "http -b ifconfig.co/json | jq '.ip'"

样本输出:

"92.121.64.197"

entrypoint上面的命令将用覆盖默认值/bin/sh,然后您可以使用它jq来解析httpie管道中的输出。

于 2019-06-05T02:25:19.700 回答