diff --git a/README.md b/README.md index 820acd8..c6dba3f 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,17 @@ $containerInstance = DockerContainer::create($imageName) ->start(); ``` +#### Adding Commands + +You can add commands using the `setCommands` method. + +```php +$containerInstance = DockerContainer::create($imageName) + ->setCommands('--api.insecure=true', '--providers.docker=true') + ->start(); +``` +These commands will be placed at the end of to the `docker run` command. + #### Add optional arguments If you want to add optional arguments to the `docker run` command, use `setOptionalArgs` method: @@ -154,7 +165,7 @@ $containerInstance = DockerContainer::create($imageName) ->setOptionalArgs('-it', '-a') ->start(); ``` -These arguments will be places after `docker run` immediately. +These arguments will be placed after `docker run` immediately. #### Automatically stopping the container after PHP exists diff --git a/src/DockerContainer.php b/src/DockerContainer.php index f3b1811..05eef6c 100644 --- a/src/DockerContainer.php +++ b/src/DockerContainer.php @@ -44,6 +44,8 @@ class DockerContainer public array $optionalArgs = []; + public array $commands = []; + public function __construct(string $image, string $name = '') { $this->image = $image; @@ -157,6 +159,13 @@ public function setOptionalArgs(...$args): self return $this; } + public function setCommands(...$args): self + { + $this->commands = $args; + + return $this; + } + public function stopOnDestruct(bool $stopOnDestruct = true): self { $this->stopOnDestruct = $stopOnDestruct; @@ -195,6 +204,7 @@ public function getStartCommand(): string 'run', ...$this->getExtraOptions(), $this->image, + ...$this->commands, ]; if ($this->command !== '') { diff --git a/tests/DockerContainerTest.php b/tests/DockerContainerTest.php index acd4a68..0054b18 100644 --- a/tests/DockerContainerTest.php +++ b/tests/DockerContainerTest.php @@ -60,7 +60,7 @@ public function it_can_not_be_cleaned_up() $this->assertEquals('docker run -d spatie/docker', $command); } - /** @test **/ + /** @test * */ public function it_can_be_named() { $command = $this->container @@ -111,7 +111,8 @@ public function it_can_set_volumes() ->setVolume('/data', '/data') ->getStartCommand(); - $this->assertEquals('docker run -v /on/my/host:/on/my/container -v /data:/data -d --rm spatie/docker', $command); + $this->assertEquals('docker run -v /on/my/host:/on/my/container -v /data:/data -d --rm spatie/docker', + $command); } /** @test */ @@ -123,7 +124,8 @@ public function it_can_set_labels() ->setLabel('name', 'spatie') ->getStartCommand(); - $this->assertEquals('docker run -l traefik.enable=true -l foo=bar -l name=spatie -d --rm spatie/docker', $command); + $this->assertEquals('docker run -l traefik.enable=true -l foo=bar -l name=spatie -d --rm spatie/docker', + $command); } /** @test */ @@ -136,6 +138,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_commands() + { + $command = $this->container + ->setCommands('--api.insecure=true', '--entrypoints.web.address=:80') + ->getStartCommand(); + + $this->assertEquals('docker run -d --rm spatie/docker --api.insecure=true --entrypoints.web.address=:80', $command); + } + /** @test */ public function it_can_set_network() { @@ -201,7 +213,8 @@ public function it_can_generate_exec_command_with_remote_host() ->remoteHost('ssh://username@host') ->getExecCommand('abcdefghijkl', 'whoami'); - $this->assertEquals('echo "whoami" | docker -H ssh://username@host exec --interactive abcdefghijkl bash -', $command); + $this->assertEquals('echo "whoami" | docker -H ssh://username@host exec --interactive abcdefghijkl bash -', + $command); } /** @test */