docker-client/open-api
Autogenerated PHP OpenAPI client for the Docker Engine API. Install via Composer and use generated endpoints/models to talk to Docker over the official v1.40 spec. Versioning tracks jane-php major + Docker API major/minor + patch.
Installation:
composer require docker-client/open-api
Ensure your composer.json targets PHP 7.4+ (check supported versions).
First Use Case: Initialize the client and list containers:
use Docker\Client;
$client = new Client();
$containers = $client->containers()->listContainers();
print_r($containers);
Where to Look First:
src/Api/ directory in the package for raw API classes (if extending).Basic CRUD Operations:
// Create a container
$client->containers()->createContainer([
'Image' => 'nginx:latest',
'Cmd' => ['nginx', '-g', 'daemon off;'],
]);
// Start/Stop
$client->containers()->start('container_id');
$client->containers()->stop('container_id');
// Inspect
$inspect = $client->containers()->inspect('container_id');
Handling Responses:
Use ApiException for error handling:
try {
$logs = $client->containers()->logs('container_id');
} catch (ApiException $e) {
echo "Error: " . $e->getMessage();
}
Pagination:
Leverage listContainers() with limit and offset:
$containers = $client->containers()->listContainers(['limit' => 10, 'offset' => 0]);
Async Operations:
Use wait() for container lifecycle events:
$result = $client->containers()->wait('container_id');
Laravel Service Provider: Bind the client to the container for dependency injection:
$this->app->singleton(Client::class, function () {
return new Client();
});
Then inject Client into controllers/services.
Environment Configuration: Override Docker host/port via constructor:
$client = new Client('unix:///var/run/docker.sock'); // Unix socket
// or
$client = new Client('tcp://127.0.0.1:2375'); // TCP
Logging: Enable debug logging for API calls:
$client = new Client(['basePath' => 'unix:///var/run/docker.sock', 'debug' => true]);
Version Mismatch:
v6.1.40.0 targets Docker API v1.40 with jane-php v6.x.composer.json to avoid breaking changes:
"docker-client/open-api": "v6.1.40.0"
Deprecated Endpoints:
/containers/{id}/attach/ws) are WebSocket-only and may not work with this client.Authentication:
$client = new Client('https://user:pass@host:2376', [
'verifyPeer' => false, // Disable if using self-signed certs (not recommended)
]);
Rate Limiting:
use GuzzleHttp\Exception\RequestException;
try {
$client->containers()->listContainers();
} catch (RequestException $e) {
if ($e->getCode() === 429) {
sleep(2); // Retry after 2 seconds
retry();
}
}
Enable Debug Mode:
$client = new Client(['debug' => true]);
Logs will show raw API requests/responses.
Inspect Raw Responses:
Use getLastResponse() to debug:
$response = $client->containers()->listContainers();
echo $client->getLastResponse()->getBody();
Common Errors:
404 Not Found: Verify container IDs or endpoint paths (e.g., /containers/{id} vs /containers/{name}).500 Internal Server Error: Check Docker daemon logs (journalctl -u docker or docker logs <daemon-container>).Custom API Clients:
Extend Docker\Client to add domain-specific methods:
class CustomClient extends Client {
public function restartAllContainers() {
$containers = $this->containers()->listContainers();
foreach ($containers as $container) {
$this->containers()->restart($container['Id']);
}
}
}
Override HTTP Client: Replace the default Guzzle client for custom behavior (e.g., middleware):
use GuzzleHttp\Client as GuzzleClient;
$guzzle = new GuzzleClient([
'timeout' => 30,
'headers' => ['User-Agent' => 'MyApp/1.0'],
]);
$client = new Client(['httpClient' => $guzzle]);
Add New Endpoints: If missing an endpoint, generate it manually using the OpenAPI spec and the Jane PHP generator.
How can I help you explore Laravel packages today?