PHP Classes

PHP Docker API: Interact with Docker instances with the Engine API

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 57 All time: 10,556 This week: 60Up
Version License PHP version Categories
docker-engine-api-po 1.0.0Custom (specified...5Networking, PHP 5, Web services, Hosting
Description 

Author

This package can interact with Docker instances with the Engine API.

It provides an Docker server class that can send HTTP requests to Docker instances with given host to perform several types of operations. Currently it can:

- Get the list of containers running in a host
- Get the details and statistics for a container
- Get the images list and their details
- Get the networks list and their details
- Get the volumes list and their details

Innovation Award
PHP Programming Innovation award nominee
November 2020
Number 6


Prize: 1 Year Subscription to NomadPHP Advanced PHP Learning
Docker is a well known tool to manage containers on which developers can deploy their applications in the development or test environments in a way that it is easy to push to the production environments, so there is not much difference between the different environments.

This package provides a PHP solution that allows querying Docker to obtain details of the state of the instances that may be running on a given machine.

It makes it easy to monitor applications running as Docker instances, so developers can check the status of their applications.

Manuel Lemos
Picture of Angel Campos
Name: Angel Campos <contact>
Classes: 8 packages by
Country: Spain Spain
Age: ???
All time rank: 377599 in Spain Spain
Week rank: 164 Up5 in Spain Spain Up
Innovation award
Innovation award
Nominee: 4x

Documentation

Docker Poller for Laravel

This package allows laravel applications to interact with the Docker Engine API.

The Docker Engine API is an HTTP API served by Docker Engine. It is the API the Docker client uses to communicate with the Engine, so everything the Docker client can do can be done with the API.

Installation

You can install the package via composer and then publish the assets:


composer require acamposm/docker-poller

php artisan vendor:publish --provider="Acamposm\DockerEngineApiPoller\DockerPollerServiceProvider"

*Note:* We try to follow SemVer v2.0.0.

Requirements

To use this packet, you must enable first the Docker Engine API, normally the Engine API listens on port 2375, but it is configurable.

*Note:* In production environments, you must always secure the API with SSL encryption and control who can perform request to this API.

Usage

Basic Initialization

First, create a DockerServer instance with the details of the docker server hosts.

*Note:* By default, the DockerServer class, has as a default parameters the default port of Docker Engine API (2375) and the protocol (http).

Docker Engine API over HTTP

use Acamposm\DockerEngineApiPoller\DockerServer;

$server = (new DockerServer())->server('localhost');

or

use Acamposm\DockerEngineApiPoller\DockerServer;

$server = (new DockerServer())->insecure()->port(12375)->server('localhost');

Docker Engine API over HTTPS

use Acamposm\DockerEngineApiPoller\DockerServer;

$server = (new DockerServer())->secure()->server('localhost');

or

use Acamposm\DockerEngineApiPoller\DockerServer;

$server = (new DockerServer())->secure()->port(12375)->server('localhost');

API Resources

Containers

Get Containers List

Get a list of the running containers in the docker host.

use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;

$server = (new DockerServer())->server('192.168.10.101');

$containers_list = (new DockerApiRequest($server))
  ->containers(ResourceMethods::CONTAINERS_LIST)
  ->get();

Get Container Details

To get the full details of a container...

use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;

$server = (new DockerServer())->server('192.168.10.101');

$container_details = (new DockerApiRequest($server))
  ->containers(ResourceMethods::CONTAINERS_INSPECT, 'container_name')
  ->get();

Get Container Stats

Get the resources used by a single container, then use the class ContainerMetrics to get the usage of a container.

use Acamposm\DockerEngineApiPoller\ContainerMetrics;
use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;
$server = (new DockerServer())->server('192.168.10.101');

$container_stats = (new DockerApiRequest($server))
  ->containers(ResourceMethods::CONTAINERS_STATS, 'container_name')
  ->get();

$metrics = (new ContainerMetrics($container_stats))->metrics();

var_dump($metrics);

The result will be a json object with the container statistics, ready to save to a database.

{
  "timestamp": "2020-09-20T19:00:05.491127778Z",
  "id": "2206b35c6fecc6ce320effb68492d8a79fd5f2e5f230dda9371fca8c822428df",
  "name": "/nextcloud",
  "cpu": {
    "count": 2,
    "percent_free": 99.9960912,
    "percent_used": 0.0039088
  },
  "memory": {
    "free": 8236134400,
    "used": 105889792,
    "total": 8342024192,
    "percent_free": 98.730646308823,
    "percent_used": 1.2693536911766
  },
  "network": [
    {
      "eth0": {
        "rx_bytes": 3337270,
        "rx_packets": 3306,
        "rx_errors": 0,
        "rx_dropped": 0,
        "tx_bytes": 1002431,
        "tx_packets": 2090,
        "tx_errors": 0,
        "tx_dropped": 0
      }
    }
  ]
}

Images

Get Images List

To get a list with all images on the docker host.

use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;

$server = (new DockerServer())->server('192.168.10.101');

$images = (new DockerApiRequest($server))
  ->images(ResourceMethods::IMAGES_LIST)
  ->get();

Get Image Details

To get the full details of an image.

use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;

$server = (new DockerServer())->server('192.168.10.101');

$image_details = (new DockerApiRequest($server))
  ->images(ResourceMethods::IMAGES_INSPECT, 'image_name')
  ->get();

Networks

Get Networks List

use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;

$server = (new DockerServer())->server('192.168.10.101');

$networks = (new DockerApiRequest($server))
  ->networks(ResourceMethods::NETWORKS_LIST)
  ->get();
Get Network Details

To get the full details of a network in the docker host.

use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;

$server = (new DockerServer())->server('192.168.10.101');

$network_details = (new DockerApiRequest($server))
  ->networks(ResourceMethods::NETWORKS_INSPECT, 'network_name')
  ->get();

Volumes

Get Volumes List

Get a list of volumes in the docker host.

use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;

$server = (new DockerServer())->server('192.168.10.101');

$volumes = (new DockerApiRequest($server))
  ->volumes(ResourceMethods::VOLUMES_LIST)
  ->get();

Get Volume Details

Get a list of volumes in the docker host.

use Acamposm\DockerEngineApiPoller\DockerServer;
use Acamposm\DockerEngineApiPoller\DockerApiRequest;
use Acamposm\DockerEngineApiPoller\Enums\ResourceMethods;

$server = (new DockerServer())->server('192.168.10.101');

$volume_details = (new DockerApiRequest($server))
  ->volumes(ResourceMethods::VOLUMES_INSPECT, 'volume_name')
  ->get();

Testing


composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Thank you for considering contributing to the improvement of the package. Please see CONTRIBUTING for details.

Security Vulnerabilities

If you discover any security related issues, please send an e-mail to Angel Campos via angel.campos.m@outlook.com instead of using the issue tracker. All security vulnerabilities will be promptly addressed.

Credits

License

The package Ping is open-source package and is licensed under The MIT License (MIT). Please see License File for more information.


  Files folder image Files (31)  
File Role Description
Files folder image.github (2 files)
Files folder image.idea (5 files, 1 directory)
Files folder imagesrc (4 files, 4 directories)
Files folder imagetests (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (31)  /  .github  
File Role Description
  Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
  Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data

  Files folder image Files (31)  /  .idea  
File Role Description
Files folder imagecodeStyles (1 file)
  Accessible without login Plain text file docker-monitor.iml Data Auxiliary data
  Accessible without login Plain text file modules.xml Data Auxiliary data
  Accessible without login Plain text file phing.xml Data Auxiliary data
  Accessible without login Plain text file php.xml Data Auxiliary data
  Accessible without login Plain text file workspace.xml Data Auxiliary data

  Files folder image Files (31)  /  .idea  /  codeStyles  
File Role Description
  Accessible without login Plain text file codeStyleConfig.xml Data Auxiliary data

  Files folder image Files (31)  /  src  
File Role Description
Files folder imageEnums (1 file)
Files folder imageExceptions (6 files)
Files folder imageInterfaces (1 file)
Files folder imageResources (5 files)
  Plain text file ContainerMetrics.php Class Class source
  Plain text file DockerApiRequest.php Class Class source
  Plain text file DockerEngineApiPollerServiceProvider.php Class Class source
  Plain text file DockerServer.php Class Class source

  Files folder image Files (31)  /  src  /  Enums  
File Role Description
  Plain text file ResourceMethods.php Class Class source

  Files folder image Files (31)  /  src  /  Exceptions  
File Role Description
  Plain text file ContainerNotDefinedException.php Class Class source
  Plain text file DockerApiUnreachableException.php Class Class source
  Plain text file ImageNotDefinedException.php Class Class source
  Plain text file MethodNotDefinedException.php Class Class source
  Plain text file NetworkNotDefinedException.php Class Class source
  Plain text file VolumeNotDefinedException.php Class Class source

  Files folder image Files (31)  /  src  /  Interfaces  
File Role Description
  Plain text file ApiResourceInterface.php Class Class source

  Files folder image Files (31)  /  src  /  Resources  
File Role Description
  Plain text file Containers.php Class Class source
  Plain text file Images.php Class Class source
  Plain text file Networks.php Class Class source
  Plain text file System.php Class Class source
  Plain text file Volumes.php Class Class source

  Files folder image Files (31)  /  tests  
File Role Description
  Plain text file DockerServerTest.php Class Class source
  Plain text file TestCase.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:57
This week:0
All time:10,556
This week:60Up