feat(ecr): prefer locally present images over the loopback registry rewrite (#3022)

* feat(ecr): prefer locally present images over the loopback registry rewrite

EcrRegistryManager.rewriteImageUri rewrote every AWS-shaped
<account>.dkr.ecr.<region>.amazonaws.com/<repo>:<tag> URI to Floci's
loopback registry unconditionally, so a container image Lambda function
(or ECS task) whose image was built locally and tagged with its real ECR
URI could never run: the launcher started the registry:2 sidecar and
tried to pull from it, failing with "failed to resolve reference ..."
(Lambda.InitError) even though the daemon already held the image.

The rewrite now first asks the Docker daemon whether the image exists
under the URI as given; if it does, the URI is used unchanged and the
registry container is not started. Images the daemon does not have keep
the existing rewrite-and-pull path, so CDK DockerImageFunction flows are
unaffected. floci.services.ecr.prefer-local-images
(FLOCI_SERVICES_ECR_PREFER_LOCAL_IMAGES, default true) restores the old
behaviour when set to false.

Verified with a provided.al2023 Go image tagged
000000000000.dkr.ecr.ap-southeast-2.amazonaws.com/e2e/hello-image:local:
Invoke fails on 2.0.1 and succeeds with this change, both with Floci as
a host process and inside a container sharing the host's docker.sock; an
image absent from the daemon still takes the rewrite path.

* fix(ecr): inspect the image reference ContainerBuilder launches when preferring local images

With floci.docker.image-registry-base set, rewriteImageUri inspected the bare
AWS-shaped URI while ContainerBuilder later prefixed it, so a locally present
image was reported as found and then missed at launch. Resolve the image
through ContainerBuilder before asking the daemon so both paths agree.
This commit is contained in:
Kurt McAlpine authored and GitHub committed 2026-09-04 10:59:51 -04:00
1 parent d398376642
commit 23728e0b75
9 files changed
+126 -4

No files matched your search

@@ -1804,6 +1804,13 @@ public interface EmulatorConfig {
@WithDefault("hostname")
String uriStyle();
/**
* When true, an AWS-shaped ECR image URI that names an image already present on the Docker
* daemon is used as-is instead of being rewritten to Floci's loopback registry.
*/
@WithDefault("true")
boolean preferLocalImages();
Optional<String> dockerNetwork();
}
@@ -65,7 +65,7 @@ public class ContainerBuilder {
return new Builder(resolveImage(image), config, dockerHostResolver, embeddedDnsServer, currentContainerNetworkResolver);
}
private String resolveImage(String image) {
public String resolveImage(String image) {
return resolveImage(image, configuredImageRegistryBase(config));
}
@@ -15,6 +15,7 @@ import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.api.command.InspectExecResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import jakarta.enterprise.context.ApplicationScoped;
@@ -95,7 +96,10 @@ public class EcrRegistryManager {
* starting the backing registry container on first use. Any image that doesn't
* match the AWS ECR URI shape (e.g. a plain Docker Hub reference or an image
* already pointing at Floci's registry) is returned unchanged, without starting
* the registry container.
* the registry container, as is an AWS-shaped URI naming an image the Docker
* daemon already has (under the reference {@link ContainerBuilder#resolveImage}
* launches, so {@code floci.docker.image-registry-base} is honoured) while
* {@code floci.services.ecr.prefer-local-images} is on.
*/
public String rewriteImageUri(String image) {
if (image == null) {
@@ -105,6 +109,13 @@ public class EcrRegistryManager {
if (!m.matches()) {
return image;
}
if (config.services().ecr().preferLocalImages()) {
String resolved = containerBuilder.resolveImage(image);
if (isPresentOnDaemon(resolved)) {
LOG.infov("Using locally present image {0} as-is (not rewriting to the emulated ECR registry)", resolved);
return image;
}
}
String account = m.group(1);
String region = m.group(2);
String repoAndTag = m.group(3);
@@ -114,6 +125,19 @@ public class EcrRegistryManager {
return rewritten;
}
private boolean isPresentOnDaemon(String image) {
try {
lifecycleManager.getDockerClient().inspectImageCmd(image).exec();
return true;
} catch (NotFoundException e) {
return false;
} catch (RuntimeException e) {
LOG.debugv("Could not inspect image {0} on the Docker daemon ({1}); rewriting to the emulated ECR registry",
image, e.getMessage());
return false;
}
}
/** Returns the docker-pullable repository URI for the given account/region/name. */
public String getRepositoryUri(String accountId, String region, String repoName) {
int port = effectivePort();