Skip to content

Commit 19ffc9b

Browse files
authored
Docker API connection via TCP rather than unix socket
Connecting to Docker API via TCP rather than unix socket for security purposes. This change requires additional actions in the docker configuration to expose the api on a specific local port.
2 parents dbed4f5 + dfeefe9 commit 19ffc9b

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,45 @@ PHP Docker client
22
==================
33
> Docker API driver for PHP.
44
5+
Docker configuration
6+
--------------------
7+
Docker Engine API must be exposed on a local port in order to be able to connect.
8+
9+
##### 1. Edit the `docker.service` which by default on debian is located at `/lib/systemd/system/docker.service`
10+
11+
From this:
12+
```shell
13+
# /lib/systemd/system/docker.service
14+
...
15+
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
16+
...
17+
```
18+
19+
To this:
20+
```shell
21+
# /lib/systemd/system/docker.service
22+
...
23+
ExecStart=/usr/bin/dockerd
24+
...
25+
```
26+
27+
##### 2. Edit `/etc/docker/daemon.json` to expose docker api at `127.0.0.1:2375`
28+
Add `hosts` to the json file as next:
29+
```json
30+
{
31+
...
32+
"hosts": ["fd://", "tcp://127.0.0.1:2375"]
33+
...
34+
}
35+
```
36+
37+
##### 3. Restart Docker completely
38+
```shell
39+
systemctl daemon-reload
40+
systemctl restart docker
41+
service docker restart
42+
```
43+
544
Installation
645
------------
746
composer require ibra-akv/php-docker-client
@@ -15,8 +54,7 @@ Initialize client
1554
use IterativeCode\Component\DockerClient\DockerClient;
1655

1756
$docker = new DockerClient([
18-
'docker_base_uri' => 'http://localhost/v1.41', # Optional (default: http://localhost/v1.41)
19-
'unix_socket' => '/var/run/docker.sock' # Optional (defaukt: /var/run/docker.sock)
57+
'local_endpoint' => 'http://localhost:2375/v1.41', # Optional (default: http://localhost:2375)
2058
]);
2159

2260
```

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"name": "ibra-akv/php-docker-client",
3-
"version": "1.41.13",
4-
"description": "Docker APIs driver for PHP.",
5-
"homepage": "https://github.com/ibra-akv/php-docker-client",
3+
"description": "Docker API driver for PHP.",
64
"type": "library",
5+
"homepage": "https://github.com/ibra-akv/php-docker-client",
76
"license": "MIT",
87
"authors": [
98
{

docs/DockerClient.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public __construct($options = [])
2828
$options : array
2929
###### Tags
3030
**throws**
31-
- DockerSocketNotFound
31+
- DockerConnectionFailed
3232

3333

3434
#### listContainers()

src/DockerClient.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use GuzzleHttp\Exception\GuzzleException;
66
use IterativeCode\Component\DockerClient\Exception\BadParameterException;
7-
use IterativeCode\Component\DockerClient\Exception\DockerSocketNotFound;
7+
use IterativeCode\Component\DockerClient\Exception\DockerConnectionFailed;
88
use IterativeCode\Component\DockerClient\Exception\ResourceBusyException;
99
use IterativeCode\Component\DockerClient\Exception\ResourceNotFound;
1010
use GuzzleHttp\Client as HttpClient;
@@ -18,34 +18,27 @@ class DockerClient
1818
private $options;
1919

2020
/** @var string */
21-
private $dockerApiEndpoint = 'http://localhost';
22-
23-
/** @var string */
24-
private $unixSocket = '/var/run/docker.sock';
21+
private $dockerApiEndpoint = 'http://localhost:2375';
2522

2623
/**
2724
* DockerClient constructor.
2825
*
2926
* @param array $options
3027
*
31-
* @throws DockerSocketNotFound
28+
* @throws DockerConnectionFailed
3229
*/
3330
public function __construct($options = [])
3431
{
3532
$this->options = $options;
3633

37-
if (!empty($options['docker_base_uri'])) {
38-
$this->dockerApiEndpoint = $options['docker_base_uri'];
34+
if (!empty($options['local_endpoint'])) {
35+
$this->dockerApiEndpoint = $options['local_endpoint'];
3936
}
4037

4138
$this->http = new HttpClient([
4239
'base_uri' => $this->dockerApiEndpoint,
4340
]);
4441

45-
if (!empty($options['unix_socket'])) {
46-
$this->unixSocket = $options['unix_socket'];
47-
}
48-
4942
$this->testConnection();
5043
}
5144

@@ -54,13 +47,9 @@ private function testConnection()
5447
try {
5548
return $this->info();
5649
} catch (\Exception $e) {
57-
$search = 'failed binding local connection end';
58-
if (strpos(strtolower($e->getMessage()), $search) !== false) {
59-
$text = sprintf('Could not bind to docker socket at %s', $this->unixSocket);
60-
throw new DockerSocketNotFound($text);
61-
}
50+
$text = sprintf('Docker API connection failed: %s', $this->dockerApiEndpoint);
6251

63-
throw $e;
52+
throw new DockerConnectionFailed($text);
6453
}
6554
}
6655

@@ -76,7 +65,6 @@ private function testConnection()
7665
*/
7766
private function request($method, $url, $options = [], $resolveResponse = true)
7867
{
79-
$options = array_replace_recursive(['curl' => [CURLOPT_UNIX_SOCKET_PATH => $this->unixSocket]], $options);
8068
$response = $this->http->request($method, $url, $options);
8169
if ($resolveResponse) {
8270
return json_decode($response->getBody()->getContents(), true);

src/Exception/DockerSocketNotFound.php renamed to src/Exception/DockerConnectionFailed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace IterativeCode\Component\DockerClient\Exception;
44

5-
class DockerSocketNotFound extends \Exception
5+
class DockerConnectionFailed extends \Exception
66
{
77

88
}

0 commit comments

Comments
 (0)