docker-php-client

System Class Documentation

Description

System is a class for working with Docker system functions through the API. It provides methods for getting information about the Docker system, checking authentication, monitoring events, and managing system resources.

Namespace

Sangezar\DockerClient\Api

Inheritance

The System class inherits from AbstractApi and implements the SystemInterface interface.

Constants

Event Types

public const EVENT_TYPE_CONTAINER = 'container';
public const EVENT_TYPE_IMAGE = 'image';
public const EVENT_TYPE_VOLUME = 'volume';
public const EVENT_TYPE_NETWORK = 'network';
public const EVENT_TYPE_DAEMON = 'daemon';
public const EVENT_TYPE_PLUGIN = 'plugin';
public const EVENT_TYPE_SERVICE = 'service';
public const EVENT_TYPE_NODE = 'node';
public const EVENT_TYPE_SECRET = 'secret';
public const EVENT_TYPE_CONFIG = 'config';

Container Event Types

public const CONTAINER_EVENT_ATTACH = 'attach';
public const CONTAINER_EVENT_COMMIT = 'commit';
public const CONTAINER_EVENT_COPY = 'copy';
public const CONTAINER_EVENT_CREATE = 'create';
public const CONTAINER_EVENT_DESTROY = 'destroy';
public const CONTAINER_EVENT_DETACH = 'detach';
public const CONTAINER_EVENT_DIE = 'die';
public const CONTAINER_EVENT_EXEC_CREATE = 'exec_create';
public const CONTAINER_EVENT_EXEC_DETACH = 'exec_detach';
public const CONTAINER_EVENT_EXEC_START = 'exec_start';
public const CONTAINER_EVENT_EXEC_DIE = 'exec_die';
public const CONTAINER_EVENT_EXPORT = 'export';
public const CONTAINER_EVENT_HEALTH_STATUS = 'health_status';
public const CONTAINER_EVENT_KILL = 'kill';
public const CONTAINER_EVENT_OOM = 'oom';
public const CONTAINER_EVENT_PAUSE = 'pause';
public const CONTAINER_EVENT_RENAME = 'rename';
public const CONTAINER_EVENT_RESIZE = 'resize';
public const CONTAINER_EVENT_RESTART = 'restart';
public const CONTAINER_EVENT_START = 'start';
public const CONTAINER_EVENT_STOP = 'stop';
public const CONTAINER_EVENT_TOP = 'top';
public const CONTAINER_EVENT_UNPAUSE = 'unpause';
public const CONTAINER_EVENT_UPDATE = 'update';

Image Event Types

public const IMAGE_EVENT_DELETE = 'delete';
public const IMAGE_EVENT_IMPORT = 'import';
public const IMAGE_EVENT_LOAD = 'load';
public const IMAGE_EVENT_PULL = 'pull';
public const IMAGE_EVENT_PUSH = 'push';
public const IMAGE_EVENT_SAVE = 'save';
public const IMAGE_EVENT_TAG = 'tag';
public const IMAGE_EVENT_UNTAG = 'untag';

Volume Event Types

public const VOLUME_EVENT_CREATE = 'create';
public const VOLUME_EVENT_DESTROY = 'destroy';
public const VOLUME_EVENT_MOUNT = 'mount';
public const VOLUME_EVENT_UNMOUNT = 'unmount';

Network Event Types

public const NETWORK_EVENT_CREATE = 'create';
public const NETWORK_EVENT_CONNECT = 'connect';
public const NETWORK_EVENT_DESTROY = 'destroy';
public const NETWORK_EVENT_DISCONNECT = 'disconnect';
public const NETWORK_EVENT_REMOVE = 'remove';

Methods

version

public function version(): array

Gets information about the Docker version.

Returns:

Exceptions:

info

public function info(): array

Gets system-wide information.

Returns:

Exceptions:

auth

public function auth(array $authConfig): array

Checks the authentication configuration.

Parameters:

Returns:

Exceptions:

ping

public function ping(): bool

Checks the availability of the Docker server.

Returns:

events

public function events(array $filters = []): array

Gets events from the server in real-time.

Parameters:

Returns:

Exceptions:

Example:

// Get only container events
$events = $system->events(['type' => [System::EVENT_TYPE_CONTAINER]]);

// Get events for containers and networks from yesterday
$events = $system->events([
    'type' => [System::EVENT_TYPE_CONTAINER, System::EVENT_TYPE_NETWORK],
    'since' => strtotime('-1 day')
]);

dataUsage

public function dataUsage(): array

Gets information about data usage.

Returns:

Exceptions:

usage

public function usage(): array

Equivalent to the dataUsage() method, gets information about system usage.

Returns:

Exceptions:

prune

public function prune(array $options = []): array

Removes unused data (containers, networks, images, volumes).

Parameters:

Returns:

Exceptions:

Usage Examples

Getting Information About Docker

use Sangezar\DockerClient\DockerClient;

$client = DockerClient::createUnix();

// Get information about Docker version
$version = $client->system()->version();
echo "Docker Engine version: {$version['Version']}\n";
echo "API version: {$version['ApiVersion']}\n";

// Get system information
$info = $client->system()->info();
echo "Number of containers: {$info['Containers']}\n";
echo "Number of images: {$info['Images']}\n";
echo "OS/Architecture: {$info['OperatingSystem']}/{$info['Architecture']}\n";

Checking Authentication

use Sangezar\DockerClient\DockerClient;

$client = DockerClient::createUnix();

$auth = $client->system()->auth([
    'username' => 'myusername',
    'password' => 'mypassword',
    'serveraddress' => 'https://index.docker.io/v1/'
]);

if (isset($auth['Status']) && $auth['Status'] === 'Login Succeeded') {
    echo "Authentication successful\n";
} else {
    echo "Authentication failed\n";
}

Monitoring Events

use Sangezar\DockerClient\DockerClient;
use Sangezar\DockerClient\Api\System;

$client = DockerClient::createUnix();

// Get container events from the last hour
$events = $client->system()->events([
    'type' => [System::EVENT_TYPE_CONTAINER],
    'since' => strtotime('-1 hour')
]);

foreach ($events as $event) {
    echo "Event: {$event['Type']}/{$event['Action']} for {$event['Actor']['ID']}\n";
    echo "Time: " . date('Y-m-d H:i:s', $event['time']) . "\n";
}

Cleaning Up Unused Resources

use Sangezar\DockerClient\DockerClient;

$client = DockerClient::createUnix();

// Clean up all unused resources (containers, networks, images, volumes)
$pruneResult = $client->system()->prune([
    'volumes' => true,
    'images' => true
]);

echo "Space freed: " . ($pruneResult['SpaceReclaimed'] ?? 0) . " bytes\n";