Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit 9d1869d

Browse files
committed
feat: allow overriding Docker API version
1 parent 41afe41 commit 9d1869d

3 files changed

Lines changed: 74 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ composer require beluga-php/docker-php
2020

2121
## Docker API Version
2222

23-
By default it will use the last version of docker api available, if you want to fix a version (like 1.41) you can add this
24-
requirement to composer:
23+
Each release line targets a Docker API version. The 1.45 client uses API v1.45 by default.
24+
25+
You can override the version used in request URLs when connecting to a Docker daemon that no longer accepts v1.45:
26+
27+
```bash
28+
DOCKER_API_VERSION=1.52 php your-script.php
29+
```
30+
31+
This only changes the API version in request URLs. The generated models and endpoints still use the v1.45 specification.
32+
33+
To use an older API specification, install the matching API package:
2534

2635
```bash
2736
composer require "beluga-php/docker-php-api:6.1.41.*"

‎src/DockerClientFactory.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public static function create(array $config = [], ?PluginClientFactory $pluginCl
2626

2727
$uriFactory = Psr17FactoryDiscovery::findUriFactory();
2828
$host = preg_match('/unix:\/\//', $config['remote_socket']) ? 'http://localhost' : $config['remote_socket'];
29+
$dockerApiVersion = ltrim(getenv('DOCKER_API_VERSION') ?: 'v1.45', '/');
30+
if (!str_starts_with($dockerApiVersion, 'v')) {
31+
$dockerApiVersion = 'v'.$dockerApiVersion;
32+
}
2933

3034
$pluginClientFactory ??= new PluginClientFactory();
3135

@@ -34,7 +38,7 @@ public static function create(array $config = [], ?PluginClientFactory $pluginCl
3438
[
3539
new ContentLengthPlugin(),
3640
new DecoderPlugin(),
37-
new AddPathPlugin($uriFactory->createUri('/v1.45')),
41+
new AddPathPlugin($uriFactory->createUri('/'.$dockerApiVersion)),
3842
new AddHostPlugin($uriFactory->createUri($host)),
3943
new HeaderDefaultsPlugin([
4044
'host' => parse_url($host, \PHP_URL_HOST),

‎tests/DockerClientFactoryTest.php‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
namespace Docker\Tests;
66

77
use Docker\DockerClientFactory;
8+
use Http\Client\Common\Plugin\AddPathPlugin;
9+
use Http\Client\Common\PluginClient;
10+
use Http\Client\Common\PluginClientFactory;
811
use Psr\Http\Client\ClientInterface;
912

1013
class DockerClientFactoryTest extends TestCase
1114
{
1215
protected function tearDown(): void
1316
{
1417
parent::tearDown();
18+
PluginClientFactory::setFactory(static function ($client, array $plugins, array $options): PluginClient {
19+
unset($options['client_name']);
20+
21+
return new PluginClient($client, $plugins, $options);
22+
});
23+
putenv('DOCKER_API_VERSION');
1524
putenv('DOCKER_TLS_VERIFY');
1625
}
1726

@@ -20,6 +29,32 @@ public function testStaticConstructor(): void
2029
$this->assertInstanceOf(ClientInterface::class, DockerClientFactory::create());
2130
}
2231

32+
public function testDefaultApiVersion(): void
33+
{
34+
$this->assertSame('/v1.45', $this->getApiPath());
35+
}
36+
37+
public function testApiVersionFromEnvironment(): void
38+
{
39+
putenv('DOCKER_API_VERSION=v1.52');
40+
41+
$this->assertSame('/v1.52', $this->getApiPath());
42+
}
43+
44+
public function testApiVersionWithLeadingSlash(): void
45+
{
46+
putenv('DOCKER_API_VERSION=/v1.52');
47+
48+
$this->assertSame('/v1.52', $this->getApiPath());
49+
}
50+
51+
public function testApiVersionWithoutVersionPrefix(): void
52+
{
53+
putenv('DOCKER_API_VERSION=1.52');
54+
55+
$this->assertSame('/v1.52', $this->getApiPath());
56+
}
57+
2358
public function testCreateFromEnvWithoutCertPath(): void
2459
{
2560
$this->expectException(\RuntimeException::class);
@@ -68,4 +103,27 @@ public function testCreateCustomPeerName(): void
68103
$this->assertSame('/abc/key.pem', $context['ssl']['local_pk']);
69104
$this->assertSame('test', $context['ssl']['peer_name']);
70105
}
106+
107+
private function getApiPath(): string
108+
{
109+
$plugins = [];
110+
PluginClientFactory::setFactory(static function ($client, array $createdPlugins, array $options) use (&$plugins): PluginClient {
111+
$plugins = $createdPlugins;
112+
unset($options['client_name']);
113+
114+
return new PluginClient($client, $createdPlugins, $options);
115+
});
116+
117+
DockerClientFactory::create();
118+
119+
foreach ($plugins as $plugin) {
120+
if ($plugin instanceof AddPathPlugin) {
121+
$uri = (new \ReflectionProperty($plugin, 'uri'))->getValue($plugin);
122+
123+
return $uri->getPath();
124+
}
125+
}
126+
127+
$this->fail('The Docker API path plugin was not configured.');
128+
}
71129
}

0 commit comments

Comments
 (0)