Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Hector Ventura6d1d892a1926
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
index 26f37d64cb..16078efda2 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
@@ -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();
+ }
}
/**
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java
index bbd0250dd2..fd7c46acca 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java
@@ -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}",
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java
index 519a6b8e1e..16e2112bd4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java
@@ -9,10 +9,13 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
import io.github.hectorvent.floci.core.common.docker.PortAllocator;
+import com.github.dockerjava.api.model.Container;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
+import java.util.List;
+import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -100,4 +103,37 @@ class EcrRegistryManagerTest {
assertEquals("http://" + REGISTRY_NAME + ":5000", manager.httpClient().baseUrl());
}
+
+ @Test
+ void adoptUsesPublishedHostPortEvenWhenRunningInsideDocker() {
+ // Regression: in container mode adopt()'s endpoint resolves to the registry's
+ // internal port (5000); the advertised proxy endpoint must use the published
+ // host binding instead, or docker login from the host daemon fails.
+ when(containerDetector.isRunningInContainer()).thenReturn(true);
+ Container existing = Mockito.mock(Container.class);
+ when(existing.getId()).thenReturn("0123456789abcdef");
+ when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing));
+ when(lifecycleManager.adopt("0123456789abcdef", List.of(5000)))
+ .thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef",
+ Map.of(5000, new ContainerLifecycleManager.EndpointInfo("172.17.0.5", 5000)),
+ Map.of(5000, BASE_PORT + 1)));
+
+ manager.ensureStarted();
+
+ assertEquals(BASE_PORT + 1, manager.effectivePort());
+ assertEquals("http://localhost:" + (BASE_PORT + 1), manager.getProxyEndpoint());
+ }
+
+ @Test
+ void adoptKeepsConfiguredPortWhenNoPublishedBindingExists() {
+ Container existing = Mockito.mock(Container.class);
+ when(existing.getId()).thenReturn("0123456789abcdef");
+ when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(existing));
+ when(lifecycleManager.adopt("0123456789abcdef", List.of(5000)))
+ .thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef", Map.of()));
+
+ manager.ensureStarted();
+
+ assertEquals(BASE_PORT, manager.effectivePort());
+ }
}