From 16fc7a009cebc9d0ab65797e3445671ad5a757c2 Mon Sep 17 00:00:00 2001 From: Stephen Stack Date: Fri, 24 May 2024 15:55:02 +0100 Subject: [PATCH 1/2] Add optional UDP protocol to port mapping with TCP as default protocol --- src/DockerContainer.php | 6 +++--- src/PortMapping.php | 8 ++++++-- tests/DockerContainerTest.php | 13 +++++++++++-- tests/PortMappingTest.php | 10 ++++++++-- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/DockerContainer.php b/src/DockerContainer.php index bbbc1ea..e099dcc 100644 --- a/src/DockerContainer.php +++ b/src/DockerContainer.php @@ -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; } @@ -285,7 +285,7 @@ public function start(): DockerContainerInstance $process->run(); - if (! $process->isSuccessful()) { + if (!$process->isSuccessful()) { throw CouldNotStartDockerContainer::processFailed($this, $process); } diff --git a/src/PortMapping.php b/src/PortMapping.php index cbb6fec..2840f71 100644 --- a/src/PortMapping.php +++ b/src/PortMapping.php @@ -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}"; } } diff --git a/tests/DockerContainerTest.php b/tests/DockerContainerTest.php index 0569d75..bf22528 100644 --- a/tests/DockerContainerTest.php +++ b/tests/DockerContainerTest.php @@ -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 () { @@ -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 () { diff --git a/tests/PortMappingTest.php b/tests/PortMappingTest.php index 994ce7d..8f1db27 100644 --- a/tests/PortMappingTest.php +++ b/tests/PortMappingTest.php @@ -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'); }); From febc5d0277966db6a0dc914eecfbfe3e16a4abc3 Mon Sep 17 00:00:00 2001 From: Stephen Stack Date: Mon, 27 May 2024 10:39:59 +0100 Subject: [PATCH 2/2] Update readme for Mapping ports UDP reference --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index da83627..137eb30 100644 --- a/README.md +++ b/README.md @@ -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.