Categories
DevOps

How to query Docker socket using curl

Query Docker socket using curl.

Display Docker version.

$ curl --silent -XGET --unix-socket /run/docker.sock http://localhost/version | jq .
{                                                                               
  "Platform": {
    "Name": "Docker Engine - Community"
  },
  "Components": [
    {
      "Name": "Engine",
      "Version": "20.10.7",
      "Details": {
        "ApiVersion": "1.41",
        "Arch": "amd64",
        "BuildTime": "2021-06-02T11:55:12.000000000+00:00",
        "Experimental": "false",
        "GitCommit": "b0f5bc3",
        "GoVersion": "go1.13.15",
        "KernelVersion": "5.10.0-7-amd64",
        "MinAPIVersion": "1.12",
        "Os": "linux"
      }
    },
    {
      "Name": "containerd",
      "Version": "1.4.6",
      "Details": {
        "GitCommit": "d71fcd7d8303cbf684402823e425e9dd2e99285d"
      }
    },
    {
      "Name": "runc",
      "Version": "1.0.0-rc95",
      "Details": {
        "GitCommit": "b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7"
      }
    },
    {
      "Name": "docker-init",
      "Version": "0.19.0",
      "Details": {
        "GitCommit": "de40ad0"
      }
    }
  ],
  "Version": "20.10.7",
  "ApiVersion": "1.41",
  "MinAPIVersion": "1.12",
  "GitCommit": "b0f5bc3",
  "GoVersion": "go1.13.15",
  "Os": "linux",
  "Arch": "amd64",
  "KernelVersion": "5.10.0-7-amd64",
  "BuildTime": "2021-06-02T11:55:12.000000000+00:00"
}

Display Docker details.

$ curl --silent -XGET --unix-socket /run/docker.sock http://localhost/info | jq .
{                  
  "ID": "CPMB:VYBU:UIFT:KG3E:O74F:KY5G:TRJY:R6WH:YMT2:FU6M:RHKU:MZQS",
  "Containers": 8,    
  "ContainersRunning": 0,
  "ContainersPaused": 0,     
  "ContainersStopped": 8,    
  "Images": 2,         
  "Driver": "overlay2",       
  "DriverStatus": [ 
    [            
      "Backing Filesystem",            
      "extfs"            
    ],                     
    [                       
      "Supports d_type",
      "true"
    ],                    
    [                      
      "Native Overlay Diff",                                                    
      "true"                   
    ],                      
    [                            
      "userxattr",            
      "false"                       
    ]                                                                           
  ],                
  "Plugins": {                
    "Volume": [            
      "local"                                                                   
    ],                 
    "Network": [                                                                
      "bridge",                                                                 
      "host",                 
      "ipvlan",    
      "macvlan",                                                                
      "null",                                                                   
      "overlay"     
    ],                      
    "Authorization": null,
    "Log": [
      "awslogs",        
      "fluentd",      
      "gcplogs",    
      "gelf",                      
      "journald",  
      "json-file",
      "local",           
      "logentries",        
      "splunk",                      
      "syslog"
    ]
  },
  "MemoryLimit": true,
  "SwapLimit": true,
  "KernelMemory": false,
  "KernelMemoryTCP": false,
  "CpuCfsPeriod": true,
  "CpuCfsQuota": true,
  "CPUShares": true,
  "CPUSet": true,
  "PidsLimit": true,
  "IPv4Forwarding": true,
  "BridgeNfIptables": true,
  "BridgeNfIp6tables": true,
  "Debug": false,
  "NFd": 22,
  "OomKillDisable": false,
  "NGoroutines": 34,
  "SystemTime": "2021-07-11T21:35:42.722660756Z",
  "LoggingDriver": "json-file",
  "CgroupDriver": "systemd",
  "CgroupVersion": "2",
  "NEventsListener": 0,
  "KernelVersion": "5.10.0-7-amd64",
  "OperatingSystem": "Debian GNU/Linux 11 (bullseye)",
  "OSVersion": "11",
  "OSType": "linux",
  "Architecture": "x86_64",
  "IndexServerAddress": "https://index.docker.io/v1/",
  "RegistryConfig": {
    "AllowNondistributableArtifactsCIDRs": [],
    "AllowNondistributableArtifactsHostnames": [],
    "InsecureRegistryCIDRs": [
      "127.0.0.0/8"
    ],
    "IndexConfigs": {
      "docker.io": {
        "Name": "docker.io",
        "Mirrors": [],
        "Secure": true,
        "Official": true
      }
    },
    "Mirrors": []
  },
  "NCPU": 32,
  "MemTotal": 1015853056,
  "GenericResources": null,
  "DockerRootDir": "/var/lib/docker",
  "HttpProxy": "",
  "HttpsProxy": "",
  "NoProxy": "",
  "Name": "bullseye",
  "Labels": [],
  "ExperimentalBuild": false,
  "ServerVersion": "20.10.7",
  "Runtimes": {
    "io.containerd.runc.v2": {
      "path": "runc"
    },
    "io.containerd.runtime.v1.linux": {
      "path": "runc"
    },
    "runc": {
      "path": "runc"
    }
  },
  "DefaultRuntime": "runc",
  "Swarm": {
    "NodeID": "",
    "NodeAddr": "",
    "LocalNodeState": "inactive",
    "ControlAvailable": false,
    "Error": "",
    "RemoteManagers": null
  },
  "LiveRestoreEnabled": false,
  "Isolation": "",
  "InitBinary": "docker-init",
  "ContainerdCommit": {
    "ID": "d71fcd7d8303cbf684402823e425e9dd2e99285d",
    "Expected": "d71fcd7d8303cbf684402823e425e9dd2e99285d"
  },
  "RuncCommit": {
    "ID": "b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7",
    "Expected": "b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7"
  },
  "InitCommit": {
    "ID": "de40ad0",
    "Expected": "de40ad0"
  },
  "SecurityOptions": [
    "name=apparmor",
    "name=seccomp,profile=default",
    "name=cgroupns"
  ],
  "Warnings": null
}

