fix(ecs): assign awsvpc tasks dynamic host ports to avoid collisions (#1785)

In awsvpc mode every AWS task receives its own ENI, so a portMapping's
hostPort carries no host-binding semantics. Floci was publishing it
literally on the single local Docker host, so two tasks of the same
service — or two services sharing a port — collided on start.

awsvpc mappings now get a dynamic host port in native mode, or
expose-only in Docker mode where consumers reach containers via the
docker network IP. bridge and host mode keep publishing hostPort
literally, matching AWS.

Also fix the leak this surfaced: a failed start in
ContainerLifecycleManager.createAndStart left the created container
behind, so retrying callers accumulated Created containers and
fixed-name callers hit name conflicts on the next attempt.

Refs #1778
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-07-09 23:45:54 -05:00
1 parent 768019836f
commit 3771c1e9a2
5 files changed
+177 -7

No files matched your search

@@ -67,7 +67,15 @@ public class ContainerLifecycleManager {
*/
public ContainerInfo createAndStart(ContainerSpec spec) {
String containerId = create(spec);
return startCreated(containerId, spec);
try {
return startCreated(containerId, spec);
} catch (Exception e) {
// A failed start (e.g. host-port conflict) must not leak the created
// container: retrying callers would accumulate Created containers and
// fixed-name callers would hit name conflicts on the next attempt.
removeIfExists(containerId);
throw e;
}
}
/**
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.services.ecs.model.EcsTask;
import io.github.hectorvent.floci.services.ecs.model.EfsVolumeConfiguration;
import io.github.hectorvent.floci.services.ecs.model.MountPoint;
import io.github.hectorvent.floci.services.ecs.model.NetworkBinding;
import io.github.hectorvent.floci.services.ecs.model.NetworkMode;
import io.github.hectorvent.floci.services.ecs.model.PortMapping;
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import io.github.hectorvent.floci.services.ecs.model.Volume;
@@ -132,16 +133,19 @@ public class EcsContainerManager {
specBuilder.withMemoryMb(def.getMemory());
}
// Add port mappings. An explicit hostPort is always published to the
// Docker host so the service is reachable at localhost:<hostPort>,
// matching AWS bridge mode (mirrors the ECR registry's fixed-port
// publishing). When no hostPort is requested, publish a dynamic host
// port in native mode; in Docker mode only expose the port, since ECS
// Add port mappings. In bridge/host mode an explicit hostPort is
// published to the Docker host literally, matching AWS bridge mode
// (mirrors the ECR registry's fixed-port publishing). In awsvpc mode
// every AWS task gets its own ENI, so a literal hostPort carries no
// host-binding semantics and would collide across tasks on the single
// local Docker host (#1778) — awsvpc mappings always get a dynamic
// host port in native mode, or expose-only in Docker mode where ECS
// consumers reach containers via the docker network IP.
if (def.getPortMappings() != null) {
boolean awsvpc = taskDef.getNetworkMode() == NetworkMode.awsvpc;
boolean publishToHost = !containerDetector.isRunningInContainer();
for (PortMapping pm : def.getPortMappings()) {
if (pm.hostPort() > 0) {
if (!awsvpc && pm.hostPort() > 0) {
specBuilder.withPortBinding(pm.containerPort(), pm.hostPort());
} else if (publishToHost) {
specBuilder.withDynamicPort(pm.containerPort());