From 8e38cb085825428aa5acd9ae2b82cd3a024af735 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Fri, 9 Oct 2026 09:14:20 -0500 Subject: [PATCH] feat(start): check the host port before starting and explain a missing Docker socket floci start now stops with 'Port N is already in use' and how to pick another port, instead of failing inside docker run, and when Docker cannot be reached because its local socket is missing it says how to bring Docker up. The port probe is skipped for a remote daemon (DOCKER_HOST=tcp://...). --- .../io/floci/cli/commands/StartCommand.java | 33 +++++++++- .../cli/doctor/checks/PortAvailableCheck.java | 2 +- .../io/floci/cli/unit/StartPreflightTest.java | 61 +++++++++++++++++++ 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/test/java/io/floci/cli/unit/StartPreflightTest.java diff --git a/src/main/java/io/floci/cli/commands/StartCommand.java b/src/main/java/io/floci/cli/commands/StartCommand.java index 3f63e08..6724406 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -5,6 +5,10 @@ import io.floci.cli.ProductProfile; import io.floci.cli.config.ProfileDefaultValueProvider; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; +import io.floci.cli.doctor.CheckResult; +import io.floci.cli.doctor.CheckStatus; +import io.floci.cli.doctor.checks.DockerSocketCheck; +import io.floci.cli.doctor.checks.PortAvailableCheck; import io.floci.cli.output.Ansi; import io.floci.cli.output.Printer; import picocli.CommandLine; @@ -29,11 +33,19 @@ public class StartCommand implements Callable { @Mixin protected GlobalOptions global; + private final DockerClient docker; + public StartCommand() { this(ProductProfile.AWS); } protected StartCommand(ProductProfile profile) { + this(profile, new DockerClient()); + } + + /** Test seam: {@code docker} answers every docker call this start makes. */ + public StartCommand(ProductProfile profile, DockerClient docker) { + this.docker = docker; this.profile = profile; this.global = new GlobalOptions(profile); this.port = profile.defaultPort(); @@ -192,7 +204,6 @@ public class StartCommand implements Callable { @Override public Integer call() { Printer printer = global.printer(); - DockerClient docker = new DockerClient(); String invalid = validationError(); if (invalid != null) { @@ -219,7 +230,16 @@ public class StartCommand implements Callable { printer.error("docker binary not found in PATH.\nInstall Docker Desktop from https://docs.docker.com/get-docker/"); return 1; } - printer.error("Failed to inspect container: " + e.getMessage()); + printer.error("Failed to inspect container: " + e.getMessage() + socketGuidance()); + return 1; + } + + // Checked here rather than left to 'docker run', whose bind error names neither the port + // flag nor how to find what holds the port. A remote daemon publishes on another machine, + // so a local probe would say nothing about it. + if (DockerClient.dockerHost().kind() != DockerClient.Kind.TCP && !PortAvailableCheck.isPortFree(port)) { + printer.error("Port " + port + " is already in use by another process.\n" + + "Run 'lsof -i :" + port + "' to find it, or pass --port to use a different port."); return 1; } @@ -260,4 +280,13 @@ public class StartCommand implements Callable { wait.timeout = "30s"; return wait.call(); } + + // When the daemon is unreachable because its local socket is missing, say so: docker's own + // error ("Cannot connect to the Docker daemon") does not say what to do about it. + private static String socketGuidance() { + CheckResult socket = new DockerSocketCheck().run(null, null); + return socket.status() == CheckStatus.fail + ? "\n" + socket.message() + ".\n" + socket.fix() + ", then re-run the command." + : ""; + } } diff --git a/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java b/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java index 545ac1c..d6cfe38 100644 --- a/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java @@ -46,7 +46,7 @@ public class PortAvailableCheck implements Check { "Run 'lsof -i :" + port + "' to identify the process, or start Floci with --port "); } - private static boolean isPortFree(int port) { + public static boolean isPortFree(int port) { try (ServerSocket s = new ServerSocket(port)) { return true; } catch (IOException e) { diff --git a/src/test/java/io/floci/cli/unit/StartPreflightTest.java b/src/test/java/io/floci/cli/unit/StartPreflightTest.java new file mode 100644 index 0000000..7fca838 --- /dev/null +++ b/src/test/java/io/floci/cli/unit/StartPreflightTest.java @@ -0,0 +1,61 @@ +package io.floci.cli.unit; + +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StartCommand; +import io.floci.cli.docker.DockerClient; +import org.junit.jupiter.api.Test; +import picocli.CommandLine; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +/** BL-058: start reports a busy host port itself, before pulling or running anything. */ +class StartPreflightTest { + + @Test + void aBusyPortStopsTheStartWithAHint() throws Exception { + assumeTrue(DockerClient.dockerHost().kind() != DockerClient.Kind.TCP, "a remote daemon skips the local probe"); + AtomicBoolean pulled = new AtomicBoolean(); + DockerClient docker = new DockerClient() { + @Override + public Optional inspectContainer(String name) { + return Optional.empty(); + } + + @Override + public void pull(String image, String policy) { + pulled.set(true); + } + }; + + try (ServerSocket taken = new ServerSocket()) { + taken.bind(new InetSocketAddress(0)); + int port = taken.getLocalPort(); + PrintStream out = System.out; + PrintStream err = System.err; + ByteArrayOutputStream errBuf = new ByteArrayOutputStream(); + System.setOut(new PrintStream(new ByteArrayOutputStream())); + System.setErr(new PrintStream(errBuf)); + int exit; + try { + exit = new CommandLine(new StartCommand(ProductProfile.GCP, docker)) + .execute("--port", String.valueOf(port), "--container", "floci-gcp-preflight"); + } finally { + System.setOut(out); + System.setErr(err); + } + + assertEquals(1, exit); + assertTrue(errBuf.toString().contains("Port " + port + " is already in use"), errBuf.toString()); + assertTrue(errBuf.toString().contains("--port "), errBuf.toString()); + assertFalse(pulled.get(), "nothing is pulled for a start that cannot bind"); + } + } +}