diff --git a/src/Container/GenericContainer.php b/src/Container/GenericContainer.php index 5a9d113..45d3998 100644 --- a/src/Container/GenericContainer.php +++ b/src/Container/GenericContainer.php @@ -96,6 +96,9 @@ class GenericContainer implements TestContainer */ protected array $mounts = []; + /** @var array */ + protected array $tmpfs = []; + /** @var array List of exposed ports in the format ['8080/tcp'] */ protected array $exposedPorts = []; @@ -221,6 +224,13 @@ public function withMount(string $localPath, string $containerPath): static return $this; } + public function withTmpfs(string $containerPath, string $options = 'rw,noexec'): static + { + $this->tmpfs[$containerPath] = $options; + + return $this; + } + /** * Add ports to be exposed by the Docker container. * This method accepts multiple inputs: single port, multiple ports, or ports with specific protocols @@ -458,7 +468,7 @@ protected function createHostConfig(): ?HostConfig * the API will throw ContainerCreateBadRequestException: bad parameter. * Until it will be checked and fixed, we just return null if these properties are not set. * */ - if ($this->exposedPorts === [] && !$this->isPrivileged && $this->mounts === []) { + if ($this->exposedPorts === [] && !$this->isPrivileged && $this->mounts === [] && $this->tmpfs === []) { return null; } @@ -479,6 +489,10 @@ protected function createHostConfig(): ?HostConfig $hostConfig->setMounts($this->mounts); } + if ($this->tmpfs !== []) { + $hostConfig->setTmpfs($this->tmpfs); + } + return $hostConfig; } diff --git a/src/Container/TestContainer.php b/src/Container/TestContainer.php index 43b68ed..5b16baf 100644 --- a/src/Container/TestContainer.php +++ b/src/Container/TestContainer.php @@ -43,6 +43,8 @@ public function withLabels(array $labels): static; public function withMount(string $localPath, string $containerPath): static; + public function withTmpfs(string $containerPath, string $options = 'rw,noexec'): static; + public function withName(string $name): static; public function withNetwork(string $networkName): static; diff --git a/tests/Integration/GenericContainerTest.php b/tests/Integration/GenericContainerTest.php index 0b4188f..78afe6e 100644 --- a/tests/Integration/GenericContainerTest.php +++ b/tests/Integration/GenericContainerTest.php @@ -291,6 +291,19 @@ public function testShouldSetMount(): void $container->stop(); } + public function testShouldSetTmpfs(): void + { + $container = (new GenericContainer('alpine')) + ->withTmpfs('/mnt/tmpfs') + ->withCommand(['tail', '-f', '/dev/null']) + ->start(); + + $result = $container->exec(['stat', '-f', '-c', '%T', '/mnt/tmpfs']); + self::assertSame('tmpfs', $result); + + $container->stop(); + } + public function testShouldSetPrivilegedMode(): void { $container = (new GenericContainer('alpine'))