From d2a37565689a6647b71c1659c6548ccd70ab44a5 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sun, 11 Oct 2026 10:36:43 -0500 Subject: [PATCH] fix(reset): treat a Docker daemon on this machine reached over TCP as local tcp://127.0.0.1:2375 is this machine's daemon, so its published ports are local and a reset goes ahead as with a socket. The README sentence about exiting 1 now covers only the cases where reset refuses. Signed-off-by: Hector Ventura --- README.md | 7 ++-- .../io/floci/cli/commands/ResetCommand.java | 38 ++++++++++++++----- .../io/floci/cli/unit/ResetCommandTest.java | 28 ++++++++++++++ 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 913a8c6..aa9bc92 100644 --- a/README.md +++ b/README.md @@ -733,10 +733,11 @@ Reset never guesses which server to wipe: container that happens to run locally under the default name never takes its place. - When both a container and an endpoint are configured, they have to be the same server. If they are not, nothing is reset and you are asked to pass `--endpoint` or `--container`. -- A container on a remote Docker host (`DOCKER_HOST=tcp://...` or `ssh://...`) is only reset - through an endpoint, since its published port is not on this machine. +- A container on a remote Docker host (`DOCKER_HOST=tcp://other-host:...` or `ssh://...`) is only + reset through an endpoint, since its published port is not on this machine. A daemon on this + machine reached over TCP (`tcp://127.0.0.1:2375`) is local. -In each of those cases the command exits 1 without sending anything. +When reset refuses to choose a server, the command exits 1 without sending anything. ### `floci config` diff --git a/src/main/java/io/floci/cli/commands/ResetCommand.java b/src/main/java/io/floci/cli/commands/ResetCommand.java index f2c24f1..d6f6839 100644 --- a/src/main/java/io/floci/cli/commands/ResetCommand.java +++ b/src/main/java/io/floci/cli/commands/ResetCommand.java @@ -155,7 +155,7 @@ public class ResetCommand implements Callable { } DockerClient.DockerHost daemon = docker.daemonHost(); - boolean remote = daemon.kind() == DockerClient.Kind.TCP; + boolean remote = isRemote(daemon); if (endpointNamed && !typedContainer) { // A container and an endpoint were both configured: they have to be one server. if (sameServer(published.get(), global.endpoint, daemon)) return new Target(global.endpoint, null); @@ -184,28 +184,48 @@ public class ResetCommand implements Callable { } private static String whereItRuns(String published, DockerClient.DockerHost daemon) { - return daemon.kind() == DockerClient.Kind.TCP + return isRemote(daemon) ? "port " + URI.create(published).getPort() + " of the remote Docker host " + daemon.raw() : published; } + /** + * Whether the daemon runs on another machine. Docker reached over TCP or SSH usually does, + * but {@code tcp://127.0.0.1:2375} is this machine's daemon on a socket of another kind, and + * its published ports are local like any other's. + */ + public static boolean isRemote(DockerClient.DockerHost daemon) { + if (daemon.kind() != DockerClient.Kind.TCP) return false; + try { + String host = URI.create(daemon.raw()).getHost(); + return host == null || !thisMachine(host); + } catch (IllegalArgumentException | NullPointerException e) { + return true; // cannot tell: the careful answer + } + } + + private static boolean thisMachine(String host) { + String name = host.toLowerCase(Locale.ROOT); + return name.equals("localhost") || name.equals("127.0.0.1") || name.equals("[::1]") || name.equals("::1") + || name.equals("0.0.0.0") || name.equals("localhost.floci.io"); + } + /** * Whether {@code configured} is the server a container publishes at {@code published} - * ({@code http://localhost:} as docker reports it). The port has to match. For a local - * daemon the configured host has to mean this machine; for a remote one it has to be the - * daemon's host, since that is where the port was published. + * ({@code http://localhost:} as docker reports it). The port has to match. For a daemon + * on this machine the configured host has to mean this machine; for a remote one it has to be + * the daemon's host, since that is where the port was published. */ public static boolean sameServer(String published, String configured, DockerClient.DockerHost daemon) { try { URI there = URI.create(withoutTrailingSlashes(configured)); if (URI.create(published).getPort() != there.getPort()) return false; - String host = there.getHost() == null ? "" : there.getHost().toLowerCase(Locale.ROOT); - if (daemon.kind() == DockerClient.Kind.TCP) { + String host = there.getHost() == null ? "" : there.getHost(); + if (isRemote(daemon)) { String daemonHost = URI.create(daemon.raw()).getHost(); return daemonHost != null && daemonHost.equalsIgnoreCase(host); } - return host.equals("localhost") || host.equals("127.0.0.1") || host.equals("[::1]") - || host.equals("::1") || host.equals("0.0.0.0") || host.equals("localhost.floci.io"); + return thisMachine(host); } catch (IllegalArgumentException | NullPointerException e) { return false; } diff --git a/src/test/java/io/floci/cli/unit/ResetCommandTest.java b/src/test/java/io/floci/cli/unit/ResetCommandTest.java index e64ccfd..77bec5f 100644 --- a/src/test/java/io/floci/cli/unit/ResetCommandTest.java +++ b/src/test/java/io/floci/cli/unit/ResetCommandTest.java @@ -270,6 +270,34 @@ class ResetCommandTest { assertEquals(same, ResetCommand.sameServer("http://localhost:14566", endpoint, remote)); } + /** Docker on this machine reached over TCP is local: its published ports are this machine's. */ + @ParameterizedTest + @CsvSource({ + "tcp://127.0.0.1:2375, false", + "tcp://localhost:2375, false", + "tcp://[::1]:2376, false", + "unix:///var/run/docker.sock, false", + "tcp://remote-host:2375, true", + "ssh://deploy@remote-host, true", + "tcp://192.168.1.20:2375, true"}) + void onlyADaemonOnAnotherMachineIsRemote(String dockerHost, boolean remote) { + assertEquals(remote, ResetCommand.isRemote(DockerClient.parseDockerHost(dockerHost, null, "Linux"))); + } + + @Test + void aLocalTcpDaemonResetsItsContainerLikeASocketOne() throws Exception { + DockerHost localTcp = DockerClient.parseDockerHost("tcp://127.0.0.1:2375", null, "Linux"); + + ResetCommand.Target plain = targetOf(dockerWith("floci", "running", "4566->4566/tcp", localTcp)); + ResetCommand.Target withProfile = targetWithProfile(dockerWith("floci", "running", "14566->4566/tcp", localTcp), + "container: floci\nport: 14566\nendpoint: http://localhost:14566\n"); + + assertEquals("http://localhost:4566", plain.endpoint()); + assertNull(withProfile.error(), withProfile.error()); + assertEquals("http://localhost:14566", withProfile.endpoint()); + assertTrue(ResetCommand.sameServer("http://localhost:14566", "http://localhost:14566", localTcp)); + } + /** A profile for an instance on a remote Docker host: its endpoint and its container are one server. */ @Test void aRemoteInstanceWhoseEndpointMatchesIsReset() throws Exception {