Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Docker Php Api Laravel Package

docker-php/docker-php-api

Unmaintained generated Docker Engine API PHP client (Jane OpenAPI). Not intended for direct use—use docker-php instead. Versioning follows Jane major + Docker API version (e.g., 4.1.25.*); pin versions to avoid breakage.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Avoid Direct Usage: The package explicitly states not to use it directly—instead, rely on docker-php/docker, which depends on this generated API client.
  2. Install via Docker-PHP:
    composer require docker-php/docker
    
    This pulls the API client as a dependency automatically.
  3. First Use Case:
    use Docker\Docker;
    $client = new Docker('unix:///var/run/docker.sock');
    $containers = $client->listContainers();
    print_r($containers);
    
    Verify Docker daemon connectivity and API responses.

Where to Look First


Implementation Patterns

Core Workflows

  1. Container Management:

    // Pull an image
    $client->pull('nginx:latest');
    
    // Run a container
    $container = $client->run('nginx:latest', ['detach' => true]);
    
    // Inspect container
    $inspect = $client->inspectContainer($container['Id']);
    
  2. Image Operations:

    // List images
    $images = $client->listImages(['all' => true]);
    
    // Build from Dockerfile
    $client->buildImage('/path/to/Dockerfile', ['tag' => 'my-image']);
    
  3. Networks/Volumes:

    // Create a network
    $network = $client->createNetwork(['Name' => 'my-net']);
    
    // List volumes
    $volumes = $client->listVolumes();
    

Integration Tips

  • Error Handling: Wrap API calls in try-catch:
    try {
        $client->stopContainer($containerId);
    } catch (\Docker\Exception $e) {
        log::error("Docker error: " . $e->getMessage());
    }
    
  • Async Operations: Use callbacks for long-running tasks (e.g., buildImage):
    $client->buildImage($path, ['tag' => 'my-image'], function ($output) {
        echo $output['stream'] ?? '';
    });
    
  • Configuration: Pass custom Docker host/port:
    $client = new Docker('tcp://192.168.99.100:2376', [
        'ssl' => true,
        'certPath' => '/path/to/certs'
    ]);
    

Laravel-Specific Patterns

  1. Service Provider Binding:
    // app/Providers/DockerServiceProvider.php
    public function register()
    {
        $this->app->singleton('docker', function () {
            return new Docker(env('DOCKER_HOST', 'unix:///var/run/docker.sock'));
        });
    }
    
  2. Artisan Commands:
    // app/Console/Commands/RebuildDocker.php
    protected $docker;
    public function __construct(Docker $docker) { $this->docker = $docker; }
    
    public function handle()
    {
        $this->docker->buildImage('/path/to/Dockerfile', ['tag' => 'app']);
    }
    
  3. Queue Jobs:
    // app/Jobs/DeployContainer.php
    public function handle(Docker $docker)
    {
        $docker->run('app-image', ['detach' => true, 'name' => 'app-container']);
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warning:

    • The package is archived and not maintained. Use docker-php/docker (which depends on this) instead.
    • Risk: Breaking changes if Docker API evolves (e.g., v1.45+ features may not be supported).
  2. Versioning Quirks:

    • Version format: MAJOR.JANE_VERSION.DOCKER_API_VERSION.MINOR (e.g., 4.1.25.0).
    • Fix all but the last number to avoid unexpected API mismatches:
      composer require docker-php/docker-php-api:4.1.25.*
      
  3. SSL/TLS Issues:

    • If using tcp://, ensure certificates are properly configured:
      $client = new Docker('tcp://host:2376', [
          'ssl' => true,
          'certPath' => '/path/to/certs',
          'caPath' => '/path/to/ca.pem'
      ]);
      
    • Debug: Enable verbose output:
      $client->setDebug(true);
      
  4. Rate Limiting:

    • Docker API may throttle requests. Implement retries:
      use Docker\Exception\DockerException;
      $attempts = 0;
      do {
          try { return $client->listContainers(); }
          catch (DockerException $e) {
              if (++$attempts >= 3) throw $e;
              sleep(1);
          }
      } while (true);
      
  5. Platform-Specific Paths:

    • Docker socket paths vary:
      • Linux: unix:///var/run/docker.sock
      • macOS (Docker Desktop): unix:///Users/$USER/.docker/run/docker.sock
      • Windows (WSL2): unix:///mnt/c/users/$USER/.docker/run/docker.sock

Debugging

  • Enable Debug Mode:
    $client->setDebug(true); // Logs raw API requests/responses
    
  • Check Docker Daemon Logs:
    journalctl -u docker.service -f  # Systemd
    docker logs <container-id>       # Container logs
    
  • Validate API Responses: Use var_dump() or print_r() to inspect raw responses:
    $response = $client->inspectContainer($id);
    var_dump($response->getStatusCode(), $response->getBody());
    

Extension Points

  1. Custom API Endpoints: Override the client to extend unsupported endpoints:
    $client->get('/_ping', [], 'GET'); // Raw API call
    
  2. Middleware: Add request/response filters:
    $client->addMiddleware(function ($request) {
        $request->setHeader('X-Custom-Header', 'value');
    });
    
  3. Mocking for Tests: Use GuzzleHttp\HandlerStack to mock responses:
    $handler = HandlerStack::create();
    $handler->push(Middleware::tap(function ($request) {
        return new Response(200, [], json_encode(['Id' => 'test-id']));
    }));
    $client->setHandler($handler);
    

Performance Tips

  • Reuse Connections: Instantiate $client once (e.g., as a singleton).
  • Batch Operations: Use listContainers() with filters instead of multiple inspectContainer() calls.
  • Streaming: For large outputs (e.g., logs), stream responses:
    $client->getContainerLogs($id, [
        'follow' => true,
        'stdout' => true,
        'stream' => true
    ], function ($chunk) {
        echo $chunk;
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity