6

DevOps Shorts: The Docker REST API

 2 years ago
source link: https://blog.knoldus.com/devops-shorts-the-docker-rest-api/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

DevOps Shorts: The Docker REST API

Reading Time: 4 minutes

It’s 2021 people and Docker is practically everywhere in IT. Now, I know Docker comes with a lot of plumbing and it’s convenient using the docker cli for workflows. But, I don’t know if it’s just me, I like to explore things; Try to understand the ins and outs of them.

Now, if you have been using Docker for a while you then you know that it follows a client-server architecture. The Docker client talks to the Docker server process(also called the Docker daemon), and it takes care of a lot of heavy lifting behind the scenes using a lot of components(ergo the line, “a lot of plumbing”). If you are on a system with systemd, you can check up on this daemon using the following command:

$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2021-01-13 14:54:21 IST; 3 days ago
     Docs: https://docs.docker.com
 Main PID: 2165 (dockerd)
    Tasks: 18
   CGroup: /system.slice/docker.service
           └─2165 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.505185902+05:30" level=warning msg="Your kernel does not support CPU realtime scheduler"
Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.505190934+05:30" level=warning msg="Your kernel does not support cgroup blkio weight"
Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.505194908+05:30" level=warning msg="Your kernel does not support cgroup blkio weight_device"
Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.505320543+05:30" level=info msg="Loading containers: start."
Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.733891690+05:30" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set
Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.760584033+05:30" level=info msg="Loading containers: done."
Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.813572951+05:30" level=info msg="Docker daemon" commit=8891c58 graphdriver(s)=overlay2 version=20.10.2
Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.814482990+05:30" level=info msg="Daemon has completed initialization"
Jan 13 14:54:21 base systemd[1]: Started Docker Application Container Engine.
Jan 13 14:54:21 base dockerd[2165]: time="2021-01-13T14:54:21.827736468+05:30" level=info msg="API listen on /var/run/docker.sock"

If you read the output of the aforementioned command a little closely, there’s a unix domain socket in use behind the scenes and the API listens on /var/run/docker.sock by default. The docker cli communicates with the API server using this unix domain socket by sending HTTP commands. So, if the Docker cli is too vanilla for you, you can communicate with the dockerd using cURL and in this blog we are going to do just that.

Note: Since the output comes back to us in JSON, I think it’d be a great idea to install jq first. I use an ubuntu machine and you can install jq on it using the command:

sudo apt install jq

Experiment #1: Get Version

Using the docker cli

$ docker version --format '{{json .}}' | jq
{
  "Client": {
    "Platform": {
      "Name": "Docker Engine - Community"
    },
    "Version": "20.10.2",
    "ApiVersion": "1.41",
    "DefaultAPIVersion": "1.41",
    "GitCommit": "2291f61",
    "GoVersion": "go1.13.15",
    "Os": "linux",
    "Arch": "amd64",
    "BuildTime": "Mon Dec 28 16:17:32 2020",
    "Context": "default",
    "Experimental": true
  },
  "Server": {
    "Platform": {
      "Name": "Docker Engine - Community"
    },
    "Components": [
      {
        "Name": "Engine",
        "Version": "20.10.2",
        "Details": {
          "ApiVersion": "1.41",
          "Arch": "amd64",
          "BuildTime": "Mon Dec 28 16:15:09 2020",
          "Experimental": "false",
          "GitCommit": "8891c58",
          "GoVersion": "go1.13.15",
          "KernelVersion": "5.4.0-60-generic",
          "MinAPIVersion": "1.12",
          "Os": "linux"
        }
      },
      {
        "Name": "containerd",
        "Version": "1.4.3",
        "Details": {
          "GitCommit": "269548fa27e0089a8b8278fc4fc781d7f65a939b"
        }
      },
      {
        "Name": "runc",
        "Version": "1.0.0-rc92",
        "Details": {
          "GitCommit": "ff819c7e9184c13b7c2607fe6c30ae19403a7aff"
        }
      },
      {
        "Name": "docker-init",
        "Version": "0.19.0",
        "Details": {
          "GitCommit": "de40ad0"
        }
      }
    ],
    "Version": "20.10.2",
    "ApiVersion": "1.41",
    "MinAPIVersion": "1.12",
    "GitCommit": "8891c58",
    "GoVersion": "go1.13.15",
    "Os": "linux",
    "Arch": "amd64",
    "KernelVersion": "5.4.0-60-generic",
    "BuildTime": "2020-12-28T16:15:09.000000000+00:00"
  }
}

