Skip to content

Commit

Permalink
Code reuse on inspecting a docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
dinamic committed Jul 16, 2024
1 parent de59891 commit ed18c0b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
8 changes: 1 addition & 7 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,7 @@ public function run(bool $wait = true): self
$this->process = new Process($params);
$this->process->mustRun();

$inspect = new Process(['docker', 'inspect', $this->id]);
$inspect->mustRun();

/** @var ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks $inspectedData */
$inspectedData = json_decode($inspect->getOutput(), true, 512, JSON_THROW_ON_ERROR);

$this->inspectedData = $inspectedData;
$this->inspectedData = $this->getContainerInspect($this->id);

Registry::add($this);

Expand Down
21 changes: 16 additions & 5 deletions src/Trait/DockerContainerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ trait DockerContainerAwareTrait
protected function getContainerAddress(string $containerId, ?string $networkName = null, ?array $inspectedData = null): string
{
if (! is_array($inspectedData)) {
$process = new Process(['docker', 'inspect', $containerId]);
$process->mustRun();

/** @var ContainerInspect $inspectedData */
$inspectedData = json_decode($process->getOutput(), true, 512, JSON_THROW_ON_ERROR);
$inspectedData = $this->getContainerInspect($containerId);
}

if (is_string($networkName)) {
Expand All @@ -47,4 +43,19 @@ protected function getContainerAddress(string $containerId, ?string $networkName

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);
}
}

0 comments on commit ed18c0b

Please sign in to comment.