Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker PHP SDK

Asynchronous Docker Engine API client for PHP, built on amphp and generated from the official OpenAPI specification.

This is a rewrite of docker-php/docker-php.

CAUTION Not all endpoints have streaming support implemented.

Requirements

  • PHP 8.3+
  • Docker Engine (Unix socket access)

Installation

composer require prsw/docker-php

Quick Start

use PRSW\Docker\Client;

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

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

Usage

Basic Operations

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();

Streaming Events

Stream real-time Docker events using FETCH_STREAM:

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();

Container Logs

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

Service Logs

use PRSW\Docker\Client;

$client = Client::withHttpClient();

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

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

Custom Socket Path

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

Custom Client Options

$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
    ],
);

You can also create a new client instance with different options using:

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

API Version

This SDK targets the Docker Engine API v1.47 (Docker 27.x+). The OpenAPI specification is at v1.47.yaml.

License

MIT

About

Async Docker Client

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages