Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat(ecs): publish awsvpc ports on demand (#3766)

Aniket Shukla869589e93247
diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md
index c9bd11e0fd..8df59ba96c 100644
--- a/docs/configuration/environment-variables.md
+++ b/docs/configuration/environment-variables.md
@@ -466,6 +466,7 @@ These services spawn Docker containers. They require access to the Docker socket
|---|---|---|
| `FLOCI_SERVICES_ECS_ENABLED` | `true` | Enable the ECS service |
| `FLOCI_SERVICES_ECS_MOCK` | `false` | When `true`, tasks are registered but not actually run |
+| `FLOCI_SERVICES_ECS_PUBLISH_AWSVPC_PORTS_TO_HOST` | `false` | Publish `awsvpc` task ports on stable Docker host ports for host-side clients |
| `FLOCI_SERVICES_ECS_DEFAULT_MEMORY_MB` | `512` | Default task memory when not specified in the task definition |
| `FLOCI_SERVICES_ECS_DEFAULT_CPU_UNITS` | `256` | Default task CPU units when not specified in the task definition |
| `FLOCI_SERVICES_ECS_DOCKER_NETWORK` | _(none)_ | Docker network for ECS task containers |
diff --git a/docs/services/ecs.md b/docs/services/ecs.md
index c5c045e64b..afedf6d7a6 100644
--- a/docs/services/ecs.md
+++ b/docs/services/ecs.md
@@ -350,6 +350,24 @@ services:
FLOCI_SERVICES_ECS_DOCKER_NETWORK: aws-local_default
```
+### Host access to awsvpc task ports
+
+By default, Floci preserves the isolation expected from `awsvpc`: native runs use a dynamic Docker host port, while Floci-in-Docker exposes the container port only on the configured Docker network. A process running directly on the Docker host therefore has no stable port for an `awsvpc` task.
+
+Set `FLOCI_SERVICES_ECS_PUBLISH_AWSVPC_PORTS_TO_HOST=true` to opt into stable host publishing. Floci binds each `containerPort` to the same host port, or uses an explicit non-zero `hostPort` when one is present. A host-side Terraform provider can then connect to `localhost:<port>`.
+
+```yaml
+services:
+ floci:
+ image: floci/floci:latest
+ volumes:
+ - /var/run/docker.sock:/var/run/docker.sock
+ environment:
+ FLOCI_SERVICES_ECS_PUBLISH_AWSVPC_PORTS_TO_HOST: "true"
+```
+
+This setting is an emulator-specific networking convenience and defaults to `false`. Docker cannot bind the same host port twice, so use it only when at most one running task publishes each port.
+
## Examples
```bash
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index 4d650105c0..fde2bc5881 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -1853,6 +1853,14 @@ public interface EmulatorConfig {
@WithDefault("false")
boolean mock();
+ /**
+ * Publish {@code awsvpc} task ports on the Docker host so local host processes can
+ * reach them. This is an emulator-only escape hatch and can cause port collisions
+ * when more than one task exposes the same port.
+ */
+ @WithDefault("false")
+ boolean publishAwsvpcPortsToHost();
+
Optional<String> dockerNetwork();
@WithDefault("512")
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
index cee11dbaa2..c58554a71c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
@@ -264,14 +264,23 @@ public class EcsContainerManager {
// 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.
+ // consumers reach containers via the docker network IP. The explicit
+ // emulator opt-in below publishes a stable port for host-side clients.
if (protectedNetwork == null && def.getPortMappings() != null) {
boolean awsvpc = taskDef.getNetworkMode() == NetworkMode.awsvpc;
- boolean publishToHost = !containerDetector.isRunningInContainer();
+ boolean publishAwsvpcPorts = awsvpc
+ && config.services().ecs().publishAwsvpcPortsToHost();
+ boolean publishDynamicPortsToHost = !containerDetector.isRunningInContainer();
for (PortMapping pm : def.getPortMappings()) {
- if (!awsvpc && pm.hostPort() > 0) {
+ if (publishAwsvpcPorts) {
+ int hostPort = pm.hostPort() > 0 ? pm.hostPort() : pm.containerPort();
+ LOG.warnv("Publishing ECS awsvpc container port {0} on host port {1}; "
+ + "multiple tasks cannot share this host port",
+ pm.containerPort(), hostPort);
+ specBuilder.withPortBinding(pm.containerPort(), hostPort);
+ } else if (!awsvpc && pm.hostPort() > 0) {
specBuilder.withPortBinding(pm.containerPort(), pm.hostPort());
- } else if (publishToHost) {
+ } else if (publishDynamicPortsToHost) {
specBuilder.withDynamicPort(pm.containerPort());
} else {
specBuilder.withExposedPort(pm.containerPort());
@@ -474,14 +483,10 @@ public class EcsContainerManager {
String eniId = eni.getNetworkInterfaceId();
SecurityGroupFirewallManager.Namespace namespace = null;
try {
- Map<Integer, Integer> bindings = new LinkedHashMap<>();
- if (!containerDetector.isRunningInContainer()) {
- for (ContainerDefinition container : definition.getContainerDefinitions()) {
- if (container.getPortMappings() != null) {
- container.getPortMappings().forEach(port -> bindings.put(port.containerPort(), 0));
- }
- }
- }
+ Map<Integer, Integer> bindings = namespacePortBindings(
+ definition,
+ containerDetector.isRunningInContainer(),
+ config.services().ecs().publishAwsvpcPortsToHost());
namespace = firewallManager.createNamespace("ecs", taskId, regionResolver.getAccountId(),
region, config.services().ecs().dockerNetwork(), bindings);
List<String> groupIds = eni.getGroups().stream().map(g -> g.getGroupId()).toList();
@@ -514,6 +519,26 @@ public class EcsContainerManager {
}
}
+ static Map<Integer, Integer> namespacePortBindings(
+ TaskDefinition definition, boolean runningInContainer, boolean publishAwsvpcPortsToHost) {
+ Map<Integer, Integer> bindings = new LinkedHashMap<>();
+ if (runningInContainer && !publishAwsvpcPortsToHost) {
+ return bindings;
+ }
+ for (ContainerDefinition container : definition.getContainerDefinitions()) {
+ if (container.getPortMappings() == null) {
+ continue;
+ }
+ for (PortMapping port : container.getPortMappings()) {
+ int hostPort = publishAwsvpcPortsToHost
+ ? (port.hostPort() > 0 ? port.hostPort() : port.containerPort())
+ : 0;
+ bindings.put(port.containerPort(), hostPort);
+ }
+ }
+ return bindings;
+ }
+
private record PreparedNetwork(NetworkInterface eni, SecurityGroupFirewallManager.Namespace namespace) {}
/**
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index b988327aef..ca95bf4c36 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -534,6 +534,7 @@ floci:
ecs:
enabled: true
mock: false
+ publish-awsvpc-ports-to-host: false
host-volume-roots: []
allow-unsafe-host-volumes: false
appconfig:
diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerPortMappingsTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerPortMappingsTest.java
index 8bb3f73e8f..701d2e87a2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerPortMappingsTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerPortMappingsTest.java
@@ -67,6 +67,7 @@ class EcsContainerManagerPortMappingsTest {
private ContainerBuilder.Builder builder;
private ContainerLifecycleManager lifecycleManager;
private ContainerDetector containerDetector;
+ private EmulatorConfig config;
private RegionResolver regionResolver;
private EcsContainerManager manager;
@@ -86,7 +87,7 @@ class EcsContainerManagerPortMappingsTest {
ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
containerDetector = mock(ContainerDetector.class);
- EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
+ config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
regionResolver = mock(RegionResolver.class);
LaunchedContainerAwsEnv awsEnv = mock(LaunchedContainerAwsEnv.class);
when(awsEnv.sdkBaselineEnv(any(), any())).thenReturn(List.of());
@@ -193,6 +194,71 @@ class EcsContainerManagerPortMappingsTest {
verify(builder, never()).withDynamicPort(80);
}
+ @Test
+ void awsvpcPortCanBePublishedStablyForHostRunners() {
+ when(containerDetector.isRunningInContainer()).thenReturn(true);
+ when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
+
+ startWith(List.of(new PortMapping(15_672)), NetworkMode.awsvpc);
+
+ verify(builder, times(1)).withPortBinding(15_672, 15_672);
+ verify(builder, never()).withExposedPort(15_672);
+ verify(builder, never()).withDynamicPort(15_672);
+ }
+
+ @Test
+ void awsvpcHostPublishingHonorsAnExplicitHostPort() {
+ when(containerDetector.isRunningInContainer()).thenReturn(true);
+ when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
+
+ startWith(List.of(new PortMapping(8080, 18_080, "tcp")), NetworkMode.awsvpc);
+
+ verify(builder, times(1)).withPortBinding(8080, 18_080);
+ }
+
+ @Test
+ void protectedAwsvpcNamespaceUsesTheStableHostBindings() {
+ ContainerDefinition app = new ContainerDefinition();
+ app.setPortMappings(List.of(
+ new PortMapping(6379),
+ new PortMapping(8080, 18_080, "tcp")));
+ TaskDefinition taskDefinition = new TaskDefinition();
+ taskDefinition.setContainerDefinitions(List.of(app));
+
+ Map<Integer, Integer> bindings = EcsContainerManager.namespacePortBindings(
+ taskDefinition, true, true);
+
+ assertEquals(Map.of(6379, 6379, 8080, 18_080), bindings);
+ }
+
+ @Test
+ void protectedAwsvpcNamespaceKeepsDefaultContainerModePrivate() {
+ ContainerDefinition app = new ContainerDefinition();
+ app.setPortMappings(List.of(new PortMapping(6379)));
+ TaskDefinition taskDefinition = new TaskDefinition();
+ taskDefinition.setContainerDefinitions(List.of(app));
+
+ Map<Integer, Integer> bindings = EcsContainerManager.namespacePortBindings(
+ taskDefinition, true, false);
+
+ assertEquals(Map.of(), bindings);
+ }
+
+ @Test
+ void protectedAwsvpcNamespaceUsesStableBindingsInNativeModeWithOptIn() {
+ ContainerDefinition app = new ContainerDefinition();
+ app.setPortMappings(List.of(
+ new PortMapping(6379),
+ new PortMapping(8080, 18_080, "tcp")));
+ TaskDefinition taskDefinition = new TaskDefinition();
+ taskDefinition.setContainerDefinitions(List.of(app));
+
+ Map<Integer, Integer> bindings = EcsContainerManager.namespacePortBindings(
+ taskDefinition, false, true);
+
+ assertEquals(Map.of(6379, 6379, 8080, 18_080), bindings);
+ }
+
@Test
void twoAwsvpcTasksWithSameHostPortNeverBindLiterally() {
when(containerDetector.isRunningInContainer()).thenReturn(false);