PHP code example of sytxlabs / dockphp

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/ */

    

sytxlabs / dockphp example snippets


use Sytxlabs\Dockphp\DockerClient;

$docker = new DockerClient('/var/run/docker.sock');

// Containers
$docker->containers()->list();
$docker->containers()->inspect('container-id');
$docker->containers()->start('container-id');
$docker->containers()->stop('container-id');
$docker->containers()->restart('container-id');
$docker->containers()->remove('container-id');
$docker->containers()->logs('container-id');

// Images
$docker->images()->list();
$docker->images()->inspect('nginx:latest');
$docker->images()->pull('nginx', 'latest');
$docker->images()->remove('image-id');

// Networks
$docker->networks()->list();
$docker->networks()->create('my-network');
$docker->networks()->remove('network-id');

// Volumes
$docker->volumes()->list();
$docker->volumes()->create('my-volume');
$docker->volumes()->remove('my-volume');

$container = $docker->containers()->inspect('container-id');
echo $container->getName();      // "web", not "/web"
echo $container->isRunning();    // bool, reads State.Running for you

$image = $docker->images()->inspect('nginx:latest');
echo $image->getName();          // "nginx:latest" (first RepoTag), or null if untagged

foreach ($docker->containers()->list(['all' => true]) as $summary) {
    echo $summary->getName(), ' - ', $summary->status, "\n";
}

$container = $docker->containers()->create([
    'Image' => 'nginx:latest',
    'name' => 'web',
    'HostConfig' => [
        'PortBindings' => [
            '80/tcp' => [
                ['HostPort' => '8080'],
            ],
        ],
    ],
]);

$id = $container->json()['Id'];
$docker->containers()->start($id);

$docker->networks()->create('my-network', ['Driver' => 'bridge']);
$docker->volumes()->create('my-volume', ['Driver' => 'local']);

$docker = new DockerClient('/var/run/docker.sock', apiVersion: '1.43');

$docker = new DockerClient(
    socketPath: '/var/run/docker.sock',
    connectTimeout: 5.0, // seconds
    timeout: 30.0,       // seconds
);

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";
    });
});

$exec = $docker->exec()->create('web', [
    'Cmd' => ['ls', '-la'],
    'AttachStdout' => true,
    'AttachStderr' => true,
])->json()['Id'];

$output = $docker->exec()->start($exec)->getBody();

$docker = DockerClient::tcp('docker.example.com', 2376, tls: true, caFile: '/path/ca.pem', certFile: '/path/cert.pem', keyFile: '/path/key.pem');

$docker->images()->pull('registry.example.com/private/app', 'latest', registryAuth: [
    'username' => 'me',
    'password' => 'secret',
    'serveraddress' => 'registry.example.com',
]);

$docker->images()->push('registry.example.com/private/app', 'latest', registryAuth: [
    'username' => 'me',
    'password' => 'secret',
    'serveraddress' => 'registry.example.com',
]);