fix(reset): keep a Docker daemon reached over SSH remote, loopback address or not

ssh://user@127.0.0.1:2222 is a forwarded port to a VM or another machine. Treating it as local would let reset --yes go to localhost on this machine.
Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-11 11:01:07 -05:00
1 parent d2a3756568
commit a71b0d0e0d
2 files changed
+21 -4

No files matched your search

@@ -190,14 +190,18 @@ public class ResetCommand implements Callable<Integer> {
}
/**
* 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.
* Whether the daemon runs on another machine. Docker reached over TCP 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. SSH is always remote: a loopback address there
* ({@code ssh://user@127.0.0.1:2222}) is a forwarded port to a VM or another machine, not a
* daemon on this one.
*/
public static boolean isRemote(DockerClient.DockerHost daemon) {
if (daemon.kind() != DockerClient.Kind.TCP) return false;
try {
String host = URI.create(daemon.raw()).getHost();
URI address = URI.create(daemon.raw());
if ("ssh".equalsIgnoreCase(address.getScheme())) return true;
String host = address.getHost();
return host == null || !thisMachine(host);
} catch (IllegalArgumentException | NullPointerException e) {
return true; // cannot tell: the careful answer
@@ -279,6 +279,8 @@ class ResetCommandTest {
"unix:///var/run/docker.sock, false",
"tcp://remote-host:2375, true",
"ssh://deploy@remote-host, true",
"ssh://user@127.0.0.1:2222, true",
"ssh://localhost, true",
"tcp://192.168.1.20:2375, true"})
void onlyADaemonOnAnotherMachineIsRemote(String dockerHost, boolean remote) {
assertEquals(remote, ResetCommand.isRemote(DockerClient.parseDockerHost(dockerHost, null, "Linux")));
@@ -298,6 +300,17 @@ class ResetCommandTest {
assertTrue(ResetCommand.sameServer("http://localhost:14566", "http://localhost:14566", localTcp));
}
/** SSH to a loopback address is a forwarded port to somewhere else: never reset through localhost. */
@Test
void anSshDaemonOnALoopbackAddressIsStillRemote() {
DockerHost forwarded = DockerClient.parseDockerHost("ssh://user@127.0.0.1:2222", null, "Linux");
ResetCommand.Target target = targetOf(dockerWith("floci", "running", "4566->4566/tcp", forwarded));
assertNull(target.endpoint());
assertTrue(target.error().contains("runs on the remote Docker host ssh://user@127.0.0.1:2222"), target.error());
}
/** A profile for an instance on a remote Docker host: its endpoint and its container are one server. */
@Test
void aRemoteInstanceWhoseEndpointMatchesIsReset() throws Exception {