top of page
Writer's pictureSiddhesh Kadam

Monitoring Docker containers through the Docker API



Monitoring Docker Through API

containers through the Docker API allows you to retrieve various metrics and information about your containers programmatically. The Docker API provides a RESTful interface that you can use to query and manage Docker resources, including containers.


Here are the key steps to monitor Docker containers using the Docker API:


1.Enable the Docker Remote API


By default, Docker listens on a Unix socket. To enable the Docker Remote API, you need to modify the Docker daemon configuration. This typically involves editing the Docker daemon configuration file, which is commonly located at /etc/docker/daemon.json. Add or modify the following lines:

[root@siddhesh ~]# cat /etc/docker/daemon.json
{
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:8888"]
}
[root@siddhesh ~]#

This configuration allows Docker to listen on both the Unix socket and a TCP socket (in this case, on port 8888). Be cautious when exposing the Docker API over the network, as it can pose security risks. Consider using TLS for secure communication.


Restart the Docker Service.

[root@siddhesh ~]# systemctl restart docker

2. Retrieve Container Information


You can use tools like curl or programming languages with HTTP client libraries to interact with the Docker API. For example, to retrieve a list of containers, you can use:

[root@siddhesh ~]# curl -s http://localhost:8888/containers/json

[{"Id":"6ba73e0ddfc506194ee68811cd6aaa4807b4f2f268b8c307e96be9f59ef24e3f","Names":["/pensive_lovelace"],"Image":"nginx","ImageID":"sha256:c20060033e06f882b0fbe2db7d974d72e0887a3be5e554efdb0dcf8d53512647","Command":"/docker-entrypoint.sh nginx -g 'daemon off;'","Created":1702378945,"Ports":[{"IP":"0.0.0.0","PrivatePort":80,"PublicPort":443,"Type":"tcp"}],"Labels":{"maintainer":"NGINX Docker Maintainers <docker-maint@nginx.com>"},"State":"running","Status":"Up About a minute","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"5a5749aeacb8de26da13b6956d0d0ae8f4b4976084fc600404950b23ba6c7d2b","EndpointID":"e2f47af0b9e7b6e8637b7742b116221342e8ba21b9dce80499e02eba9500dadb","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}}},"Mounts":[]}]
[root@siddhesh ~]#


3. Monitor Container Stats


The Docker API provides an endpoint to retrieve real-time statistics for a running container. For example.

[root@siddhesh ~]# curl -s http://localhost:8888/containers/6ba73e0ddfc506194ee68811cd6aaa4807b4f2f268b8c307e96be9f59ef24e3f/stats?stream=0

{"read":"2023-12-12T11:08:39.343106576Z","preread":"2023-12-12T11:08:38.342878338Z","pids_stats":{"current":5},"blkio_stats":{"io_service_bytes_recursive":[],"io_serviced_recursive":[],"io_queue_recursive":[],"io_service_time_recursive":[],"io_wait_time_recursive":[],"io_merged_recursive":[],"io_time_recursive":[],"sectors_recursive":[]},"num_procs":0,"storage_stats":{},"cpu_stats":{"cpu_usage":{"total_usage":45249673,"percpu_usage":[7680030,18485702,17323293,1760648],"usage_in_kernelmode":0,"usage_in_usermode":10000000},"system_cpu_usage":15145587090000000,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"precpu_stats":{"cpu_usage":{"total_usage":45249673,"percpu_usage":[7680030,18485702,17323293,1760648],"usage_in_kernelmode":0,"usage_in_usermode":10000000},"system_cpu_usage":15145583140000000,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"memory_stats":{"usage":3678208,"max_usage":3796992,"stats":{"active_anon":3653632,"active_file":0,"cache":24576,"hierarchical_memory_limit":9223372036854771712,"hierarchical_memsw_limit":9223372036854771712,"inactive_anon":4096,"inactive_file":20480,"mapped_file":4096,"pgfault":3771,"pgmajfault":0,"pgpgin":1370,"pgpgout":472,"rss":3653632,"rss_huge":0,"swap":0,"total_active_anon":3653632,"total_active_file":0,"total_cache":24576,"total_inactive_anon":4096,"total_inactive_file":20480,"total_mapped_file":4096,"total_pgfault":0,"total_pgmajfault":0,"total_pgpgin":0,"total_pgpgout":0,"total_rss":3653632,"total_rss_huge":0,"total_swap":0,"total_unevictable":0,"unevictable":0},"limit":16461549568},"name":"/pensive_lovelace","id":"6ba73e0ddfc506194ee68811cd6aaa4807b4f2f268b8c307e96be9f59ef24e3f","networks":{"eth0":{"rx_bytes":656,"rx_packets":8,"rx_errors":0,"rx_dropped":0,"tx_bytes":656,"tx_packets":8,"tx_errors":0,"tx_dropped":0}}}
[root@siddhesh ~]#

This command fetches the container stats.


Below are some key endpoints of the Docker Remote API.


Containers:

List Containers: GET /containers

Create Container: POST /containers/create

Container Details: GET /containers/{id}/json

Start Container: POST /containers/{id}/start

Stop Container: POST /containers/{id}/stop

Restart Container: POST /containers/{id}/restart

Kill Container: POST /containers/{id}/kill

Remove Container: DELETE /containers/{id}

Images:

List Images: GET /images

Build Image: POST /build

Image Details: GET /images/{name}/json

Push Image: POST /images/{name}/push

Remove Image: DELETE /images/{name}

Volumes:

List Volumes: GET /volumes

Create Volume: POST /volumes/create

Volume Details: GET /volumes/{name}

Remove Volume: DELETE /volumes/{name}

Networks:

List Networks: GET /networks

Create Network: POST /networks/create

Network Details: GET /networks/{id}

Remove Network: DELETE /networks/{id}

Info and System:

Docker System Info: GET /info

Docker Version: GET /version

Events:

Monitor Docker Events: GET /events

Exec:

Create Exec Instance: POST /containers/{id}/exec

Start Exec Instance: POST /exec/{id}/start

Miscellaneous:

Docker Ping: GET /_ping

Build Contexts: GET /build

Distribution Information: GET /distribution


Коментарі

Оцінка: 0 з 5 зірок.
Ще немає оцінок

Додайте оцінку
bottom of page