From 3e02ff995f80c72ddaa1700990e909320fce4184 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:28:27 -0400 Subject: [PATCH] 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:///), or a repositoryUri it handed out, became /localhost:/...: 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. --- docs/configuration/environment-variables.md | 2 +- docs/services/ecr.md | 2 +- .../io/floci/aws/config/EmulatorConfig.java | 3 +- .../core/common/docker/ContainerBuilder.java | 30 +++++++++++++++- .../common/docker/ContainerBuilderTest.java | 35 +++++++++++++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index f089ab5ca9..2740625557 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -119,7 +119,7 @@ This foundation release provides opt-in security-group filtering for EC2 Docker | `FLOCI_DOCKER_DOCKER_CONFIG_PATH` | _(none)_ | Path to a directory containing Docker's `config.json` for registry auth | | `FLOCI_DOCKER_MAX_CONNECTIONS` | `1024` | Connection pool size for Floci's Docker client. Each live Lambda container holds one connection (its exit watcher) plus one per extension; log streams use the streaming pool. See [Docker Configuration](docker.md#connection-pool) | | `FLOCI_DOCKER_STREAMING_MAX_CONNECTIONS` | `512` | Connection pool size for container log-follow streams, kept apart from the main pool so they never starve create, start, stop and remove. See [Docker Configuration](docker.md#connection-pool) | -| `FLOCI_DOCKER_IMAGE_REGISTRY_BASE` | _(none)_ | Optional registry/repository base for every Docker image Floci launches. When set, `postgres:16-alpine` resolves as `/postgres:16-alpine` and `public.ecr.aws/docker/library/ubuntu:24.04` resolves as `/public.ecr.aws/docker/library/ubuntu:24.04` | +| `FLOCI_DOCKER_IMAGE_REGISTRY_BASE` | _(none)_ | Optional registry/repository base for every Docker image Floci launches. When set, `postgres:16-alpine` resolves as `/postgres:16-alpine` and `public.ecr.aws/docker/library/ubuntu:24.04` resolves as `/public.ecr.aws/docker/library/ubuntu:24.04`. An image in a registry on `localhost` or a loopback address, Floci's own ECR among them, is launched as it is | | `FLOCI_DOCKER_LOG_MAX_SIZE` | `10m` | Log rotation max size for spawned containers (e.g. `10m`, `1g`) | | `FLOCI_DOCKER_LOG_MAX_FILE` | `3` | Number of rotated log files to keep for spawned containers | | `FLOCI_DOCKER_RESOURCE_NAMESPACE` | _(none)_ | Optional namespace prefix for managed child Docker container and volume names | diff --git a/docs/services/ecr.md b/docs/services/ecr.md index ee3b9fc780..38fea8849a 100644 --- a/docs/services/ecr.md +++ b/docs/services/ecr.md @@ -97,7 +97,7 @@ the optional `filter` fields. - **Cross-account / cross-region isolation.** Internally the registry namespaces repositories as `//`, so the same repository name in different accounts or regions cannot collide. - **Reconcile on first start.** When the registry container starts, Floci queries `GET /v2/_catalog` and recreates `Repository` metadata entries for any namespaces present in the registry but missing from local storage. This means image bytes are never orphaned across restarts. - **Upgrade from direct registry access.** Floci recreates an older backing container with the same volume and a loopback-only port binding. Repositories written through the former direct endpoint retain their physical paths and map to the default account and Region; their returned URI moves to port `4566`. -- **Lambda, ECS and EMR Serverless integration.** Image-backed Lambda functions (`PackageType=Image`), ECS task containers and the custom images of EMR Serverless applications (in its `docker` runner mode) reference the same loopback `repositoryUri`. Floci rewrites real-AWS-shaped `.dkr.ecr..amazonaws.com/...` URIs to the loopback registry at pull time, path style on `localhost` (`localhost:///`) in either URI style and with or without TLS, an address a Linux Docker daemon pulls from over plain HTTP. On Docker Desktop the rewrite is `.dkr.ecr..localhost:/` instead: Desktop's daemon runs in a VM and dials `localhost` there, where a host port Docker chose for Floci is not bound, while it sends `*.localhost` through its host proxy to the port Floci publishes. Floci asks the daemon once whether it is Docker Desktop. CDK's `DockerImageFunction` (which generates AWS-shaped URIs in CloudFormation templates) works without any user-side rewriting. +- **Lambda, ECS and EMR Serverless integration.** Image-backed Lambda functions (`PackageType=Image`), ECS task containers and the custom images of EMR Serverless applications (in its `docker` runner mode) reference the same loopback `repositoryUri`. Floci rewrites real-AWS-shaped `.dkr.ecr..amazonaws.com/...` URIs to the loopback registry at pull time, path style on `localhost` (`localhost:///`) in either URI style and with or without TLS, an address a Linux Docker daemon pulls from over plain HTTP. On Docker Desktop the rewrite is `.dkr.ecr..localhost:/` instead: Desktop's daemon runs in a VM and dials `localhost` there, where a host port Docker chose for Floci is not bound, while it sends `*.localhost` through its host proxy to the port Floci publishes. Floci asks the daemon once whether it is Docker Desktop. CDK's `DockerImageFunction` (which generates AWS-shaped URIs in CloudFormation templates) works without any user-side rewriting. With `FLOCI_DOCKER_IMAGE_REGISTRY_BASE` set, the rewritten reference, and a loopback `repositoryUri` named directly, are still pulled from Floci's registry: the base goes in front of the images a mirror holds, never of one on `localhost`. - **Locally built images win.** An AWS-shaped URI that names an image already present on the Docker daemon is used as-is instead of being rewritten (`FLOCI_SERVICES_ECR_PREFER_LOCAL_IMAGES`, default `true`). Build the image under the exact URI your function or task definition declares (`docker build -t .dkr.ecr..amazonaws.com/: .`) and it runs without a push to the emulated registry, and without the registry container being started. The check uses the reference Floci would launch, so with `FLOCI_DOCKER_IMAGE_REGISTRY_BASE` set the image must be present as `/.dkr.ecr..amazonaws.com/:`. ## Pull-time update exclusions diff --git a/src/main/java/io/floci/aws/config/EmulatorConfig.java b/src/main/java/io/floci/aws/config/EmulatorConfig.java index fe90a827d0..114ff0dc07 100644 --- a/src/main/java/io/floci/aws/config/EmulatorConfig.java +++ b/src/main/java/io/floci/aws/config/EmulatorConfig.java @@ -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 imageRegistryBase(); diff --git a/src/main/java/io/floci/aws/core/common/docker/ContainerBuilder.java b/src/main/java/io/floci/aws/core/common/docker/ContainerBuilder.java index e47a9421cd..20ae72ef1b 100644 --- a/src/main/java/io/floci/aws/core/common/docker/ContainerBuilder.java +++ b/src/main/java/io/floci/aws/core/common/docker/ContainerBuilder.java @@ -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 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:///} + * or {@code .dkr.ecr..localhost[.floci.io]:/}. + */ + 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 configuredImageRegistryBase(EmulatorConfig config) { if (config == null || config.docker() == null) { return Optional.empty(); diff --git a/src/test/java/io/floci/aws/core/common/docker/ContainerBuilderTest.java b/src/test/java/io/floci/aws/core/common/docker/ContainerBuilderTest.java index 5a04add71e..9c92fead0d 100644 --- a/src/test/java/io/floci/aws/core/common/docker/ContainerBuilderTest.java +++ b/src/test/java/io/floci/aws/core/common/docker/ContainerBuilderTest.java @@ -528,6 +528,41 @@ class ContainerBuilderTest { assertEquals(imageId, fixture.builder.newContainer(imageId).build().image()); } + @ParameterizedTest + @ValueSource(strings = { + "localhost:4566/000000000000/us-east-1/spark/custom:7.5.0", + "000000000000.dkr.ecr.us-east-1.localhost:4566/spark/custom:7.5.0", + "000000000000.dkr.ecr.us-east-1.localhost.floci.io:4566/spark/custom:7.5.0", + "localhost.floci.io:4566/000000000000/us-east-1/spark/custom:7.5.0"}) + void imageRegistryBaseLeavesAnImageInFlocisOwnEcrAlone(String image) { + TestFixture fixture = new TestFixture(); + when(fixture.docker.imageRegistryBase()).thenReturn(Optional.of("ghcr.io/floci-io/mirror")); + + assertEquals(image, fixture.builder.resolveImage(image)); + assertEquals(image, fixture.builder.newContainer(image).build().image()); + } + + @ParameterizedTest + @ValueSource(strings = {"localhost/team/app:1", "LOCALHOST:5000/team/app:1", "registry.LOCALHOST:5000/team/app:1", + "127.0.0.1:5000/team/app:1", + "127.1.2.3:5000/team/app:1", "[::1]:5000/team/app:1", "[0:0:0:0:0:0:0:1]:5000/team/app:1"}) + void imageRegistryBaseLeavesAnImageInAnotherLoopbackRegistryAlone(String image) { + TestFixture fixture = new TestFixture(); + when(fixture.docker.imageRegistryBase()).thenReturn(Optional.of("ghcr.io/floci-io/mirror")); + + assertEquals(image, fixture.builder.newContainer(image).build().image()); + } + + @ParameterizedTest + @ValueSource(strings = {"localhost:16", "team/localhost:16", "localhost.example.com/team/app:1", + "mylocalhost.example.com/team/app:1", "127.0.0.1.nip.io/team/app:1"}) + void imageRegistryBasePrefixesAnImageOnlyNamedLikeALoopbackOne(String image) { + TestFixture fixture = new TestFixture(); + when(fixture.docker.imageRegistryBase()).thenReturn(Optional.of("ghcr.io/floci-io/mirror")); + + assertEquals("ghcr.io/floci-io/mirror/" + image, fixture.builder.newContainer(image).build().image()); + } + private static class TestFixture { final EmulatorConfig config = mock(EmulatorConfig.class); final EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);