Skip to content

Commit ed3a1ad

Browse files
authored
Merge pull request #40 from jkniest/feature/modifiable-shell
Allow to specify shell
2 parents c9263f0 + 3070a1b commit ed3a1ad

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ $containerInstance = DockerContainer::create($imageName)
7575
->start();
7676
```
7777

78+
#### Custom shell
79+
80+
If the `bash` shell is not available in your docker image, you can specify an alternative shell.
81+
82+
```php
83+
$containerInstance = DockerContainer::create($imageName)
84+
->shell('sh')
85+
->start();
86+
```
87+
7888
#### Naming the container
7989

8090
You can name the container by passing the name as the second argument to the constructor.

src/DockerContainer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class DockerContainer
1818

1919
public bool $privileged = false;
2020

21+
public string $shell = 'bash';
22+
2123
/** @var PortMapping[] */
2224
public array $portMappings = [];
2325

@@ -80,6 +82,13 @@ public function privileged(bool $privileged = true): self
8082
return $this;
8183
}
8284

85+
public function shell(string $shell): self
86+
{
87+
$this->shell = $shell;
88+
89+
return $this;
90+
}
91+
8392
public function doNotDaemonize(): self
8493
{
8594
$this->daemonize = false;
@@ -203,7 +212,8 @@ public function getExecCommand(string $dockerIdentifier, string $command): strin
203212
'exec',
204213
'--interactive',
205214
$dockerIdentifier,
206-
'bash -',
215+
$this->shell,
216+
'-',
207217
];
208218

209219
return implode(' ', $execCommand);

tests/DockerContainerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ public function it_can_generate_exec_command_with_remote_host()
183183
$this->assertEquals('echo "whoami" | docker -H ssh://username@host exec --interactive abcdefghijkl bash -', $command);
184184
}
185185

186+
/** @test */
187+
public function it_can_generate_exec_command_with_custom_shell()
188+
{
189+
$command = $this->container
190+
->shell('sh')
191+
->getExecCommand('abcdefghijkl', 'whoami');
192+
193+
$this->assertEquals('echo "whoami" | docker exec --interactive abcdefghijkl sh -', $command);
194+
}
195+
186196
/** @test */
187197
public function it_can_generate_copy_command()
188198
{

0 commit comments

Comments
 (0)