Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional UDP protocol to port mapping class with TCP as default protocol #58

Merged
merged 2 commits into from
May 27, 2024
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
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');
});
Loading