Using cURL

$ curl -s -XGET --unix-socket /var/run/docker.sock http://localhost/version | jq 
{
  "Platform": {
    "Name": "Docker Engine - Community"
  },
  "Components": [
    {
      "Name": "Engine",
      "Version": "20.10.2",
      "Details": {
        "ApiVersion": "1.41",
        "Arch": "amd64",
        "BuildTime": "2020-12-28T16:15:09.000000000+00:00",
        "Experimental": "false",
        "GitCommit": "8891c58",
        "GoVersion": "go1.13.15",
        "KernelVersion": "5.4.0-60-generic",
        "MinAPIVersion": "1.12",
        "Os": "linux"
      }
    },
    {
      "Name": "containerd",
      "Version": "1.4.3",
      "Details": {
        "GitCommit": "269548fa27e0089a8b8278fc4fc781d7f65a939b"
      }
    },
    {
      "Name": "runc",
      "Version": "1.0.0-rc92",
      "Details": {
        "GitCommit": "ff819c7e9184c13b7c2607fe6c30ae19403a7aff"
      }
    },
    {
      "Name": "docker-init",
      "Version": "0.19.0",
      "Details": {
        "GitCommit": "de40ad0"
      }
    }
  ],
  "Version": "20.10.2",
  "ApiVersion": "1.41",
  "MinAPIVersion": "1.12",
  "GitCommit": "8891c58",
  "GoVersion": "go1.13.15",
  "Os": "linux",
  "Arch": "amd64",
  "KernelVersion": "5.4.0-60-generic",
  "BuildTime": "2020-12-28T16:15:09.000000000+00:00"
}

Experiment #2: Ping the Docker Server

$ curl -s -XGET --unix-socket /var/run/docker.sock http://localhost/_ping
OK

Experiment #3: Create a Docker Volume

using the docker cli

$ docker volume ls
DRIVER    VOLUME NAME

$ docker volume create cassandra
cassandra

$ docker volume ls
DRIVER    VOLUME NAME
local     cassandra

using cURL

$ docker volume ls
DRIVER    VOLUME NAME
local     cassandra

$  curl -s -XPOST --unix-socket /var/run/docker.sock -H 'CONTENT-TYPE: application/json' -d '{
  "Name": "cassandra",
  "Driver": "local"
}'  http://localhost/volumes/create | jq
{
  "CreatedAt": "2021-01-17T04:31:45+05:30",
  "Driver": "local",
  "Labels": null,
  "Mountpoint": "/var/lib/docker/volumes/cassandra/_data",
  "Name": "cassandra",
  "Options": null,
  "Scope": "local"
}

$ docker volume ls
DRIVER    VOLUME NAME
local     cassandra

Experiment #4: List Volumes

using the docker cli

$ docker volume ls
DRIVER    VOLUME NAME
local     cassandra

using cURL

$ curl -s -XGET --unix-socket /var/run/docker.sock -H 'CONTENT-TYPE: application/json' http://localhost/volumes | jq
{
  "Volumes": [
    {
      "CreatedAt": "2021-01-17T04:31:45+05:30",
      "Driver": "local",
      "Labels": null,
      "Mountpoint": "/var/lib/docker/volumes/cassandra/_data",
      "Name": "cassandra",
      "Options": null,
      "Scope": "local"
    }
  ],
  "Warnings": null
}

Now, I know this is something that you probably won’t be using as your daily driver. But it’s little things like this one that keeps our weekends happy.

I hope this short article brought some happiness in your life. Please tell us in the comments section how do you use the plumbing that comes as part of Docker

Until next time! 🍺

Knoldus-blog-footer-image

Related


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK