From 585719f247ec6778abc870257ca368e705f079b7 Mon Sep 17 00:00:00 2001 From: Philipp Andreas Date: Thu, 14 Mar 2024 20:27:26 +0100 Subject: [PATCH] Added support to specify the network used by container --- src/Container/Container.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Container/Container.php b/src/Container/Container.php index 319454c..3ba7e0c 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -25,6 +25,7 @@ class Container private Process $process; private WaitInterface $wait; + private ?string $network = null; private ?string $healthCheckCommand = null; private int $healthCheckIntervalInMS; @@ -85,6 +86,13 @@ public function withMount(string $localPath, string $containerPath): self return $this; } + public function withNetwork(string $network): self + { + $this->network = $network; + + return $this; + } + public function run(bool $wait = true): self { $this->id = uniqid('testcontainer', true); @@ -111,6 +119,11 @@ public function run(bool $wait = true): self $params[] = $this->healthCheckIntervalInMS . 'ms'; } + if ($this->network !== null) { + $params[] = '--network'; + $params[] = $this->network; + } + $params[] = $this->image; $this->process = new Process($params); @@ -210,6 +223,10 @@ public function logs(): string public function getAddress(): string { + if ($this->network !== null && !empty($this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress'])) { + return $this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress']; + } + return $this->inspectedData[0]['NetworkSettings']['IPAddress']; } }