fix(ecr): advertise published host port when adopting registry container (#1715)

* fix(ecr): advertise published host port when adopting registry container

When Floci runs inside Docker and adopts an already-running ECR registry
container, adopt()'s endpoint resolves to the container IP and internal port
5000. That port was advertised in getRepositoryUri/getProxyEndpoint, but those
URIs are consumed by the host-side Docker daemon, so docker login against the
advertised endpoint failed. Read the published host binding when adopting and
fall back to the configured port with a warning when no binding exists.

* refactor(ecr): read published binding from adopt's existing inspect

Applies the review suggestion: adopt() already inspects the container, so
carry the published host bindings in ContainerInfo instead of re-inspecting
via a separate publishedHostPort lookup. Removes the extra Docker round-trip
on every adoption.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-07-03 01:23:20 -05:00
1 parent fb546d54c4
commit 6d1d892a19
3 files changed
+89 -5

No files matched your search

@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
/**
* Manages Docker container lifecycle operations including create, start, stop, and remove.
@@ -254,11 +255,36 @@ public class ContainerLifecycleManager {
}
Map<Integer, EndpointInfo> endpoints = new HashMap<>();
Map<Integer, Integer> publishedHostPorts = new HashMap<>();
for (int port : ports) {
endpoints.put(port, resolveEndpoint(inspect, port));
OptionalInt published = readPublishedHostPort(inspect, port);
if (published.isPresent()) {
publishedHostPorts.put(port, published.getAsInt());
}
}
return new ContainerInfo(containerId, endpoints);
return new ContainerInfo(containerId, endpoints, publishedHostPorts);
}
/**
* Reads the host port a container's internal port is published on, independent of
* whether Floci itself runs inside a container. Unlike {@link #resolveEndpoint} —
* which switches to container-IP + internal port in container mode — this always
* reads the port binding, for URIs consumed by the host-side Docker daemon.
*/
private static OptionalInt readPublishedHostPort(InspectContainerResponse inspect, int containerPort) {
Ports ports = inspect.getNetworkSettings().getPorts();
if (ports != null) {
Ports.Binding[] binding = ports.getBindings().get(ExposedPort.tcp(containerPort));
if (binding != null && binding.length > 0) {
try {
return OptionalInt.of(Integer.parseInt(binding[0].getHostPortSpec()));
} catch (NumberFormatException ignored) {
}
}
}
return OptionalInt.empty();
}
/**
@@ -500,17 +526,33 @@ public class ContainerLifecycleManager {
*
* @param containerId the Docker container ID
* @param endpoints map of container port to resolved endpoint (host:port for connection)
* @param publishedHostPorts map of container port to the host port it is published on;
* a port without a binding is absent
*/
public record ContainerInfo(
String containerId,
Map<Integer, EndpointInfo> endpoints
Map<Integer, EndpointInfo> endpoints,
Map<Integer, Integer> publishedHostPorts
) {
public ContainerInfo(String containerId, Map<Integer, EndpointInfo> endpoints) {
this(containerId, endpoints, Map.of());
}
/**
* Gets the endpoint for a specific container port.
*/
public EndpointInfo getEndpoint(int containerPort) {
return endpoints.get(containerPort);
}
/**
* Gets the host port a container port is published on, regardless of whether
* Floci itself runs inside a container. Empty when the port has no binding.
*/
public OptionalInt publishedHostPort(int containerPort) {
Integer published = publishedHostPorts.get(containerPort);
return published != null ? OptionalInt.of(published) : OptionalInt.empty();
}
}
/**
@@ -337,9 +337,15 @@ public class EcrRegistryManager {
this.containerId = existing.getId();
try {
ContainerInfo info = lifecycleManager.adopt(containerId, List.of(CONTAINER_INTERNAL_PORT));
var endpoint = info.getEndpoint(CONTAINER_INTERNAL_PORT);
if (endpoint != null) {
this.hostPort = endpoint.port();
// getRepositoryUri/getProxyEndpoint are consumed by the host-side docker
// daemon, so hostPort must be the published binding — adopt's endpoint
// resolves to the container-internal port when Floci runs inside Docker.
var published = info.publishedHostPort(CONTAINER_INTERNAL_PORT);
if (published.isPresent()) {
this.hostPort = published.getAsInt();
} else {
LOG.warnv("Adopted ECR registry container {0} has no published binding for port {1}; keeping configured port {2}",
containerId, String.valueOf(CONTAINER_INTERNAL_PORT), String.valueOf(hostPort));
}
this.started = true;
LOG.infov("Adopted existing ECR registry container {0} on host port {1}",