1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dock\Docker; |
4
|
|
|
|
5
|
|
|
use Dock\Docker\Containers\Container; |
6
|
|
|
use Dock\Docker\Dns\ContainerAddressResolver; |
7
|
|
|
use Dock\IO\ProcessRunner; |
8
|
|
|
|
9
|
|
|
class ContainerDetails implements Containers\ContainerDetails |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ProcessRunner |
13
|
|
|
*/ |
14
|
|
|
private $processRunner; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ContainerAddressResolver |
18
|
|
|
*/ |
19
|
|
|
private $containerAddressResolver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param ProcessRunner $processRunner |
23
|
|
|
* @param ContainerAddressResolver $containerAddressResolver |
24
|
|
|
*/ |
25
|
|
|
public function __construct(ProcessRunner $processRunner, ContainerAddressResolver $containerAddressResolver) |
26
|
|
|
{ |
27
|
|
|
$this->processRunner = $processRunner; |
28
|
|
|
$this->containerAddressResolver = $containerAddressResolver; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $containerId |
33
|
|
|
* |
34
|
|
|
* @return Container |
35
|
|
|
*/ |
36
|
|
|
public function findById($containerId) |
37
|
|
|
{ |
38
|
|
|
return $this->getContainerFromInspection($this->inspectContainer($containerId)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $containerId |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
private function inspectContainer($containerId) |
47
|
|
|
{ |
48
|
|
|
$command = sprintf('docker inspect %s', $containerId); |
49
|
|
|
$rawOutput = $this->processRunner->run($command)->getOutput(); |
50
|
|
|
$rawOutput = trim($rawOutput); |
51
|
|
|
|
52
|
|
|
if (null === ($inspection = json_decode($rawOutput, true))) { |
53
|
|
|
throw new \RuntimeException('Unable to inspect container'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $inspection[0]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $inspection |
61
|
|
|
* |
62
|
|
|
* @return Container |
63
|
|
|
*/ |
64
|
|
|
private function getContainerFromInspection(array $inspection) |
65
|
|
|
{ |
66
|
|
|
$containerName = substr($inspection['Name'], 1); |
67
|
|
|
$containerConfiguration = $inspection['Config']; |
68
|
|
|
$imageName = $containerConfiguration['Image']; |
69
|
|
|
$exposedPorts = isset($containerConfiguration['ExposedPorts']) ? array_keys( |
70
|
|
|
$containerConfiguration['ExposedPorts'] |
71
|
|
|
) : []; |
72
|
|
|
$componentName = isset($containerConfiguration['Labels']['com.docker.compose.service']) ? $containerConfiguration['Labels']['com.docker.compose.service'] : null; |
73
|
|
|
$ipAddress = isset($inspection['NetworkSettings']['IPAddress']) ? $inspection['NetworkSettings']['IPAddress'] : null; |
74
|
|
|
|
75
|
|
|
return new Container( |
76
|
|
|
$containerName, |
77
|
|
|
$imageName, |
78
|
|
|
$inspection['State']['Running'] ? Container::STATE_RUNNING : Container::STATE_EXITED, |
79
|
|
|
$this->containerAddressResolver->getDnsByContainerNameAndImage($containerName, $imageName), |
80
|
|
|
$exposedPorts, |
81
|
|
|
$componentName, |
82
|
|
|
$ipAddress |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|