Performing Container Operations Using Apis

In the previous recipe, we launched a container and executed a command inside the container using the /create , /attach , and /start API. In this recipe, we perform a few more operations on the container.

How to do it…

In this recipe, we’ll look at a few container operations:

  1. To list containers, use the following API:

Diagrama

Here are a few examples:

  • Here is how to get all the running containers:
$ curl --unix-socket /var/run/docker.sock \
           http:/containers/json
  • Here is how to get all the running containers, including the stopped ones:
$ curl --unix-socket /var/run/docker.sock \
           http:/containers/json?all=1
  1. To inspect a container, use the following API:

Diagrama

Here is an example of inspecting a container 591ab8ac2650 :

$ curl --unix-socket /var/run/docker.sock \
         http:/containers/591ab8ac2650/json
  1. To get the list of processes running inside a container, use the following API:

Diagrama

Here is an example of listing the process running inside the 591ab8ac2650 container:

$ curl --unix-socket /var/run/docker.sock \
         http:/containers/591ab8ac2650/top
  1. To get the resource usage statistics of a container, use the following API:

Diagrama

Here is an example of getting the resource usage statistics for the 591ab8ac2650 container:

$ curl --unix-socket /var/run/docker.sock \
         http:/containers/591ab8ac2650/stats

By default, this API streams the resource usage statistics. However, you can disable streaming by using the stream parameter, as shown here:

$ curl --unix-socket /var/run/docker.sock \
         http:/containers/591ab8ac2650/stats?stream=0

How it works…

When we connect with the Docker engine using the APIs described in this recipe, the Docker engine would in turn gather the relevant information from its data source and send it back to the client.

See also

Each API endpoint can have different inputs to control operations. For more details, visit the documentation on the Docker website at https://docs.docker.com/engine/api/latest/.