Skip to content

Commit ffa7e48

Browse files
authored
Merge pull request #43 from jkniest/feature/set-network-on-container
Allow to specify a docker network
2 parents fab9bab + da89a9d commit ffa7e48

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ $containerInstance = DockerContainer::create($imageName)
167167
->start();
168168
```
169169

170+
#### Attaching a network to the container
171+
172+
If you want to attach the container to a docker network, use `network` method:
173+
174+
```php
175+
$containerInstance = DockerContainer::create($imageName)
176+
->network('my-network')
177+
->start();
178+
```
179+
170180
#### Specify a remote docker host for execution
171181

172182
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.

src/DockerContainer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class DockerContainer
2020

2121
public string $shell = 'bash';
2222

23+
public ?string $network = null;
24+
2325
/** @var PortMapping[] */
2426
public array $portMappings = [];
2527

@@ -89,6 +91,13 @@ public function shell(string $shell): self
8991
return $this;
9092
}
9193

94+
public function network(string $network): self
95+
{
96+
$this->network = $network;
97+
98+
return $this;
99+
}
100+
92101
public function doNotDaemonize(): self
93102
{
94103
$this->daemonize = false;
@@ -306,6 +315,10 @@ protected function getExtraOptions(): array
306315
$extraOptions[] = '--rm';
307316
}
308317

318+
if ($this->network) {
319+
$extraOptions[] = '--network ' . $this->network;
320+
}
321+
309322
return $extraOptions;
310323
}
311324

tests/DockerContainerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ public function it_can_set_optional_args()
136136
$this->assertEquals('docker run -it -a -i -t -d --rm spatie/docker', $command);
137137
}
138138

139+
/** @test */
140+
public function it_can_set_network()
141+
{
142+
$command = $this->container
143+
->network('my-network')
144+
->getStartCommand();
145+
146+
$this->assertEquals('docker run -d --rm --network my-network spatie/docker', $command);
147+
}
148+
139149
/** @test */
140150
public function it_can_use_remote_docker_host()
141151
{

0 commit comments

Comments
 (0)