mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
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>
This commit is contained in:
2 files changed
+33
-5
No files matched your search
@@ -179,7 +179,8 @@ public class StartCommand implements Callable<Integer> {
|
||||
* a directory it cannot write to itself.
|
||||
*/
|
||||
public String preparePersistDir() {
|
||||
return preparePersistDir(DockerClient.dockerHost());
|
||||
// The daemon docker resolved, so a remote context counts as remote here too.
|
||||
return preparePersistDir(docker.daemonHost());
|
||||
}
|
||||
|
||||
/** {@link #preparePersistDir()} for a daemon reached through {@code daemon}; the test seam. */
|
||||
@@ -301,7 +302,11 @@ public class StartCommand implements Callable<Integer> {
|
||||
return 2;
|
||||
}
|
||||
|
||||
String unwritable = preparePersistDir();
|
||||
// Asked once: the persist directory, the port probe and the bind source all turn on
|
||||
// whether the daemon docker will talk to is on this machine.
|
||||
DockerClient.DockerHost daemon = docker.daemonHost();
|
||||
|
||||
String unwritable = preparePersistDir(daemon);
|
||||
if (unwritable != null) {
|
||||
printer.error(unwritable);
|
||||
return 1;
|
||||
@@ -326,14 +331,14 @@ public class StartCommand implements Callable<Integer> {
|
||||
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() + socketGuidance(docker.daemonHost()));
|
||||
printer.error("Failed to inspect container: " + e.getMessage() + socketGuidance(daemon));
|
||||
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 (probesPortLocally(docker.daemonHost(), port) && !PortAvailableCheck.isPortFree(port)) {
|
||||
if (probesPortLocally(daemon, port) && !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 <other> to use a different port.");
|
||||
return 1;
|
||||
@@ -348,7 +353,7 @@ public class StartCommand implements Callable<Integer> {
|
||||
return 1;
|
||||
}
|
||||
|
||||
List<String> args = dockerRunArgs(DockerClient.dockerSocketRunArgs(), DockerClient.dockerHost());
|
||||
List<String> args = dockerRunArgs(DockerClient.dockerSocketRunArgs(), daemon);
|
||||
|
||||
try {
|
||||
printer.println("Starting " + Ansi.gold(profile.displayName()) + " container...");
|
||||
|
||||
@@ -38,6 +38,7 @@ class StartPreflightTest {
|
||||
private static class FakeDocker extends DockerClient {
|
||||
final DockerHost daemon;
|
||||
final List<String> calls = new ArrayList<>();
|
||||
final List<String> runArgs = new ArrayList<>();
|
||||
|
||||
FakeDocker(DockerHost daemon) {
|
||||
this.daemon = daemon;
|
||||
@@ -60,6 +61,7 @@ class StartPreflightTest {
|
||||
|
||||
@Override
|
||||
public String startContainer(List<String> args) {
|
||||
runArgs.addAll(args);
|
||||
calls.add("run");
|
||||
return "0123456789abcdef";
|
||||
}
|
||||
@@ -147,6 +149,27 @@ class StartPreflightTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One answer for the whole start: a daemon that is remote only through its context also gets
|
||||
* its persist path passed on as given, and a relative one refused, as with DOCKER_HOST=tcp://.
|
||||
*/
|
||||
@Test
|
||||
void aRemoteContextDecidesThePersistPathToo() {
|
||||
FakeDocker docker = new FakeDocker(REMOTE);
|
||||
|
||||
Out ok = start(docker, "--persist", "/srv/floci-data", "--container", "floci-gcp-preflight", "--detach");
|
||||
|
||||
assertEquals(0, ok.exit(), ok.err());
|
||||
assertTrue(docker.runArgs.contains("/srv/floci-data:/app/data"), docker.runArgs.toString());
|
||||
|
||||
FakeDocker refused = new FakeDocker(REMOTE);
|
||||
Out relative = start(refused, "--persist", "data", "--container", "floci-gcp-preflight", "--detach");
|
||||
|
||||
assertEquals(1, relative.exit());
|
||||
assertTrue(relative.err().contains("must be an absolute path on the daemon's machine"), relative.err());
|
||||
assertEquals(List.of(), refused.calls);
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyARealPortOnALocalDaemonIsProbed() {
|
||||
assertTrue(StartCommand.probesPortLocally(LOCAL, 4588));
|
||||
|
||||
Reference in new issue
Block a user