Skip to content

Commit

Permalink
Merge pull request #58 from stephenstack/main
Browse files Browse the repository at this point in the history
Add optional UDP protocol to port mapping class with TCP as default protocol
  • Loading branch information
freekmurze authored May 27, 2024
2 parents b0c96f3 + febc5d0 commit 7d41541
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ $containerInstance = DockerContainer::create($imageName)
->start();
```

The default protocol for the port mapping is TCP. If you want to use UDP, you can pass it as the third argument.

```php
$containerInstance = DockerContainer::create($imageName)
->mapPort($portOnHost, $portOnContainer, 'udp')
->start();
```

#### Environment variables

You can set environment variables using the `setEnvironmentVariable` method. To add multiple arguments, just call `setEnvironmentVariable` multiple times.
Expand Down
6 changes: 3 additions & 3 deletions src/DockerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public function doNotCleanUpAfterExit(): self
/**
* @param int|string $portOnHost
*/
public function mapPort($portOnHost, int $portOnDocker): self
public function mapPort($portOnHost, int $portOnDocker, string $protocol = 'tcp'): self
{
$this->portMappings[] = new PortMapping($portOnHost, $portOnDocker);
$this->portMappings[] = new PortMapping($portOnHost, $portOnDocker, $protocol);

return $this;
}
Expand Down Expand Up @@ -285,7 +285,7 @@ public function start(): DockerContainerInstance

$process->run();

if (! $process->isSuccessful()) {
if (!$process->isSuccessful()) {
throw CouldNotStartDockerContainer::processFailed($this, $process);
}

Expand Down
8 changes: 6 additions & 2 deletions src/PortMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ class PortMapping

private int $portOnDocker;

private string $protocol;

/**
* @param int|string $portOnHost
*/
public function __construct($portOnHost, int $portOnDocker)
public function __construct($portOnHost, int $portOnDocker, string $protocol = 'tcp')
{
$this->portOnHost = $portOnHost;

$this->portOnDocker = $portOnDocker;

$this->protocol = $protocol;
}

public function __toString()
{
return "-p {$this->portOnHost}:{$this->portOnDocker}";
return "-p {$this->portOnHost}:{$this->portOnDocker}/{$this->protocol}";
}
}
13 changes: 11 additions & 2 deletions tests/DockerContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
->mapPort(9000, 21)
->getStartCommand();

expect($command)->toEqual('docker run -p 4848:22 -p 9000:21 -d --rm spatie/docker');
expect($command)->toEqual('docker run -p 4848:22/tcp -p 9000:21/tcp -d --rm spatie/docker');
});

it('can map string ports', function () {
Expand All @@ -65,7 +65,16 @@
->mapPort('0.0.0.0:9000', 21)
->getStartCommand();

expect($command)->toEqual('docker run -p 127.0.0.1:4848:22 -p 0.0.0.0:9000:21 -d --rm spatie/docker');
expect($command)->toEqual('docker run -p 127.0.0.1:4848:22/tcp -p 0.0.0.0:9000:21/tcp -d --rm spatie/docker');
});

it('can map string ports with udp set', function () {
$command = $this->container
->mapPort('127.0.0.1:69', 69, 'udp')
->mapPort('0.0.0.0:69', 69, 'udp')
->getStartCommand();

expect($command)->toEqual('docker run -p 127.0.0.1:69:69/udp -p 0.0.0.0:69:69/udp -d --rm spatie/docker');
});

it('can set environment variables', function () {
Expand Down
10 changes: 8 additions & 2 deletions tests/PortMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

use Spatie\Docker\PortMapping;

it('should convert to a string correctly', function () {
it('should convert to a string correctly with tcp protocol as default', function () {
$portMapping = new PortMapping(8080, 80);

expect($portMapping)->toEqual('-p 8080:80');
expect($portMapping)->toEqual('-p 8080:80/tcp');
});

it('should convert to a string correctly with configured udp protocol', function () {
$portMapping = new PortMapping(8080, 80, 'udp');

expect($portMapping)->toEqual('-p 8080:80/udp');
});

0 comments on commit 7d41541

Please sign in to comment.