mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-12 10:29:37 +00:00
Code reuse on inspecting a docker container
This commit is contained in:
@@ -167,13 +167,7 @@ class Container
|
|||||||
$this->process = new Process($params);
|
$this->process = new Process($params);
|
||||||
$this->process->mustRun();
|
$this->process->mustRun();
|
||||||
|
|
||||||
$inspect = new Process(['docker', 'inspect', $this->id]);
|
$this->inspectedData = $this->getContainerInspect($this->id);
|
||||||
$inspect->mustRun();
|
|
||||||
|
|
||||||
/** @var ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks $inspectedData */
|
|
||||||
$inspectedData = json_decode($inspect->getOutput(), true, 512, JSON_THROW_ON_ERROR);
|
|
||||||
|
|
||||||
$this->inspectedData = $inspectedData;
|
|
||||||
|
|
||||||
Registry::add($this);
|
Registry::add($this);
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,7 @@ trait DockerContainerAwareTrait
|
|||||||
protected function getContainerAddress(string $containerId, ?string $networkName = null, ?array $inspectedData = null): string
|
protected function getContainerAddress(string $containerId, ?string $networkName = null, ?array $inspectedData = null): string
|
||||||
{
|
{
|
||||||
if (! is_array($inspectedData)) {
|
if (! is_array($inspectedData)) {
|
||||||
$process = new Process(['docker', 'inspect', $containerId]);
|
$inspectedData = $this->getContainerInspect($containerId);
|
||||||
$process->mustRun();
|
|
||||||
|
|
||||||
/** @var ContainerInspect $inspectedData */
|
|
||||||
$inspectedData = json_decode($process->getOutput(), true, 512, JSON_THROW_ON_ERROR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_string($networkName)) {
|
if (is_string($networkName)) {
|
||||||
@@ -47,4 +43,19 @@ trait DockerContainerAwareTrait
|
|||||||
|
|
||||||
throw new UnexpectedValueException('Unable to find container IP address');
|
throw new UnexpectedValueException('Unable to find container IP address');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $containerId
|
||||||
|
* @return ContainerInspect
|
||||||
|
*
|
||||||
|
* @throws JsonException
|
||||||
|
*/
|
||||||
|
protected function getContainerInspect(string $containerId): array
|
||||||
|
{
|
||||||
|
$process = new Process(['docker', 'inspect', $containerId]);
|
||||||
|
$process->mustRun();
|
||||||
|
|
||||||
|
/** @var ContainerInspect */
|
||||||
|
return json_decode($process->getOutput(), true, 512, JSON_THROW_ON_ERROR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user