Last Updated: January 06, 2019
·
1.046K
· skyzyx

Querying the Docker Engine API for any running container

Find the right version of the Docker Engine API for your Docker release from https://docs.docker.com/develop/sdk/#api-version-matrix. In these examples, I was testing against API v1.37 and connecting over a Unix socket.

Get the running process list.

curl -sSL --unix-socket /var/run/docker.sock "http:/v1.37/containers/{id}/top" | jq

Get the list of running containers, filter for an image name, then get the network bridge IP address.

curl -sSL \
    --unix-socket /var/run/docker.sock \
    "http:/v1.37/containers/json" \
    | jq -r '
        .[] | select(.Image == "datadog/agent") | .NetworkSettings.Networks.bridge.IPAddress
    ' \
;

Get the list of running containers, filter for an image name, then get the list of public ports that are being exposed from the container.

curl -sSL \
    --unix-socket /var/run/docker.sock \
    "http:/v1.37/containers/json" \
    | jq -r '
        .[] | select(.Image == "datadog/agent") | .Ports[] | select(.PublicPort) | .PublicPort
    ' \
;

Get the list of running containers, filter for an image name, then get the network bridge IP address + public ports that are opened on the container.

curl -sSL \
    --unix-socket /var/run/docker.sock \
    "http:/v1.37/containers/json" \
    | jq -r '
        .[] | select(.Image == "datadog/agent") | [
            .NetworkSettings.Networks.bridge.IPAddress,
            (.Ports[] | select(.PublicPort) | .PublicPort) | tostring
        ] | join(":")
    ' \
;