fix(docker): launch an image in Floci's own ECR without the registry base (#8243)

With floci.docker.image-registry-base set, ContainerBuilder.resolveImage put
the base in front of every image, so an ECR image Floci had rewritten to its
loopback registry (localhost:<port>/<account>/<region>/<repo>), or a
repositoryUri it handed out, became <base>/localhost:<port>/...: a reference
Docker refuses, and no image a mirror holds. An ECS task, an image-backed
Lambda function or an EMR Serverless application with a custom image could not
run from Floci's ECR with the base set.

- An image in a registry on the Docker host's loopback (localhost, a
  .localhost name, Floci's localhost.floci.io alias or a loopback address in
  any spelling, checked with NetworkExposureGuard.isLoopback, with or without
  a port) is launched as it is; every other image still goes under the base.
- The setting's Javadoc, its environment variable row and the ECR page say so.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-13 22:28:27 -04:00
1 parent 3ae36eb7be
commit 3e02ff995f
5 files changed
+68 -4

No files matched your search

@@ -4187,7 +4187,8 @@ public interface EmulatorConfig {
* Optional registry/repository base for every Docker image Floci launches.
* When set, images such as {@code postgres:16-alpine} and
* {@code public.ecr.aws/docker/library/ubuntu:24.04} resolve under this
* base before the container is created.
* base before the container is created. An image in a registry on the Docker
* host's loopback, Floci's own ECR among them, is launched as it is.
*/
Optional<String> imageRegistryBase();
@@ -10,6 +10,7 @@ import com.github.dockerjava.api.model.MountType;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.api.model.VolumesFrom;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.config.NetworkExposureGuard;
import io.floci.aws.core.common.dns.EmbeddedDnsServer;
import io.floci.aws.services.lambda.launcher.ImageCacheService;
import jakarta.enterprise.context.ApplicationScoped;
@@ -18,8 +19,11 @@ import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Fluent builder for constructing {@link ContainerSpec} instances.
@@ -41,6 +45,9 @@ public class ContainerBuilder {
private static final String HOST_DOCKER_INTERNAL = "host.docker.internal";
/** The registry host an image names before its first {@code /}, a name or a bracketed IPv6 address, and a port. */
private static final Pattern REGISTRY_HOST = Pattern.compile("(\\[[^\\]/]+\\]|[^/:\\[]+)(?::[0-9]+)?/");
private final EmulatorConfig config;
private final DockerHostResolver dockerHostResolver;
private final EmbeddedDnsServer embeddedDnsServer;
@@ -101,12 +108,17 @@ public class ContainerBuilder {
: Optional.empty());
}
/**
* The reference an image is launched under: {@code floci.docker.image-registry-base} in front of it, unless it is
* already under that base, is an image id, or is in a registry on the Docker host's loopback (Floci's own ECR
* among them), which no mirror holds.
*/
public String resolveImage(String image) {
return resolveImage(image, configuredImageRegistryBase(config));
}
static String resolveImage(String image, Optional<String> imageRegistryBase) {
if (image == null || image.isBlank() || ImageCacheService.isImageId(image)) {
if (image == null || image.isBlank() || ImageCacheService.isImageId(image) || inLoopbackRegistry(image)) {
return image;
}
return normalizeImageRegistryBase(imageRegistryBase)
@@ -115,6 +127,22 @@ public class ContainerBuilder {
.orElse(image);
}
/**
* Whether an image is in a registry on the Docker host's loopback: {@code localhost}, a {@code .localhost} name,
* Floci's {@code localhost.floci.io} alias or a loopback address in any spelling, with or without a port. Floci's
* ECR hands its images out, and rewrites AWS-shaped ECR URIs to, {@code localhost:<port>/<account>/<region>/<repo>}
* or {@code <account>.dkr.ecr.<region>.localhost[.floci.io]:<port>/<repo>}.
*/
private static boolean inLoopbackRegistry(String image) {
Matcher registry = REGISTRY_HOST.matcher(image);
if (!registry.lookingAt()) {
return false;
}
String host = registry.group(1).toLowerCase(Locale.ROOT);
return host.endsWith(".localhost") || host.equals("localhost.floci.io") || host.endsWith(".localhost.floci.io")
|| NetworkExposureGuard.isLoopback(host);
}
private static Optional<String> configuredImageRegistryBase(EmulatorConfig config) {
if (config == null || config.docker() == null) {
return Optional.empty();