mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
feat(start): check the host port before starting and explain a missing Docker socket (#51)
* 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://...). * feat(start): ask docker which daemon it uses before probing the port The port probe and the socket hint follow the daemon docker resolved (DOCKER_HOST, else the active context), a number that is no port is not probed, and a missing socket gets a start-the-daemon hint instead of a permissions one. Adds tests for the free port, remote daemon, socket guidance and context paths. * feat(start): treat a daemon reached over ssh as remote An ssh:// DOCKER_HOST or context endpoint was read as a local socket path, so a busy local port could block a start on another machine. * feat(start): resolve the daemon once for the persist directory and the bind source Start asks docker which daemon it uses once and applies that answer to the persist directory and the bind source too, so a daemon that is remote only through its context is treated like DOCKER_HOST=tcp://. Signed-off-by: Hector Ventura <hectorvent@gmail.com> * feat(start): let docker decide on a low port the user cannot bind A failed bind below 1024 can mean the user lacks the right to bind it, while the Docker daemon can publish it. Such a port now counts as taken only when something answers on it. Signed-off-by: Hector Ventura <hectorvent@gmail.com> * feat(start): keep an address-in-use error as proof a low port is taken The listener fallback now applies only when the bind failed for another reason, so a low port held on a non-loopback address is still reported as taken. Signed-off-by: Hector Ventura <hectorvent@gmail.com> --------- Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
3ca4108498
commit
4cd39a957a
5 files changed
+410
-11
No files matched your search
@@ -356,7 +356,7 @@ public class DockerClient {
|
||||
}
|
||||
}
|
||||
|
||||
/** How the Docker daemon is reached, resolved from the environment. */
|
||||
/** How the Docker daemon is reached. {@code TCP} is any remote daemon: tcp, http(s) or ssh. */
|
||||
public enum Kind { UNIX, TCP, NPIPE }
|
||||
|
||||
/**
|
||||
@@ -388,7 +388,10 @@ public class DockerClient {
|
||||
if (value.startsWith("unix://")) {
|
||||
return new DockerHost(Kind.UNIX, value.substring("unix://".length()), value);
|
||||
}
|
||||
if (value.startsWith("tcp://") || value.startsWith("http://") || value.startsWith("https://")) {
|
||||
// ssh:// is as remote as tcp://: the daemon, its ports and its bind paths are on
|
||||
// another machine. Read as a bare path it would have been taken for a local socket.
|
||||
if (value.startsWith("tcp://") || value.startsWith("http://") || value.startsWith("https://")
|
||||
|| value.startsWith("ssh://")) {
|
||||
return new DockerHost(Kind.TCP, null, value);
|
||||
}
|
||||
if (value.startsWith("npipe://")) {
|
||||
@@ -408,6 +411,26 @@ public class DockerClient {
|
||||
return new DockerHost(Kind.UNIX, DEFAULT_UNIX_SOCKET, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The daemon the docker CLI will actually talk to. {@link #dockerHost()} only reads the
|
||||
* environment, which does not show a context selected with {@code DOCKER_CONTEXT} or
|
||||
* {@code docker context use}; docker itself reports the endpoint it resolved. An explicit
|
||||
* {@code DOCKER_HOST} wins over any context, so it is answered without asking.
|
||||
*/
|
||||
public DockerHost daemonHost() {
|
||||
DockerHost fromEnvironment = dockerHost();
|
||||
if (fromEnvironment.raw() != null) return fromEnvironment;
|
||||
try {
|
||||
String endpoint = run("docker", "context", "inspect", "--format", "{{.Endpoints.docker.Host}}").trim();
|
||||
if (!endpoint.isBlank()) {
|
||||
return parseDockerHost(endpoint, null, System.getProperty("os.name", ""));
|
||||
}
|
||||
} catch (DockerException e) {
|
||||
// Podman, or a docker without contexts: the environment is all there is.
|
||||
}
|
||||
return fromEnvironment;
|
||||
}
|
||||
|
||||
/** Back-compat accessor for the resolved local socket/pipe path (null for TCP). */
|
||||
public static String socketPath() {
|
||||
return dockerHost().socketPath();
|
||||
|
||||
Reference in new issue
Block a user