docker api,附加容器返回额外的字符

时间:2014-08-06 12:58:59

标签: javascript api curl docker

我在浏览器中使用javascript消费docker api, 我启动了一个执行' composer install'命令然后我附加到它以获得输出。 一切正常但我在每一行的开头都有一些额外的字符

    -Installing assets using the hard copy option
    PInstalling assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
    BInstalling assets for Propel\PropelBundle into web/bundles/propel

特别是在这两行示例中安装单词之前的P和B,但实际上是随机字母或数字。

在显示之前,是否需要解析或处理附加容器的输出?

注意:不仅来自浏览器而且来自终端curl命令,也在几乎行的开头获得相同的随机字符

curl 'http://localhost/dockerapi/containers/54053a10dc05b45c400b16733d2edd1cbfb6b6d877badf5cc26d3da7d2165375/attach?logs=1&stream=1&stdout=1' 
-X POST 
-H 'Origin: http://localhost' 
-H 'Accept-Encoding: gzip,deflate,sdch' 
-H 'Accept-Language: en-US,en;q=0.8,es;q=0.6' 
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'
-H 'Accept: application/json, text/plain, */*' 
-H 'Referer: http://localhost/' 
-H 'Connection: keep-alive'
-H 'Content-Length: 0' 
--compressed

1 个答案:

答案 0 :(得分:0)

原来这里记录了这一点 http://docs.docker.com/reference/api/docker_remote_api_v1.13/#attach-to-a-container

我只是忽略了,因为我认为的文本格式是其余的响应,但它实际上是文档。 @soullou用户已在https://github.com/docker/docker/issues/7375#issuecomment-51462963

中澄清了这一点
It is encoded on the first 8 bytes like this:

header := [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}

`STREAM_TYPE` can be:

-   0: stdin (will be written on stdout)
-   1: stdout
-   2: stderr

`SIZE1, SIZE2, SIZE3, SIZE4` are the 4 bytes of
the uint32 size encoded as big endian.