PHP code example of prsw / docker-php

1. Go to this page and download the library: Download prsw/docker-php 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/ */

    

prsw / docker-php example snippets


use PRSW\Docker\Client;

// Connect to the default Docker Unix socket
$client = Client::withHttpClient();

// List all containers
$containers = $client->containerList();
var_dump($containers);

use PRSW\Docker\Client;

$client = Client::withHttpClient();

// Inspect a container
$info = $client->containerInspect('container_id');

// Start/stop a container
$client->containerStart('container_id');
$client->containerStop('container_id');

// List images
$images = $client->imageList();

use PRSW\Docker\Client;

\Amp\async(function () {
    $client = Client::withHttpClient();

    $response = $client->systemEvents(fetch: Client::FETCH_STREAM);

    foreach ($response->getBody() as $event) {
        var_dump($event);
    }
})->await();

use PRSW\Docker\Client;

$client = Client::withHttpClient();

$logs = $client->containerLogs('container_id', [
    'stdout' => true,
    'stderr' => true,
    'since'  => (new \DateTime('-7 days'))->getTimestamp(),
]);

foreach ($logs->getBody() as $line) {
    var_dump($line);
}

use PRSW\Docker\Client;

$client = Client::withHttpClient();

$logs = $client->serviceLogs('service_id', [
    'stdout' => true,
    'stderr' => true,
]);

foreach ($logs->getBody() as $line) {
    var_dump($line);
}

$client = Client::withHttpClient(socketPath: '/path/to/docker.sock');

$client = Client::withHttpClient(
    socketPath: '/var/run/docker.sock',
    options: [
        'timeout' => 30,    // request timeout in seconds (-1 for no timeout)
        'retry'   => 3,     // number of retries on failure
    ],
);

$client = $client->withHttpClientOptions(['timeout' => 60, 'retry' => 5]);
bash
composer