mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(restart): keep the instance's port, image, data directory and services (#49)
* fix(restart): keep the instance's port, image, data directory and services Without a profile, restart reads the running container's settings (host port, image, the directory mounted at /app/data, its services and resource namespace) before stopping it and starts it with them, instead of falling back to the defaults and moving the instance to the default port without its state. A profile still decides when one is given, and restart now starts the container when none exists instead of failing. A container started without --persist has Docker's anonymous volume at /app/data; restart took that volume's internal path for a persist directory and failed to start again. Only a bind mount is a persist directory now. * fix(restart): stop before the stop when the running container's settings cannot be read A failed or timed-out inspect is no longer treated as a missing container, which removed the instance and started it on the defaults. Adds tests through call() for the missing-container, existing-container and unreadable-settings paths.
This commit is contained in:
1 parent
52ebdee98d
commit
f6cec794d7
4 files changed
+328
-5
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package io.floci.cli.docker;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.cli.GlobalOptions;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -9,7 +11,9 @@ import java.io.InputStreamReader;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -50,6 +54,62 @@ public class DockerClient {
|
||||
|
||||
public record ImageInfo(String id, String repository, String tag, String digest) {}
|
||||
|
||||
/**
|
||||
* What a container was started with, as {@code restart} needs it back: the image, the host
|
||||
* port bound to {@code containerPort}, the host directory bind-mounted at {@code /app/data}
|
||||
* (null for a docker volume), and the environment. Any of them may be absent (null, or an empty map).
|
||||
*/
|
||||
public record RunSettings(String image, Integer hostPort, String persistSource, Map<String, String> env) {}
|
||||
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
public Optional<RunSettings> inspectRunSettings(String name, int containerPort) throws DockerException {
|
||||
String out;
|
||||
try {
|
||||
out = run("docker", "container", "inspect", "--format", "{{json .}}", "--", name);
|
||||
} catch (DockerException e) {
|
||||
if (e.getMessage() != null && e.getMessage().toLowerCase().contains("no such")) {
|
||||
return Optional.empty();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
if (out.isBlank()) return Optional.empty();
|
||||
try {
|
||||
JsonNode c = JSON.readTree(out);
|
||||
String image = textOrNull(c.path("Config").path("Image"));
|
||||
Integer hostPort = null;
|
||||
JsonNode binding = c.path("HostConfig").path("PortBindings").path(containerPort + "/tcp");
|
||||
if (binding.isArray() && !binding.isEmpty()) {
|
||||
String text = binding.get(0).path("HostPort").asText("");
|
||||
if (!text.isBlank()) hostPort = Integer.valueOf(text);
|
||||
}
|
||||
String persist = null;
|
||||
for (JsonNode mount : c.path("Mounts")) {
|
||||
// Only a host directory is a --persist. The images declare /app/data as a volume,
|
||||
// so without --persist docker mounts an anonymous volume there; its Source is a
|
||||
// path inside the docker VM, not something start can mount again.
|
||||
if ("/app/data".equals(mount.path("Destination").asText())
|
||||
&& "bind".equals(mount.path("Type").asText())) {
|
||||
persist = textOrNull(mount.path("Source"));
|
||||
}
|
||||
}
|
||||
Map<String, String> env = new LinkedHashMap<>();
|
||||
for (JsonNode entry : c.path("Config").path("Env")) {
|
||||
String kv = entry.asText();
|
||||
int eq = kv.indexOf('=');
|
||||
if (eq > 0) env.put(kv.substring(0, eq), kv.substring(eq + 1));
|
||||
}
|
||||
return Optional.of(new RunSettings(image, hostPort, persist, Map.copyOf(env)));
|
||||
} catch (Exception e) {
|
||||
throw new DockerException("Could not read the settings of container '" + name + "': " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String textOrNull(JsonNode node) {
|
||||
String text = node.asText("");
|
||||
return text.isBlank() ? null : text;
|
||||
}
|
||||
|
||||
public String dockerVersion() throws DockerException {
|
||||
return run("docker", "version", "--format", "{{.Server.Version}}").trim();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user