🐳 Как опросить сокет Docker с помощью curl

Мануал

Запрос к сокету Docker с помощью curl.

Отображение версии Docker.

$ 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"
}

Отображение сведений о Docker.

$ 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
}

Следите за текущими событиями:

$ 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}
[...]

Как создать контейнер:

$ 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":[]}

Как запустить контейнер

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

Список контейнеров:

$ 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 <docker-maint@nginx.com>"
    },
    "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": []
  }
]</docker-maint@nginx.com>

Отображение сведений о контейнере.

$ 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": "",

См. также:

 

Добавить комментарий