1. Go to this page and download the library: Download sytxlabs/dockphp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Sytxlabs\Dockphp\Exceptions\DockerNotFoundException;
use Sytxlabs\Dockphp\Exceptions\DockerApiException;
use Sytxlabs\Dockphp\Exceptions\DockerConnectionException;
try {
$docker->containers()->inspect('does-not-exist');
} catch (DockerNotFoundException $e) {
// 404
} catch (DockerApiException $e) {
// any other non-2xx response
$e->getStatusCode();
$e->getDockerMessage();
} catch (DockerConnectionException $e) {
// could not reach the socket at all
}
// Follow logs
$docker->containers()->logsStream('web', function (string $chunk) {
echo $chunk;
});
// Live stats
$docker->containers()->statsStream('web', function (string $chunk) {
// one or more JSON objects per chunk - see NdjsonLineBuffer below
});
// Pull with progress
$docker->images()->pullStream('nginx', 'latest', null, function (array $event) {
echo $event['status'] ?? '', "\n";
});
// Build with progress
$docker->images()->buildStream($tarContent, function (array $event) {
echo $event['stream'] ?? '';
});
// Real-time event feed
$docker->system()->events(function (array $event) {
echo $event['Type'], ' ', $event['Action'], "\n";
});
use Sytxlabs\Dockphp\Support\StdioDemultiplexer;
$response = $docker->containers()->logs('web');
foreach (StdioDemultiplexer::demuxAll($response->getBody()) as $frame) {
echo "[{$frame['stream']}] {$frame['payload']}";
}
$demux = new StdioDemultiplexer();
$docker->containers()->logsStream('web', function (string $chunk) use ($demux) {
$demux->push($chunk, function (string $stream, string $payload) {
echo "[$stream] $payload";
});
});