Monitor current events.

$ curl --silent -XGET --unix-socket /run/docker.sock http://localhost/events
{"status":"create","id":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","from":"hello-world","Type":"container","Action":"create","Actor":{"ID":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","Attributes":{"image":"hello-world","name":"sleepy_bose"}},"scope":"local","time":1626039182,"timeNano":1626039182461367134}
{"status":"attach","id":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","from":"hello-world","Type":"container","Action":"attach","Actor":{"ID":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","Attributes":{"image":"hello-world","name":"sleepy_bose"}},"scope":"local","time":1626039182,"timeNano":1626039182469106567}
{"Type":"network","Action":"connect","Actor":{"ID":"080e4494dc690e9649bd891f224e8bc42e915633a4f55dbdd90a741ae32e8636","Attributes":{"container":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","name":"bridge","type":"bridge"}},"scope":"local","time":1626039182,"timeNano":1626039182501956621}
{"status":"start","id":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","from":"hello-world","Type":"container","Action":"start","Actor":{"ID":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","Attributes":{"image":"hello-world","name":"sleepy_bose"}},"scope":"local","time":1626039182,"timeNano":1626039182953160024}
{"status":"die","id":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","from":"hello-world","Type":"container","Action":"die","Actor":{"ID":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","Attributes":{"exitCode":"0","image":"hello-world","name":"sleepy_bose"}},"scope":"local","time":1626039182,"timeNano":1626039182989414581}
{"Type":"network","Action":"disconnect","Actor":{"ID":"080e4494dc690e9649bd891f224e8bc42e915633a4f55dbdd90a741ae32e8636","Attributes":{"container":"e5291372d0f3f530b4b67b89dc6b6e1ef511dd4bfda57f9331a5f3db34ad0917","name":"bridge","type":"bridge"}},"scope":"local","time":1626039183,"timeNano":1626039183097029362}
[...]

Create a container.

$ curl --silent -XPOST --unix-socket /run/docker.sock -d '{"Image":"nginx:latest"}' -H 'Content-Type: application/json' http://localhost/containers/create?name=nginx
{"Id":"1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e","Warnings":[]}

Start a container.

$ curl --silent -XPOST --unix-socket /run/docker.sock -H 'Content-Type: application/json' http://localhost/containers/1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e/start

List containers.

$ curl --silent -XGET --unix-socket /run/docker.sock -H 'Content-Type: application/json' http://localhost/containers/json | jq .
[
  {
    "Id": "1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e",
    "Names": [
      "/nginx"
    ],
    "Image": "nginx:latest",
    "ImageID": "sha256:4cdc5dd7eaadff5080649e8d0014f2f8d36d4ddf2eff2fdf577dd13da85c5d2f",
    "Command": "/docker-entrypoint.sh nginx -g 'daemon off;'",
    "Created": 1626039550,
    "Ports": [
      {
        "PrivatePort": 80,
        "Type": "tcp"
      }
    ],
    "Labels": {
      "maintainer": "NGINX Docker Maintainers "
    },
    "State": "running",
    "Status": "Up 19 seconds",
    "HostConfig": {
      "NetworkMode": "default"
    },
    "NetworkSettings": {
      "Networks": {
        "bridge": {
          "IPAMConfig": null,
          "Links": null,
          "Aliases": null,
          "NetworkID": "080e4494dc690e9649bd891f224e8bc42e915633a4f55dbdd90a741ae32e8636",
          "EndpointID": "c5687cea3ffe61ebef63eeb8790bfe7d8660d37d80bcb45ca5925b02078dc8be",
          "Gateway": "172.17.0.1",
          "IPAddress": "172.17.0.2",
          "IPPrefixLen": 16,
          "IPv6Gateway": "",
          "GlobalIPv6Address": "",
          "GlobalIPv6PrefixLen": 0,
          "MacAddress": "02:42:ac:11:00:02",
          "DriverOpts": null
        }
      }
    },
    "Mounts": []
  }
]

Display container details.

$ curl --silent -XGET --unix-socket /run/docker.sock -H 'Content-Type: application/json' http://localhost/containers/1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e/json | jq .
{                      
  "Id": "1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e",
  "Created": "2021-07-11T21:39:10.89496568Z",
  "Path": "/docker-entrypoint.sh",
  "Args": [                                                                                                                                                     
    "nginx",                                                                                                                                                    
    "-g",                       
    "daemon off;"                                                               
  ],                         
  "State": {              
    "Status": "running",        
    "Running": true,             
    "Paused": false,                                                            
    "Restarting": false,  
    "OOMKilled": false,     
    "Dead": false,       
    "Pid": 2533,             
    "ExitCode": 0,          
    "Error": "",        
    "StartedAt": "2021-07-11T21:42:33.900117747Z",
    "FinishedAt": "0001-01-01T00:00:00Z"
  },
  "Image": "sha256:4cdc5dd7eaadff5080649e8d0014f2f8d36d4ddf2eff2fdf577dd13da85c5d2f",
  "ResolvConfPath": "/var/lib/docker/containers/1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e/resolv.conf",
  "HostnamePath": "/var/lib/docker/containers/1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e/hostname",
  "HostsPath": "/var/lib/docker/containers/1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e/hosts",
  "LogPath": "/var/lib/docker/containers/1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f76537228b0e/1c022ba8c88382b1bad03c5d32146f66b698b5211ea37057cb56f7
6537228b0e-json.log",
  "Name": "/nginx",
  "RestartCount": 0,
  "Driver": "overlay2",
  "Platform": "linux",
  "MountLabel": "",
  "ProcessLabel": "",
  "AppArmorProfile": "docker-default",
  "ExecIDs": null,
  "HostConfig": {
    "Binds": null,
    "ContainerIDFile": "",
    "LogConfig": {
      "Type": "json-file",
      "Config": {}
    },
    "NetworkMode": "default",
    "PortBindings": null,
    "RestartPolicy": {
      "Name": "",
      "MaximumRetryCount": 0
    },
    "AutoRemove": false,
    "VolumeDriver": "",
    "VolumesFrom": null,
    "CapAdd": null,
    "CapDrop": null,
    "CgroupnsMode": "private",
    "Dns": null,
    "DnsOptions": null,
    "DnsSearch": null,
    "ExtraHosts": null,
    "GroupAdd": null,
    "IpcMode": "private",
    "Cgroup": "",
    "Links": null,
    "OomScoreAdj": 0,
    "PidMode": "",
    "Privileged": false,
    "PublishAllPorts": false,
    "ReadonlyRootfs": false,
    "SecurityOpt": null,
    "UTSMode": "",
    "UsernsMode": "",
    "ShmSize": 67108864,
    "Runtime": "runc",
    "ConsoleSize": [
      0,
      0
    ],
    "Isolation": "",
    "CpuShares": 0,
    "Memory": 0,
    "NanoCpus": 0,
    "CgroupParent": "",
    "BlkioWeight": 0,
    "BlkioWeightDevice": null,
    "BlkioDeviceReadBps": null,
    "BlkioDeviceWriteBps": null,
    "BlkioDeviceReadIOps": null,
    "BlkioDeviceWriteIOps": null,
    "CpuPeriod": 0,
    "CpuQuota": 0,
    "CpuRealtimePeriod": 0,
    "CpuRealtimeRuntime": 0,
    "CpusetCpus": "",
    "CpusetMems": "",
    "Devices": null,
    "DeviceCgroupRules": null,
    "DeviceRequests": null,
    "KernelMemory": 0,
    "KernelMemoryTCP": 0,
    "MemoryReservation": 0,
    "MemorySwap": 0,
    "MemorySwappiness": null,
    "OomKillDisable": null,
    "PidsLimit": null,
    "Ulimits": null,
    "CpuCount": 0,
    "CpuPercent": 0,
    "IOMaximumIOps": 0,
    "IOMaximumBandwidth": 0,
    "MaskedPaths": [
      "/proc/asound",
      "/proc/acpi",
      "/proc/kcore",
      "/proc/keys",
      "/proc/latency_stats",
      "/proc/timer_list",
      "/proc/timer_stats",
      "/proc/sched_debug",
      "/proc/scsi",
      "/sys/firmware"
    ],
    "ReadonlyPaths": [
      "/proc/bus",
      "/proc/fs",
      "/proc/irq",
      "/proc/sys",
      "/proc/sysrq-trigger"
    ]
  },
  "GraphDriver": {
    "Data": {
      "LowerDir": "/var/lib/docker/overlay2/809ab5977bdd40ba6141faaa7bca71c8dd3044e4822a1d70faf1854736ce4efe-init/diff:/var/lib/docker/overlay2/8ea0fc74b8b7463c
33b8543224b577e9b18bb8819da8d30dccbdb710ecfae8ad/diff:/var/lib/docker/overlay2/3ee187260b539c40e08be6e0d8c53818c3359ba98b23e649c75ed557772d3dc3/diff:/var/lib/do
cker/overlay2/3b06d3cd101c53b70a4bffab1e630fdbee5a9ed2924ca9bf9bc934788a4daf55/diff:/var/lib/docker/overlay2/86163c3b8184b5c1c15faa95676fb5a64e918e47e4d21ba6b60
6852486b915ca/diff:/var/lib/docker/overlay2/80309168b2374ac3f2340312646df11f5c5be4f784dad24659c094c8396d7f0a/diff:/var/lib/docker/overlay2/d9adadc69b11e6f1fc2f3
938a9b88b7250bcb1b58c8185d727fa8d52a8b739fd/diff",
      "MergedDir": "/var/lib/docker/overlay2/809ab5977bdd40ba6141faaa7bca71c8dd3044e4822a1d70faf1854736ce4efe/merged",
      "UpperDir": "/var/lib/docker/overlay2/809ab5977bdd40ba6141faaa7bca71c8dd3044e4822a1d70faf1854736ce4efe/diff",
      "WorkDir": "/var/lib/docker/overlay2/809ab5977bdd40ba6141faaa7bca71c8dd3044e4822a1d70faf1854736ce4efe/work"
    },
    "Name": "overlay2"
  },
  "Mounts": [],
  "Config": {
    "Hostname": "1c022ba8c883",
    "Domainname": "",
    "User": "",
    "AttachStdin": false,
    "AttachStdout": false,
    "AttachStderr": false,
    "ExposedPorts": {
      "80/tcp": {}
    },
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": [
      "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
      "NGINX_VERSION=1.21.1",
      "NJS_VERSION=0.6.1",
      "PKG_RELEASE=1~buster"
    ],
    "Cmd": [
      "nginx",
      "-g",
      "daemon off;"
    ],
    "Image": "nginx:latest",
    "Volumes": null,
    "WorkingDir": "",
    "Entrypoint": [
      "/docker-entrypoint.sh"
    ],
    "OnBuild": null,
    "Labels": {
      "maintainer": "NGINX Docker Maintainers "
    },
    "StopSignal": "SIGQUIT"
  },
  "NetworkSettings": {
    "Bridge": "",
    "SandboxID": "9f51380f91fff1155cccdfa2c731c629fd9a1bc2514f3d1e47555f3004db2191",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {
      "80/tcp": null
    },
    "SandboxKey": "/var/run/docker/netns/9f51380f91ff",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "c5687cea3ffe61ebef63eeb8790bfe7d8660d37d80bcb45ca5925b02078dc8be",
    "Gateway": "172.17.0.1",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "172.17.0.2",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "MacAddress": "02:42:ac:11:00:02",
    "Networks": {
      "bridge": {
        "IPAMConfig": null,
        "Links": null,
        "Aliases": null,
        "NetworkID": "080e4494dc690e9649bd891f224e8bc42e915633a4f55dbdd90a741ae32e8636",
        "EndpointID": "c5687cea3ffe61ebef63eeb8790bfe7d8660d37d80bcb45ca5925b02078dc8be",
        "Gateway": "172.17.0.1",
        "IPAddress": "172.17.0.2",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "02:42:ac:11:00:02",
        "DriverOpts": null
      }
    }
  }
}

There are many more endpoints worth mentioning, so I will point you to the API documentation directly: Develop with Docker Engine API and Docker Engine API (v1.41).