Skip to content

Exercises pick their port up front, docker picks where it is published - #343

Merged
AydinHassan merged 8 commits into
masterfrom
exposed-ports
Nov 1, 2026
Merged

AydinHassan merged 8 commits into
masterfrom
exposed-ports

Conversation

@AydinHassan

@AydinHassan AydinHassan commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Step 2 of the simpler sandbox plan (#336): every run should go through the same container workflow, so a port must be known before the container is made.

  • An exercise picks its port once, in defineTestScenario(), with FreePortPicker. It is free on this machine, and every port is free in a fresh sandbox container, so the CLI on a laptop never clashes.
  • DockerSandbox publishes it on a host port from a PortPicker (constructor option): -p <portsHost>:<picked>:<port>. The default is FreePortPicker, right when docker runs on this machine. LockPortPicker claims a random port with a symfony/lock lock (new dependency) for a docker host shared by many verifies: phpschool.io passes it a LockFactory on its Redis.
  • SandboxExecution::getAddress(int $port) says where to connect, eg. 127.0.0.1:54321 on the host or 10.0.0.5:47213 in docker (the execution keeps the host once and a port map) (pass an IPv6 portsHost in brackets, eg. [fdaa::2]).
  • The CLI executing events carry the execution: $event->getExecution()->getAddress($port).
  • SandboxHost, Sandbox::getHost(), ExecutionContext::getSandboxHost() and the dispatcher's sandbox argument go.

Time Server follows in learn-you-php. Checked with its verify (correct, no server, wrong output) and run, with SANDBOX=docker and on the host.

A port free on this machine is free in a sandbox container too, so exercises pick it once with System::freePort() and docker publishes it on a free host port. Checks ask the execution where to connect, from the executing event. SandboxHost goes, so nothing asks this machine about ports on the docker host.
DockerSandbox picks the published port itself, random by default, so it knows the address without a docker port lookup. A shared docker host can pass a picker that remembers ports in use.
…domPortPicker

FreePortPicker is a port free on this machine: exercises use it, and DockerSandbox by default. LockPortPicker claims a random port with a symfony/lock lock, for a docker host shared by many runs.
@AydinHassan
AydinHassan merged commit 58b8e53 into master Nov 1, 2026
4 checks passed
AydinHassan added a commit to php-school/phpschool.io that referenced this pull request Nov 1, 2026
Uses php-workshop's `LockPortPicker`
(php-school/php-workshop#343):
`CloudWorkshopRepository` passes it to each workshop's `DockerSandbox`,
with a `LockFactory` on our Redis, so verifies sharing the docker host
never publish on the same port. The picker is required, so it can't be
forgotten.

Also updates php-workshop, which brings in symfony/lock.

Checked against a throwaway Redis: picked ports are locked for 60s and a
second claim of the same port is refused.
@AydinHassan AydinHassan mentioned this pull request Nov 1, 2026
3 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant