Light, fluffy, and always free - The AWS Local Emulator alternative
fix(docker): preserve explicitly configured DNS resolvers (#5727)
Andre Kuraitf1e00eff5c10
diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.mdindex 0d28b377fb..08569e3e3e 100644--- a/docs/configuration/environment-variables.md+++ b/docs/configuration/environment-variables.md@@ -258,7 +258,7 @@ See [Initialization Hooks](./initialization-hooks.md) for lifecycle phases and s| `FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED` | `false` | Watch Lambda code directories for changes and reload without redeployment || `FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS` | _(none)_ | Comma-separated host paths that hot-reload is allowed to watch || `FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK` | _(none)_ | Docker network for Lambda containers (overrides `FLOCI_SERVICES_DOCKER_NETWORK`) |-| `FLOCI_SERVICES_LAMBDA_DOCKER_FLAGS` | _(none)_ | Additional Docker flags applied to Lambda containers, such as `--env`, `--volume`, `--publish`, `--add-host`, `--dns`, `--label`, `--network`, `--user`, `--privileged`, and `--platform`. Published ports support `host:container` and `127.0.0.1:host:container` forms |+| `FLOCI_SERVICES_LAMBDA_DOCKER_FLAGS` | _(none)_ | Additional Docker flags applied to Lambda containers, such as `--env`, `--volume`, `--publish`, `--add-host`, `--dns`, `--label`, `--network`, `--user`, `--privileged`, and `--platform`. Explicit `--dns` entries replace automatic embedded DNS and public fallback resolvers. Published ports support `host:container` and `127.0.0.1:host:container` forms || `FLOCI_SERVICES_LAMBDA_CONTAINER_NAME_PREFIX` | `floci` | Base name prefix for Lambda-spawned containers and code volumes (must match `[A-Za-z0-9][A-Za-z0-9_.-]*`) || `FLOCI_SERVICES_LAMBDA_DOCKER_HOST_OVERRIDE` | _(none)_ | Explicit host/IP Lambda containers use to reach the Runtime API, bypassing auto-detection (e.g. rootless Podman) || `FLOCI_SERVICES_LAMBDA_AWS_CONFIG_PATH` | _(none)_ | Host path bind-mounted read-only at `/opt/aws-config` inside Lambda containers for real credential discovery |diff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex da53951886..f6d5692c82 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -491,7 +491,7 @@ These AWS Lambda operations have no handler in Floci. Calls will return `404` or| `FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED` | `false` | Enable bind-mount hot-reload via `S3Bucket=hot-reload` || `FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS` | *(unset)* | Comma-separated allowlist of host paths that may be bind-mounted || `FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK` | *(unset)* | Docker network to attach Lambda containers to (overrides `FLOCI_SERVICES_DOCKER_NETWORK`) |-| `FLOCI_SERVICES_LAMBDA_DOCKER_FLAGS` | *(unset)* | Additional Docker flags applied to Lambda containers, including `--env`, `--volume`, `--publish`, `--add-host`, `--dns`, `--label`, `--network`, `--user`, `--privileged`, and `--platform`. Published ports support `host:container` and `127.0.0.1:host:container` forms |+| `FLOCI_SERVICES_LAMBDA_DOCKER_FLAGS` | *(unset)* | Additional Docker flags applied to Lambda containers, including `--env`, `--volume`, `--publish`, `--add-host`, `--dns`, `--label`, `--network`, `--user`, `--privileged`, and `--platform`. Explicit `--dns` entries replace automatic embedded DNS and public fallback resolvers. Published ports support `host:container` and `127.0.0.1:host:container` forms || `FLOCI_SERVICES_LAMBDA_EXTRA_HOSTS` | *(unset)* | Comma-separated `hostname:ip` entries added to each Lambda container's `/etc/hosts`; `ip` may be `host-gateway`, mirroring `docker run --add-host` || `FLOCI_SERVICES_LAMBDA_DOCKER_HOST_OVERRIDE` | *(unset)* | Explicit host/IP that spawned Lambda containers use to reach Floci's Runtime API, bypassing auto-detection || `FLOCI_SERVICES_LAMBDA_CONTAINER_NAME_PREFIX` | `floci` | Base name prefix for spawned Lambda containers and code volumes (e.g. `acme` → `acme-<function>-<id>` containers, `acme-code-<function>-<hash>` volumes; with `FLOCI_DOCKER_RESOURCE_NAMESPACE` set, the namespace follows the prefix and a short hash of it ends the volume name, e.g. `acme-<namespace>-code-<function>-<hash>-<namespace hash>`). Must be a valid Docker name segment (`[A-Za-z0-9][A-Za-z0-9_.-]*`); invalid values are ignored with a warning |@@ -856,6 +856,10 @@ being mounted as an empty local volume.`8.8.8.8`, `8.8.4.4`) after its own IP on each spawned container's DNS list,so name resolution still works if the embedded forwarder cannot answer.+ Explicit `--dns` flags replace those automatic resolvers. In that case,+ the injected `AWS_ENDPOINT_URL` uses the reachable Docker host address+ instead of a hostname that requires Floci's resolver.+Tune or disable this for offline / locked-down networks where those resolversare blocked:diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilder.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilder.javaindex d025e2911b..efe554d759 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilder.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilder.java@@ -642,13 +642,17 @@ public class ContainerBuilder {/*** Injects Floci's embedded DNS server into the container so virtual-hosted* S3 hostnames (my-bucket.localhost.floci.io) resolve to Floci's Docker- * network IP. No-op when the embedded DNS server is not running.+ * network IP. No-op when explicit resolvers were already supplied or the embedded+ * DNS server is not running.** <p>When {@code floci.dns.container-fallback-enabled} is set, the configured public* fallback resolvers are appended after Floci's IP so the container's own resolver can* fall through for public hostnames if Floci's embedded forwarder cannot answer.*/public Builder withEmbeddedDns() {+ if (!dnsServers.isEmpty()) {+ return this;+ }embeddedDnsServer.getServerIp().ifPresent(flociIp -> {dnsServers.add(flociIp);if (config.dns().containerFallbackEnabled()) {diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpoint.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpoint.javaindex a76dc21d03..85f694630c 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpoint.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpoint.java@@ -57,7 +57,12 @@ public class ContainerReachableEndpoint {* listener is {@code TlsProxyServer} on {@code floci.port}.*/public String baseUrl() {- String flociHostname = embeddedDnsServer.getServerIp().isPresent()+ return baseUrl(true);+ }++ /** Uses the Docker host when the container does not receive Floci's DNS resolver. */+ public String baseUrl(boolean useEmbeddedDns) {+ String flociHostname = useEmbeddedDns && embeddedDnsServer.getServerIp().isPresent()? config.hostname().orElse(EmbeddedDnsServer.DEFAULT_SUFFIX): dockerHostResolver.resolve();return "http://" + flociHostname + ":" + port();diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnv.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnv.javaindex bee9c494f7..64a6958d8b 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnv.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnv.java@@ -69,7 +69,12 @@ public class LaunchedContainerAwsEnv {* the container reaches Floci.*/public String flociEndpoint() {- return reachableEndpoint.baseUrl();+ return flociEndpoint(true);+ }++ /** Selects an endpoint compatible with the container's DNS configuration. */+ public String flociEndpoint(boolean useEmbeddedDns) {+ return reachableEndpoint.baseUrl(useEmbeddedDns);}/**diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.javaindex d5719edb94..0f6935f339 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java@@ -217,6 +217,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {String cwLogStream = LOG_STREAM_DATE_FMT.format(LocalDate.now()) + "/[$LATEST]" + shortId;String lambdaRegion = extractRegionFromArn(fn.getFunctionArn(), config.defaultRegion());String lambdaAccountId = AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId());+ LambdaDockerFlags dockerFlags = configuredDockerFlags();// Build env varsList<String> env = new ArrayList<>();@@ -239,7 +240,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {}env.addAll(awsEnv.sdkBaselineEnv(lambdaRegion,awsConfigPath.isPresent() ? Optional.of("/opt/aws-config") : Optional.empty(),- roleCredentials, lambdaAccountId));+ awsEnv.flociEndpoint(dockerFlags.dnsServers().isEmpty()), roleCredentials, lambdaAccountId));if (fn.getEnvironment() != null) {boolean hasExecutionRoleCredentials = roleCredentials.isPresent();boolean userDefinesFullCredentialTriad = definesFullCredentialTriad(fn.getEnvironment());@@ -273,7 +274,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {.withLabels(ContainerStorageHelper.resourceIdentityLabels("lambda", fn.getFunctionName(), lambdaAccountId, lambdaRegion));- LambdaDockerFlags dockerFlags = configuredDockerFlags();applyDockerFlags(specBuilder, dockerFlags);specBuilder.withEmbeddedDns();diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilderTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilderTest.javaindex 2ec2f369c9..93d177eb17 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilderTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerBuilderTest.java@@ -91,6 +91,20 @@ class ContainerBuilderTest {assertEquals(List.of(), spec.dnsServers());}+ @Test+ void withEmbeddedDns_preservesExplicitResolversWithoutAddingFallbacks() {+ TestFixture fixture = new TestFixture();+ when(fixture.embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.4"));++ ContainerSpec spec = fixture.builder.newContainer("alpine")+ .withDnsServer("172.18.0.8")+ .withDnsServer("172.18.0.9")+ .withEmbeddedDns()+ .build();++ assertEquals(List.of("172.18.0.8", "172.18.0.9"), spec.dnsServers());+ }+@Testvoid withCgroupnsModeRecordsDockerNamespaceMode() {TestFixture fixture = new TestFixture();diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpointTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpointTest.javaindex ef8bff5d79..8fc6bb05b4 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpointTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerReachableEndpointTest.java@@ -45,6 +45,15 @@ class ContainerReachableEndpointTest {assertEquals("http://" + EmbeddedDnsServer.DEFAULT_SUFFIX + ":4566", endpoint.baseUrl());}+ @Test+ void explicitDnsUsesDockerHostEvenWhenEmbeddedDnsIsRunning() {+ hostMappedTo4811();+ lenient().when(embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.2"));+ when(dockerHostResolver.resolve()).thenReturn("172.18.0.9");++ assertEquals("http://172.18.0.9:4566", endpoint.baseUrl(false));+ }+@Testvoid dockerHostFallbackUsesListenPortNotAdvertisedPort() {hostMappedTo4811();@@ -76,6 +85,7 @@ class ContainerReachableEndpointTest {when(dockerHostResolver.resolve()).thenReturn("172.18.0.2");assertEquals("http://172.18.0.2:4811", endpoint(4811).baseUrl());+ assertEquals("http://172.18.0.2:4811", endpoint(4811).baseUrl(false));}@Test@@ -88,5 +98,6 @@ class ContainerReachableEndpointTest {when(dockerHostResolver.resolve()).thenReturn("172.18.0.2");assertEquals("http://172.18.0.2:4566", endpoint(4510).baseUrl());+ assertEquals("http://172.18.0.2:4566", endpoint(4510).baseUrl(false));}}diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.javaindex f98798ae5e..3925936e67 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java@@ -459,6 +459,8 @@ class ContainerLauncherTest {@Testvoid launchFunction_appliesConfiguredDockerFlagsToRealContainer() throws Exception {+ lenient().when(embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.4"));+ when(dockerHostResolver.resolve()).thenReturn("172.18.0.9");when(config.services().lambda().dockerFlags()).thenReturn(Optional.of("--env NODE_EXTRA_CA_CERTS=/opt/certs/root.pem "+ "--volume /tmp/certs:/opt/certs:ro --add-host api.local:host-gateway "@@ -482,12 +484,32 @@ class ContainerLauncherTest {assertEquals(Map.of(5050, 5050), spec.portBindings());assertEquals(List.of(5050), spec.loopbackPortBindings());assertTrue(spec.extraHosts().contains("api.local:host-gateway"));- assertTrue(spec.dnsServers().contains("1.1.1.1"));+ assertEquals(List.of("1.1.1.1"), spec.dnsServers());+ assertTrue(spec.env().contains("AWS_ENDPOINT_URL=http://172.18.0.9:4566"));assertEquals("debug", spec.labels().get("purpose"));assertEquals("/opt/certs", spec.binds().getFirst().getVolume().getPath());assertEquals("/tmp/certs", spec.binds().getFirst().getPath());}+ @Test+ void launchFunction_usesEmbeddedDnsEndpointWithoutExplicitResolvers() throws Exception {+ when(embeddedDnsServer.getServerIp()).thenReturn(Optional.of("172.18.0.4"));+ when(config.dns()).thenReturn(mock(EmulatorConfig.DnsConfig.class));+ Path codePath = Files.createDirectory(tempDir.resolve("embedded-dns-code"));++ LambdaFunction fn = new LambdaFunction();+ fn.setFunctionName("embedded-dns-fn");+ fn.setRuntime("nodejs20.x");+ fn.setHandler("index.handler");+ fn.setCodeLocalPath(codePath.toString());++ launcher.launch(fn);++ ContainerSpec spec = captureRealContainerSpec();+ assertEquals("172.18.0.4", spec.dnsServers().getFirst());+ assertTrue(spec.env().contains("AWS_ENDPOINT_URL=http://localhost.floci.io:4566"));+ }+@Testvoid launchFunction_rejectsPublishedPortBoundToUnsupportedHostAddress() throws Exception {when(config.services().lambda().dockerFlags()).thenReturn(Optional.of(