fix(ecr): Properly check if host-persistent-path is the name of a Docker volume (#678)

This commit is contained in:
Christian Franzen authored and GitHub committed 2026-05-01 17:15:21 -05:00
1 parent 91ed829ea0
commit aaa1998508
3 files changed
+143 -4

No files matched your search

@@ -5,6 +5,7 @@ import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Container;
@@ -302,6 +303,42 @@ public class ContainerLifecycleManager {
return dockerClient;
}
/**
* Returns {@code true} if the container runtime (Docker, Moby, or Podman) has a volume
* with the given name. The volume does not need to be attached to the current container.
* <p>
* This method uses the Docker Engine API ({@code /volumes/{name}}) which is supported
* by Docker, Moby, and Podman runtimes on all operating systems.
*
* @param name the volume name to look up
* @return {@code true} if the volume exists, {@code false} otherwise
*/
public boolean volumeExists(String name) {
if (name == null || name.isBlank()) {
return false;
}
// Is a Unix absolute or relative path (e.g. "/var/lib/data", "./data", "../data")
if (name.startsWith("/") || name.startsWith(".")) {
return false;
}
// Is a Windows absolute path (e.g. "C:\Users\data", "D:/sources/data")
if (name.length() >= 3 && Character.isLetter(name.charAt(0))
&& name.charAt(1) == ':' && (name.charAt(2) == '\\' || name.charAt(2) == '/')) {
return false;
}
try {
dockerClient.inspectVolumeCmd(name).exec();
LOG.debugv("Volume ''{0}'' exists in the container runtime", name);
return true;
} catch (NotFoundException e) {
LOG.debugv("Volume ''{0}'' not found in the container runtime", name);
return false;
} catch (DockerException e) {
LOG.warnv("Failed to inspect volume ''{0}'': {1}", name, e.getMessage());
return false;
}
}
private HostConfig buildHostConfig(ContainerSpec spec) {
HostConfig hostConfig = HostConfig.newHostConfig();
@@ -180,8 +180,7 @@ public class EcrRegistryManager {
private void addPersistenceMounts(ContainerBuilder.Builder specBuilder, List<String> env) {
String hostPersistentPath = config.storage().hostPersistentPath();
boolean inContainer = containerDetector.isRunningInContainer();
boolean isExplicitVolumeName = !hostPersistentPath.startsWith("/")
&& !hostPersistentPath.startsWith(".");
boolean isExplicitVolume = lifecycleManager.volumeExists(hostPersistentPath);
boolean isRelativeDefault = hostPersistentPath.startsWith(".");
if (inContainer && isRelativeDefault) {
@@ -190,10 +189,10 @@ public class EcrRegistryManager {
LOG.infov("Floci in container with relative host-persistent-path ({0}); "
+ "using named volume {1} for ECR registry data",
hostPersistentPath, NAMED_VOLUME);
} else if (isExplicitVolumeName) {
} else if (isExplicitVolume) {
// User set hostPersistentPath to a Docker named-volume name
String internalMountPath = "/app/data";
specBuilder.withBind(hostPersistentPath, internalMountPath);
specBuilder.withNamedVolume(hostPersistentPath, internalMountPath);
env.add("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=" + internalMountPath + "/ecr/registry");
} else {
// Host path bind-mount.