Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/DockerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class DockerContainer

public array $optionalArgs = [];

public array $commands = [];

public function __construct(string $image, string $name = '')
{
$this->image = $image;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -195,6 +204,7 @@ public function getStartCommand(): string
'run',
...$this->getExtraOptions(),
$this->image,
...$this->commands,
];

if ($this->command !== '') {
Expand Down
21 changes: 17 additions & 4 deletions tests/DockerContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -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()
{
Expand Down Expand Up @@ -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 */
Expand Down