|
1 | | -# php-docker-api-client - A PHP docker API client |
| 1 | +# PHP Docker API Client |
2 | 2 |
|
| 3 | +[](https://github.com/WebProject-xyz/php-docker-api-client/actions/workflows/tests.yml) |
| 4 | +[](https://www.codacy.com/gh/WebProject-xyz/php-docker-api-client/dashboard) |
| 5 | +[](https://packagist.org/packages/webproject/php-docker-api-client) |
| 6 | +[](https://packagist.org/packages/webproject/php-docker-api-client) |
3 | 7 |
|
4 | | -## Based on Docker API Version spec [docker-v1.51.yaml](spec/docker-v1.51.yaml) |
5 | | -> converted to openapi v3.1.0 [docker-v1.51-patched.yaml](spec/docker-v1.51-patched.yaml) see [UPDATE.md](spec/UPDATE.md) how to upgrade client |
| 8 | +A modern, high-level PHP client for the Docker Engine API, built on top of [JanePHP](https://github.com/janephp/janephp) and supporting the latest Docker API specs (v1.51+). |
6 | 9 |
|
7 | | -## Example |
| 10 | +--- |
8 | 11 |
|
9 | | -> list running all containers |
10 | | -```shell |
11 | | -bin/docker-api docker:list-containers |
| 12 | +## 🚀 Features |
| 13 | + |
| 14 | +- **High-level Service Layer**: Easy access to container information through the `DockerService`. |
| 15 | +- **Modern PHP**: Fully typed, leveraging PHP 8.3+ features. |
| 16 | +- **Auto-generated Models**: Powered by JanePHP based on official Docker OpenAPI specs. |
| 17 | +- **DTO Support**: Includes `DockerContainerDto` for easy data handling and URL extraction from environment variables. |
| 18 | +- **CLI Tool**: Built-in `bin/docker-api` for quick interactions. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 📦 Installation |
| 23 | + |
| 24 | +```bash |
| 25 | +composer require webproject/php-docker-api-client |
| 26 | +``` |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## 💻 Usage |
| 31 | + |
| 32 | +### Initialize the Client |
| 33 | + |
| 34 | +```php |
| 35 | +use WebProject\DockerApiClient\Client\DockerApiClientWrapper; |
| 36 | +use WebProject\DockerApiClient\Service\DockerService; |
| 37 | + |
| 38 | +// Create a client pointing to your local Docker socket |
| 39 | +$wrapper = DockerApiClientWrapper::create( |
| 40 | + uri: 'http://localhost', |
| 41 | + socketPath: '/var/run/docker.sock' |
| 42 | +); |
| 43 | + |
| 44 | +$dockerService = new DockerService($wrapper); |
| 45 | +``` |
| 46 | + |
| 47 | +### List and Inspect Containers |
| 48 | + |
| 49 | +```php |
| 50 | +// Get all containers (includes inspection data) |
| 51 | +$containers = $dockerService->findAllContainer(); |
| 52 | + |
| 53 | +foreach ($containers as $id => $container) { |
| 54 | + echo "Container ID: {$container->id} |
| 55 | +"; |
| 56 | + echo "Name: {$container->getName()} |
| 57 | +"; |
| 58 | + echo "Image: {$container->image} |
| 59 | +"; |
| 60 | + echo "Running: " . ($container->running ? 'Yes' : 'No') . " |
| 61 | +"; |
| 62 | + |
| 63 | + // Extract virtual hosts from env vars (e.g. for reverse proxies) |
| 64 | + $urls = $container->extractUrlsFromEnvVars(['VIRTUAL_HOST', 'DOMAIN_NAME']); |
| 65 | + print_r($urls); |
| 66 | +} |
12 | 67 | ``` |
13 | 68 |
|
14 | | -> listen for socket events |
15 | | -```shell |
| 69 | +### Direct API Access |
| 70 | + |
| 71 | +For low-level access to all Docker API endpoints, use the generated JanePHP client: |
| 72 | + |
| 73 | +```php |
| 74 | +$client = $wrapper->getDockerClient(); |
| 75 | +$info = $client->systemInfo(); |
| 76 | +echo "Docker Server Version: " . $info->getServerVersion() . " |
| 77 | +"; |
| 78 | +``` |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## 🛠 CLI Usage |
| 83 | + |
| 84 | +The package includes a command-line interface for common tasks: |
| 85 | + |
| 86 | +```bash |
| 87 | +# List all containers |
| 88 | +bin/docker-api docker:list-containers |
| 89 | + |
| 90 | +# Listen for Docker socket events |
16 | 91 | bin/docker-api docker:events:listen |
17 | 92 | ``` |
18 | 93 |
|
19 | | -#### Generated with [janephp/janephp](https://github.com/janephp/janephp) |
| 94 | +--- |
| 95 | + |
| 96 | +## 📖 Development |
| 97 | + |
| 98 | +The underlying client is generated from the Docker API spec. See [UPDATE.md](spec/UPDATE.md) for details on how to update the generated classes when new Docker API versions are released. |
| 99 | + |
| 100 | +## 🤝 Contributing |
| 101 | + |
| 102 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 103 | + |
| 104 | +## 📄 License |
| 105 | + |
| 106 | +This project is licensed under the MIT License. |
0 commit comments