The Docker API : Hands On

All right, now with the boring stuff out of the way, lets do some hands on.

As stated before Docker Remote API is a REST API that replaces the remote command line interface – rcli. For the purpose of this hands on, we have used cURL which is a command line tool that handles url manipulations. It helps make requests, get and send data, and retrieve information.

We will be covering basic operations so that even people who are new can work wit it. For a more detailed study, you can view the API documentation here.

##Setting up the lab Before we begin, we need to ensure that the docker daemon is running on http. So lets do that first.

###Command :

$ docker daemon -H http://127.0.0.1:2376

###Output :

WARN[0000] /!\ DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING /!\
INFO[0000] Listening for HTTP on tcp (127.0.0.1:2376)
ERRO[0000] WARNING: No --storage-opt dm.thinpooldev specified, using loopback; this configuration is strongly discouraged for production use
INFO[0000] [graphdriver] using prior storage driver "devicemapper"
INFO[0000] Option DefaultDriver: bridge
INFO[0000] Option DefaultNetwork: bridge
INFO[0000] Firewalld running: true
INFO[0000] Loading containers: start.
..ERRO[0000] Failed to load container 2d68faeb63b621f754b8961a45e9529d5527f31389a4b0c80ebc0b5ffa865c57: open /var/lib/docker/containers/2d68faeb63b621f754b8961a45e9529d5527f31389a4b0c80ebc0b5ffa865c57/config.json: no such file or directory
.ERRO[0000] Failed to load container 42c1b3eb75c173885cc64ff5452145830db9249cb5d45299823487b5b93cee95: open /var/lib/docker/containers/42c1b3eb75c173885cc64ff5452145830db9249cb5d45299823487b5b93cee95/config.json: no such file or directory
.......
INFO[0000] Loading containers: done.
INFO[0000] Daemon has completed initialization
INFO[0000] Docker daemon                                 commit=32b8b25/1.8.1 execdriver=native-0.2 graphdriver=devicemapper version=1.8.1.fc22'

For more information, read the docker configuration guide.

List existing images

Get the list of all images ($ docker images).

###Command :

 curl http://127.0.0.1:2376/images/json | jq .

###Output :

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   859  100   859    0     0  51237      0 --:--:-- --:--:-- --:--:-- 53687
[
  {
    "Labels": null,
    "VirtualSize": 2429728,
    "Size": 0,
    "Created": 1442598274,
    "RepoDigests": [],
    "RepoTags": [
      "docker.io/busybox:latest"
    ],
    "ParentId": "79722f6accc3345ac95c95dd5dd4af5563cd68a04691e728df4724fc8e8ad581",
    "Id": "0f864637f229eee9da53fd5591fb58138b6bfb0196f0ee4fd9417d3655fb3d28"
  },
  {
    "Labels": {},
    "VirtualSize": 283507110,
    "Size": 0,
    "Created": 1441839796,
    "RepoDigests": [],
    "RepoTags": [
      "docker.io/mysql:latest"
    ],
    "ParentId": "065018fec3d7c28754f0d40a3c1d56f103996a49f2995fde8c79ed1bd524a9d0",
    "Id": "6762f304c83428bf1945e9ab0aa05119a8a758d33d93eca50ba03665a89b5d97"
  },
  {
    "Labels": {
      "Vendor": "CentOS",
      "License": "GPLv2"
    },
    "VirtualSize": 172284372,
    "Size": 0,
    "Created": 1441652755,
    "RepoDigests": [],
    "RepoTags": [
      "docker.io/centos:latest"
    ],
    "ParentId": "f37e6a610a37349d4ad2ffe4ea163463787109c8ee5d1163c68777b619c5198f",
    "Id": "0f73ae75014f435e279d85ad31edc67e46c4a5d692b61840ff51e9d05f3b01ec"
  }
]

Create container from image

Create container from an existing image ($ docker run).

###Command :

curl -X POST http://127.0.0.1:2376/containers/create -H "Content-Type: application/json" -d '{
"Image": "docker.io/centos",
"Cmd": [
>         "/bin/bash"
> ]
> }'

###Output : {“Id”:”67ae096333b2a5c91fd89208adef35524db964f03e49ae87db0daa9935b38cf1”,”Warnings”:null}

Note : ID is dynamically assigned when the container is created. Please use the ID generated by your create command.

