From 4d3cd74409834ce406f180c62b811a0f2be18df9 Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Wed, 29 Jun 2022 21:23:00 +0200 Subject: [PATCH 1/3] Allow to specify a docker network --- README.md | 10 ++++++++++ src/DockerContainer.php | 13 +++++++++++++ tests/DockerContainerTest.php | 10 ++++++++++ 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 6b1d2f4..1b19ea7 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,16 @@ $containerInstance = DockerContainer::create($imageName) ->start(); ``` +#### Attaching a network to the container + +If you want to attach the container to a docker network, use `setNetwork` method: + +```php +$containerInstance = DockerContainer::create($imageName) + ->network('my-network') + ->start(); +``` + #### Specify a remote docker host for execution You can set the host used for executing the container. The `docker` command line accepts a daemon socket string. To connect to a remote docker host via ssh, use the syntax `ssh://username@hostname`. Note that the proper SSH keys will already need to be configured for this work. diff --git a/src/DockerContainer.php b/src/DockerContainer.php index 067fb9e..51a97b3 100644 --- a/src/DockerContainer.php +++ b/src/DockerContainer.php @@ -20,6 +20,8 @@ class DockerContainer public string $shell = 'bash'; + public ?string $network = null; + /** @var PortMapping[] */ public array $portMappings = []; @@ -89,6 +91,13 @@ public function shell(string $shell): self return $this; } + public function network(?string $network): self + { + $this->network = $network; + + return $this; + } + public function doNotDaemonize(): self { $this->daemonize = false; @@ -306,6 +315,10 @@ protected function getExtraOptions(): array $extraOptions[] = '--rm'; } + if ($this->network) { + $extraOptions[] = '--network ' . $this->network; + } + return $extraOptions; } diff --git a/tests/DockerContainerTest.php b/tests/DockerContainerTest.php index 2a952af..acd4a68 100644 --- a/tests/DockerContainerTest.php +++ b/tests/DockerContainerTest.php @@ -136,6 +136,16 @@ public function it_can_set_optional_args() $this->assertEquals('docker run -it -a -i -t -d --rm spatie/docker', $command); } + /** @test */ + public function it_can_set_network() + { + $command = $this->container + ->network('my-network') + ->getStartCommand(); + + $this->assertEquals('docker run -d --rm --network my-network spatie/docker', $command); + } + /** @test */ public function it_can_use_remote_docker_host() { From f9a495625b4fa7412162571220d2622ff4f9c7af Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Wed, 29 Jun 2022 21:24:23 +0200 Subject: [PATCH 2/3] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1b19ea7..820acd8 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ $containerInstance = DockerContainer::create($imageName) #### Attaching a network to the container -If you want to attach the container to a docker network, use `setNetwork` method: +If you want to attach the container to a docker network, use `network` method: ```php $containerInstance = DockerContainer::create($imageName) From da89a9def0e698db1b1ec837b722bf039e8b6134 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Thu, 30 Jun 2022 00:08:37 +0200 Subject: [PATCH 3/3] Update DockerContainer.php --- src/DockerContainer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DockerContainer.php b/src/DockerContainer.php index 51a97b3..f3b1811 100644 --- a/src/DockerContainer.php +++ b/src/DockerContainer.php @@ -91,7 +91,7 @@ public function shell(string $shell): self return $this; } - public function network(?string $network): self + public function network(string $network): self { $this->network = $network;