Skip to content

Commit

Permalink
Merge pull request #43 from jkniest/feature/set-network-on-container
Browse files Browse the repository at this point in the history
Allow to specify a docker network
  • Loading branch information
freekmurze authored Jun 29, 2022
2 parents fab9bab + da89a9d commit ffa7e48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `network` 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.
Expand Down
13 changes: 13 additions & 0 deletions src/DockerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class DockerContainer

public string $shell = 'bash';

public ?string $network = null;

/** @var PortMapping[] */
public array $portMappings = [];

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -306,6 +315,10 @@ protected function getExtraOptions(): array
$extraOptions[] = '--rm';
}

if ($this->network) {
$extraOptions[] = '--network ' . $this->network;
}

return $extraOptions;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/DockerContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit ffa7e48

Please sign in to comment.