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.
Sangezar\DockerClient\Api
The System
class inherits from AbstractApi
and implements the SystemInterface
interface.
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';
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';
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';
public const VOLUME_EVENT_CREATE = 'create';
public const VOLUME_EVENT_DESTROY = 'destroy';
public const VOLUME_EVENT_MOUNT = 'mount';
public const VOLUME_EVENT_UNMOUNT = 'unmount';
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';
public function version(): array
Gets information about the Docker version.
OperationFailedException
- if the request failspublic function info(): array
Gets system-wide information.
OperationFailedException
- if the request failspublic function auth(array $authConfig): array
Checks the authentication configuration.
$authConfig
- Authentication configuration:
username
(string) - Username for authentication in the registrypassword
(string) - Password for authentication in the registryemail
(string) - Email for authentication in the registry (optional)serveraddress
(string) - Registry address (e.g., https://index.docker.io/v1/)identitytoken
(string) - Identity token for the registry (optional)registrytoken
(string) - Registry token (optional)MissingRequiredParameterException
- if required parameters are missingOperationFailedException
- if authentication failspublic function ping(): bool
Checks the availability of the Docker server.
true
if the Docker server is available, false
if notpublic function events(array $filters = []): array
Gets events from the server in real-time.
$filters
- Filters to apply to events:
config
- object with attributes (optional)type
- array of event types (optional, use EVENT_TYPE_* constants)until
- timestamp (optional)since
- timestamp (optional)InvalidParameterValueException
- if some filter values are invalidOperationFailedException
- if the request fails// 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')
]);
public function dataUsage(): array
Gets information about data usage.
OperationFailedException
- if the request failspublic function usage(): array
Equivalent to the dataUsage() method, gets information about system usage.
OperationFailedException
- if the request failspublic function prune(array $options = []): array
Removes unused data (containers, networks, images, volumes).
$options
- Cleanup options:
volumes
(bool) - Remove unused volumesnetworks
(bool) - Remove unused networkscontainers
(bool) - Remove stopped containersimages
(bool) - Remove unused imagesbuilder
(bool) - Clean up build cachefilters
(array) - Filters to applyInvalidParameterValueException
- if options are invalidOperationFailedException
- if cleanup failsuse 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";
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";
}
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";
}
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";