sangezar/docker-php-client

A PHP client for the Docker Engine API

v1.0.0 2025-04-13 18:18 UTC

This package is auto-updated.

Last update: 2025-06-13 18:59:36 UTC


README

Latest Version on Packagist Total Downloads License

A modern, powerful, and elegant Docker API client for PHP applications with cluster support.

โœจ Features

  • ๐Ÿš€ Full Docker API support
  • ๐Ÿ”„ Container management (create, inspect, start, stop, remove)
  • ๐Ÿ–ผ๏ธ Image operations (build, pull, push, tag)
  • ๐ŸŒ Network configuration & management
  • ๐Ÿ’พ Volume creation & management
  • ๐Ÿ” System information & events
  • ๐Ÿ” TLS authentication support
  • ๐Ÿ”Œ Unix socket & TCP connection support
  • ๐Ÿ“ฆ PSR-18 compatible HTTP client
  • ๐Ÿงฉ Fluent interfaces for all configurations
  • ๐Ÿ”ง Comprehensive exception handling
  • ๐Ÿ˜๏ธ Docker Swarm & cluster operations
  • โšก Asynchronous operations support

๐Ÿ“‹ Requirements

  • PHP 8.2 or higher
  • ext-json
  • PSR-18 HTTP Client
  • Docker Engine API v1.41+

๐Ÿ› ๏ธ Installation

Install the package via Composer:

composer require sangezar/docker-php-client

๐Ÿš€ Quick Start

Connect to Docker via Unix Socket (Default)

use Sangezar\DockerClient\DockerClient;

// Connect to local Docker daemon through unix socket
$client = DockerClient::createUnix();

// List all containers
$containers = $client->container()->list(['all' => true]);

Connect to Docker via TCP

use Sangezar\DockerClient\DockerClient;

// Connect to remote Docker daemon through TCP
$client = DockerClient::createTcp('tcp://docker-host:2375');

// Get system information
$info = $client->system()->info();

Connect to Docker via TCP with TLS

use Sangezar\DockerClient\DockerClient;

// Connect to remote Docker daemon with TLS
$client = DockerClient::createTcp(
    'tcp://docker-host:2376',
    '/path/to/cert.pem',
    '/path/to/key.pem',
    '/path/to/ca.pem'
);

๐Ÿ“Š Working with Containers

Create and Run a Container

use Sangezar\DockerClient\Config\ContainerConfig;

// Create container configuration
$config = ContainerConfig::create()
    ->setImage('nginx:latest')
    ->setName('my-nginx')
    ->exposePorts(80, 443)
    ->addEnv('NGINX_HOST', 'example.com')
    ->addVolume('/var/www', '/usr/share/nginx/html')
    ->setRestartPolicy('always');

// Create container
$container = $client->container()->create($config);

// Start container
$client->container()->start($container['Id']);

List Containers

// List all running containers
$runningContainers = $client->container()->list();

// List all containers (including stopped ones)
$allContainers = $client->container()->list(['all' => true]);

// Filter containers
$filtered = $client->container()->list([
    'filters' => [
        'status' => ['running'],
        'label' => ['com.example.group=web']
    ]
]);

Container Lifecycle Management

$containerId = 'my-container';

// Inspect container
$info = $client->container()->inspect($containerId);

// Stop container
$client->container()->stop($containerId, 10); // 10 seconds timeout

// Restart container
$client->container()->restart($containerId);

// Remove container
$client->container()->remove($containerId, true, true); // force, remove volumes

๐Ÿ–ผ๏ธ Working with Images

// Pull an image
$client->image()->create('nginx', 'latest');

// List images
$images = $client->image()->list(['all' => true]);

// Build an image
$buildOptions = new ImageBuildOptions();
$buildOptions->setTag('my-app:latest')
    ->setContext('/path/to/context')
    ->setDockerfile('Dockerfile.prod');

$client->image()->buildWithOptions($buildOptions);

// Tag an image
$client->image()->tag('my-app:latest', 'registry.example.com/my-app', 'v1.0');

// Push an image
$client->image()->push('registry.example.com/my-app:v1.0');

๐ŸŒ Working with Networks

use Sangezar\DockerClient\Config\NetworkConfig;

// Create a network
$networkConfig = NetworkConfig::create()
    ->setName('app-network')
    ->setDriver('bridge')
    ->addSubnet('172.28.0.0/16', '172.28.0.1')
    ->addLabel('environment', 'production');

$network = $client->network()->create($networkConfig);

// Connect a container to network
$client->network()->connect('app-network', 'my-container', [
    'Aliases' => ['web-server']
]);

// List networks
$networks = $client->network()->list();

๐Ÿ’พ Working with Volumes

use Sangezar\DockerClient\Config\VolumeConfig;

// Create a volume
$volumeConfig = VolumeConfig::create()
    ->setName('data-volume')
    ->setDriver('local')
    ->addLabel('backup', 'daily');

$volume = $client->volume()->create($volumeConfig);

// Inspect volume
$volumeInfo = $client->volume()->inspect('data-volume');

// List volumes
$volumes = $client->volume()->list();

๐Ÿ˜๏ธ Working with Docker Clusters

use Sangezar\DockerClient\Cluster\DockerCluster;

// Create a cluster
$cluster = new DockerCluster();

// Add nodes to cluster
$cluster->addNode('node1', DockerClient::createTcp('tcp://node1:2375'));
$cluster->addNode('node2', DockerClient::createTcp('tcp://node2:2375'));

// Get all containers across the cluster
$allContainers = $cluster->nodes()->containers()->list(['all' => true]);

// Filter nodes by name pattern
$webNodes = $cluster->getNodeCollection()->filter(function($client, $name) {
    return str_contains($name, 'web');
});

// Create containers on specific nodes
$webNodes->containers()->create($containerConfig);

๐Ÿงฉ Advanced Usage

Custom HTTP Client

use Sangezar\DockerClient\Config\ClientConfig;
use Sangezar\DockerClient\DockerClient;
use GuzzleHttp\Client;

// Create custom HTTP client
$httpClient = new Client([
    'timeout' => 30,
    'connect_timeout' => 5
]);

// Create client config
$config = new ClientConfig();
$config->setEndpoint('unix:///var/run/docker.sock');

// Create Docker client with custom HTTP client
$client = new DockerClient($config, $httpClient);

Handle Events Stream

// Get Docker events stream
$events = $client->system()->events([
    'filters' => [
        'type' => ['container'],
        'event' => ['start', 'stop', 'die']
    ]
]);

// Process events
foreach ($events as $event) {
    $type = $event['Type'];
    $action = $event['Action'];
    $id = $event['Actor']['ID'];
    
    echo "Event: {$type} {$action} on {$id}\n";
}

๐Ÿ“š Documentation

For full documentation, please visit the documentation site.

Documentation is available in:

๐Ÿงช Testing

composer test

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

The MIT License (MIT). Please see License File for more information.

โญ Star the Project

If you find this client useful, please consider giving it a star on GitHub! It helps to increase the visibility of the project and motivate further development.

๐Ÿ™ Credits