fix(docker): preserve explicitly configured DNS resolvers (#5727)

* fix(docker): preserve explicitly configured DNS resolvers

Honor explicit --dns entries before injecting the embedded resolver and public fallbacks. Keep automatic DNS for containers without configured resolvers, and verify the shared builder and Lambda launcher preserve the requested list.

Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(lambda): keep AWS endpoints reachable with explicit DNS

Signed-off-by: Andre Kurait <akurait@amazon.com>

---------

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-16 17:49:30 -05:00
1 parent 84e5bed623
commit f1e00eff5c
9 files changed
+73 -8

No files matched your search

@@ -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());
}
@Test
void withCgroupnsModeRecordsDockerNamespaceMode() {
TestFixture fixture = new TestFixture();
@@ -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));
}
@Test
void 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));
}
}
@@ -459,6 +459,8 @@ class ContainerLauncherTest {
@Test
void 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"));
}
@Test
void launchFunction_rejectsPublishedPortBoundToUnsupportedHostAddress() throws Exception {
when(config.services().lambda().dockerFlags()).thenReturn(Optional.of(