Moby: API returns nothing when I want container's stats

Created on 4 Sep 2015  路  3Comments  路  Source: moby/moby

Hello, I have a problem with the Docker API, but first:

$ docker version
Client:
 Version:      1.8.2-rc1
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   28220ac
 Built:        Thu Sep  3 19:07:51 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.8.2-rc1
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   28220ac
 Built:        Thu Sep  3 19:07:51 UTC 2015
 OS/Arch:      linux/amd64

$ docker info   
Containers: 17
Images: 8
Storage Driver: devicemapper
 Pool Name: docker-254:0-1512633-pool
 Pool Blocksize: 65.54 kB
 Backing Filesystem: extfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 3.557 GB
 Data Space Total: 107.4 GB
 Data Space Available: 8.216 GB
 Metadata Space Used: 7.848 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.14 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.104 (2015-08-10)
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 4.1.0-1-amd64
Operating System: Debian GNU/Linux stretch/sid
CPUs: 8
Total Memory: 15.65 GiB
Name: n-pc
ID: LWBS:FFUU:5FGD:UXXH:J5FB:G4WL:WUZM:RFS4:3TEC:3IGG:K7GU:YWBV
WARNING: No memory limit support
WARNING: No swap limit support

$ uname -a
Linux n-pc 4.1.0-1-amd64 #1 SMP Debian 4.1.3-1 (2015-08-03) x86_64 GNU/Linux

I am using the API with command line, and when I want to get a container's stats, the API returns nothing:

$ echo -e "GET /containers/57d3/stats?stream=0 HTTP/1.1\r\n" | nc -U /var/run/docker.sock 
HTTP/1.1 200 OK
Content-Type: application/json
Server: Docker/1.8.2-rc1 (linux)
Date: Fri, 04 Sep 2015 08:13:10 GMT
Content-Length: 0

With the "docker" command I can get the stats, but I really need to use the API:

docker stats --no-stream 57d3
CONTAINER           CPU %               MEM USAGE/LIMIT     MEM %               NET I/O
57d3                 1.16%               0 B/16.81 GB        0.00%               8.565 MB/1.499 MB

However the API is working...

echo -e "GET /version HTTP/1.1\r\n" | nc -U /var/run/docker.sock | tail -1 | json
{
    "ApiVersion": "1.20",
    "Arch": "amd64",
    "BuildTime": "Thu Sep  3 19:07:51 UTC 2015",
    "GitCommit": "28220ac",
    "GoVersion": "go1.4.2",
    "KernelVersion": "4.1.0-1-amd64",
    "Os": "linux",
    "Version": "1.8.2-rc1"
}

What am I doing wrong please? Thank you!

Most helpful comment

I think this is an 'nc' issue. Try: ... nc -q -1 -U /var/run/docker.sock (note the "-q -1").

I believe 'nc' is closing the http request before the server does its job and as a result we stop the 'stats' processing. If you try other docker cmds that don't check the http request you should see that it works.

Gonna close this because its not really a docker issue as much as an nc issue. Can you use curl instead?

All 3 comments

This is likely happening because docker is sending the response header first...
Note that I cannot repro this with curl, only with piping to nc as you have here.
Also the CLI uses the same exact API.

I think this is an 'nc' issue. Try: ... nc -q -1 -U /var/run/docker.sock (note the "-q -1").

I believe 'nc' is closing the http request before the server does its job and as a result we stop the 'stats' processing. If you try other docker cmds that don't check the http request you should see that it works.

Gonna close this because its not really a docker issue as much as an nc issue. Can you use curl instead?

Thank you, it works! but i prefer to set -q to 5 (because -1 is for waiting forever) and 5 will set a timeout to 5 seconds.

I can use curl instead on Debian Stretch but not in Ubuntu 14.04 because: curl: option --unix-socket: is unknown

Was this page helpful?
0 / 5 - 0 ratings