##Inspect a container

Get the data about a container you created ($ docker inspect).

###Command

curl -X GET  http://127.0.0.1:2376/containers/67ae096333b2a5c91fd89208adef35524db964f03e49ae87db0daa9935b38cf1/jso
n | jq .

###Output

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2283    0  2283    0     0   427k      0 --:--:-- --:--:-- --:--:--  557k
{
  "Config": {
    "Labels": null,
    "OnBuild": null,
    "MacAddress": "",
    "NetworkDisabled": false,
    "Entrypoint": [
      ""
    ],
    "WorkingDir": "",
    "PublishService": "",
    "ExposedPorts": null,
    "AttachStderr": false,
    "AttachStdout": false,
    "AttachStdin": false,
    "User": "",
    "Domainname": "",
    "Hostname": "67ae096333b2",
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": null,
    "Cmd": [
      "bash"
    ],
    "Image": "docker.io/centos",
    "Volumes": null,
    "VolumeDriver": ""
  },
  "Mounts": [],
  "GraphDriver": {
    "Data": {
      "DeviceSize": "107374182400",
      "DeviceName": "docker-8:1-787438-67ae096333b2a5c91fd89208adef35524db964f03e49ae87db0daa9935b38cf1",
      "DeviceId": "107"
    },
    "Name": "devicemapper"
  },
  "HostConfig": {
    "ConsoleSize": [
      0,
      0
    ],
    "CgroupParent": "",
    "LogConfig": {
      "Config": {},
      "Type": "json-file"
    },
    "Ulimits": null,
    "Links": null,
    "PortBindings": null,
    "Privileged": false,
    "MemorySwappiness": null,
    "OomKillDisable": false,
    "BlkioWeight": 0,
    "CpuQuota": 0,
    "CpusetMems": "",
    "Binds": null,
    "ContainerIDFile": "",
    "LxcConf": null,
    "Memory": 0,
    "MemorySwap": 0,
    "CpuShares": 0,
    "CpuPeriod": 0,
    "CpusetCpus": "",
    "PublishAllPorts": false,
    "Dns": null,
    "DnsSearch": null,
    "ExtraHosts": null,
    "VolumesFrom": null,
    "Devices": null,
    "NetworkMode": "",
    "IpcMode": "",
    "PidMode": "",
    "UTSMode": "",
    "CapAdd": null,
    "CapDrop": null,
    "GroupAdd": null,
    "RestartPolicy": {
      "MaximumRetryCount": 0,
      "Name": ""
    },
    "SecurityOpt": null,
    "ReadonlyRootfs": false
  },
  "ExecIDs": null,
  "AppArmorProfile": "",
  "ProcessLabel": "",
  "ResolvConfPath": "",
  "NetworkSettings": {
    "SecondaryIPv6Addresses": null,
    "SecondaryIPAddresses": null,
    "IPPrefixLen": 0,
    "IPAddress": "",
    "HairpinMode": false,
    "GlobalIPv6PrefixLen": 0,
    "GlobalIPv6Address": "",
    "Gateway": "",
    "EndpointID": "",
    "Bridge": "",
    "IPv6Gateway": "",
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "MacAddress": "",
    "NetworkID": "",
    "PortMapping": null,
    "Ports": null,
    "SandboxKey": ""
  },
  "Image": "0f73ae75014f435e279d85ad31edc67e46c4a5d692b61840ff51e9d05f3b01ec",
  "State": {
    "FinishedAt": "0001-01-01T00:00:00Z",
    "StartedAt": "0001-01-01T00:00:00Z",
    "Running": false,
    "Paused": false,
    "Restarting": false,
    "OOMKilled": false,
    "Dead": false,
    "Pid": 0,
    "ExitCode": 0,
    "Error": ""
  },
  "Args": [
    "bash"
  ],
  "Path": "",
  "Created": "2016-01-07T10:09:01.029470983Z",
  "Id": "67ae096333b2a5c91fd89208adef35524db964f03e49ae87db0daa9935b38cf1",
  "HostnamePath": "",
  "HostsPath": "",
  "LogPath": "",
  "Name": "/goofy_torvalds",
  "RestartCount": 0,
  "Driver": "devicemapper",
  "ExecDriver": "native-0.2",
  "MountLabel": ""
}