-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNetwork.php
More file actions
99 lines (86 loc) · 2.47 KB
/
Copy pathNetwork.php
File metadata and controls
99 lines (86 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
namespace DefaultValue\Dockerizer\Docker;
use Symfony\Component\Process\Exception\ProcessFailedException;
class Network
{
/**
* @param \DefaultValue\Dockerizer\Shell\Shell $shell
*/
public function __construct(private \DefaultValue\Dockerizer\Shell\Shell $shell)
{
}
/**
* Return Docker networks. Note that the network ID is truncated to 12 characters.
*
* @return array<string, string>
*/
public function ls(): array
{
$networks = trim($this->shell->run(
['docker', 'network', 'ls', '--format', '{{.ID}} {{.Name}}']
)->getOutput());
$result = [];
foreach (explode(PHP_EOL, $networks) as $network) {
[$id, $name] = explode(' ', $network);
$result[$id] = $name;
}
return $result;
}
/**
* @param string $networkName
* @return void
* @throws ProcessFailedException
*/
public function rm(string $networkName): void
{
$this->shell->mustRun(
['docker', 'network', 'rm', $networkName]
);
}
/**
* @param string $network
* @param string $format
* @return string
*/
public function inspect(string $network, string $format): string
{
$process = $this->shell->mustRun(
['docker', 'network', 'inspect', '--format', $format, $network]
);
return trim($process->getOutput());
}
/**
* @param string $network
* @param string $format
* @return array<string, mixed>
* @throws \JsonException
* @throws ProcessFailedException
*/
public function inspectJsonWithDecode(string $network, string $format = ''): array
{
return json_decode($this->inspect($network, $format), true, 512, JSON_THROW_ON_ERROR);
}
/**
* @param string $network - Either network name or ID
* @param string $containerName
* @return void
*/
public function connect(string $network, string $containerName): void
{
$this->shell->mustRun(
['docker', 'network', 'connect', $network, $containerName]
);
}
/**
* @param string $network - Either network name or ID
* @param string $containerName
* @return void
*/
public function disconnect(string $network, string $containerName): void
{
$this->shell->mustRun(
['docker', 'network', 'disconnect', $network, $containerName]
);
}
}