mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(docker): keep host network mode for containers that publish ports (#4192)
* fix(docker): keep host network mode for containers that publish ports A spec with port bindings skipped HostConfig.NetworkMode on create and connected to the network after start, which Docker refuses for host, none and container:<id>. Such containers silently stayed on the default bridge with a published port, so an OpenSearch domain under FLOCI_SERVICES_DOCKER_NETWORK=host stopped listening on the host's :9200 once domains always published a host port (#2798). Namespace network modes now always go on the HostConfig, their port bindings are dropped since Docker publishes nothing there, and the post-start connect is skipped. A host-network container's endpoint resolves to localhost when Floci itself runs in Docker instead of a null host. * fix(docker): resolve container:<id> endpoints through the owning container resolveEndpoint only special-cased host, so a container sharing another container's network namespace fell through to resolveContainerIp, which has no network to read, and Floci stored a null host for it. The endpoint now follows HostConfig.NetworkMode to the owning container: localhost when that container is on the host network, otherwise its IP on the Docker network. Reading the mode from the inspect rather than the spec also covers adopted containers. * test(docker): pass portBindingHostIps to the network test spec helper ContainerSpec gained a 28th component in #4175 after this branch was cut, so the spec(...) helper added here no longer compiled once rebased onto main.
This commit is contained in:
1 parent
a624608b5d
commit
a574a6d01f
5 files changed
+240
-14
No files matched your search
@@ -195,6 +195,8 @@ Environment variable: `FLOCI_SERVICES_DOCKER_NETWORK`
|
||||
|
||||
Individual services can override the network with their own `docker-network` setting (e.g. `floci.services.lambda.docker-network`).
|
||||
|
||||
A network mode of `host`, `none` or `container:<id>` places the spawned containers in that network namespace instead of on a Docker network. Docker publishes no host ports there, so a container on the host network serves its own port directly on the host (an OpenSearch domain listens on `9200` rather than on a port from its configured range), and Floci reaches it via `localhost`. A container placed in another container's namespace (`container:<id>`) is reached through that container's address instead: its IP on the Docker network, or `localhost` when it is itself on the host network.
|
||||
|
||||
!!! tip
|
||||
In Docker Compose, the default network name is `<project-name>_default`. If your compose file is in a directory named `myapp`, the network is `myapp_default`.
|
||||
|
||||
|
||||
+38
-14
@@ -62,6 +62,7 @@ public class ContainerLifecycleManager {
|
||||
Pattern.compile("join keyctl.*disk quota exceeded", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
|
||||
|
||||
private static final long NANO_CPUS_PER_CPU = 1_000_000_000L;
|
||||
private static final String CONTAINER_NETWORK_MODE_PREFIX = "container:";
|
||||
|
||||
/** Host interface a port marked loopback-only publishes on. */
|
||||
private static final String LOOPBACK_HOST_IP = "127.0.0.1";
|
||||
@@ -295,7 +296,7 @@ public class ContainerLifecycleManager {
|
||||
LOG.infov("Started container {0}", containerId);
|
||||
|
||||
if (spec.networkMode() != null && !spec.networkMode().isBlank()
|
||||
&& spec.hasPortBindings() && !spec.hasNetworkConfiguration()) {
|
||||
&& spec.publishesPorts() && !spec.hasNetworkConfiguration()) {
|
||||
try {
|
||||
dockerClient.connectToNetworkCmd()
|
||||
.withContainerId(containerId)
|
||||
@@ -313,7 +314,7 @@ public class ContainerLifecycleManager {
|
||||
}
|
||||
|
||||
private ContainerInfo withPublishedHostPorts(ContainerInfo info, ContainerSpec spec) {
|
||||
if (spec.portBindings() == null || spec.portBindings().isEmpty()) {
|
||||
if (!spec.publishesPorts()) {
|
||||
return info;
|
||||
}
|
||||
|
||||
@@ -991,7 +992,7 @@ public class ContainerLifecycleManager {
|
||||
// binding. ECR's sibling registry is the exception: it always publishes
|
||||
// its port because host-side docker clients (and CDK in compat tests)
|
||||
// connect via localhost:<hostPort>.
|
||||
if (spec.hasPortBindings()) {
|
||||
if (spec.publishesPorts()) {
|
||||
Ports ports = new Ports();
|
||||
for (Map.Entry<Integer, Integer> entry : spec.portBindings().entrySet()) {
|
||||
int containerPort = entry.getKey();
|
||||
@@ -1006,13 +1007,17 @@ public class ContainerLifecycleManager {
|
||||
LOG.debugv("Port binding: {0} -> {1}", String.valueOf(containerPort), String.valueOf(hostPort));
|
||||
}
|
||||
hostConfig.withPortBindings(ports);
|
||||
} else if (spec.hasPortBindings()) {
|
||||
LOG.infov("Container {0} joins network {1}, which publishes no host ports; it serves {2} directly there",
|
||||
spec.name(), spec.networkMode(), spec.portBindings().keySet());
|
||||
}
|
||||
|
||||
// Network mode: only set during creation when there are no host port bindings.
|
||||
// Network mode: only set during creation when no host ports are published.
|
||||
// withNetworkMode() + port bindings suppresses port publishing on macOS Docker Desktop,
|
||||
// so containers with port bindings (e.g. ECR registry) connect to the network
|
||||
// after start via connectToNetworkCmd() instead.
|
||||
if (spec.networkMode() != null && !spec.networkMode().isBlank() && !spec.hasPortBindings()) {
|
||||
// so containers with published ports (e.g. ECR registry) connect to the network
|
||||
// after start via connectToNetworkCmd() instead. host, none and container:<id> publish
|
||||
// nothing and cannot be connected after creation, so they always go on the HostConfig.
|
||||
if (spec.networkMode() != null && !spec.networkMode().isBlank() && !spec.publishesPorts()) {
|
||||
hostConfig.withNetworkMode(spec.networkMode());
|
||||
}
|
||||
|
||||
@@ -1120,7 +1125,20 @@ public class ContainerLifecycleManager {
|
||||
}
|
||||
|
||||
private static boolean isContainerNetworkMode(String networkMode) {
|
||||
return networkMode != null && networkMode.startsWith("container:");
|
||||
return networkMode != null && networkMode.startsWith(CONTAINER_NETWORK_MODE_PREFIX);
|
||||
}
|
||||
|
||||
private static boolean isHostNetworkMode(InspectContainerResponse inspect) {
|
||||
HostConfig hostConfig = inspect.getHostConfig();
|
||||
return hostConfig != null && "host".equals(hostConfig.getNetworkMode());
|
||||
}
|
||||
|
||||
private static String networkNamespaceOwner(InspectContainerResponse inspect) {
|
||||
HostConfig hostConfig = inspect.getHostConfig();
|
||||
String networkMode = hostConfig == null ? null : hostConfig.getNetworkMode();
|
||||
return isContainerNetworkMode(networkMode)
|
||||
? networkMode.substring(CONTAINER_NETWORK_MODE_PREFIX.length())
|
||||
: null;
|
||||
}
|
||||
|
||||
private Map<Integer, EndpointInfo> resolveEndpoints(String containerId, ContainerSpec spec) {
|
||||
@@ -1155,15 +1173,21 @@ public class ContainerLifecycleManager {
|
||||
// Fallback to container port
|
||||
return new EndpointInfo("localhost", containerPort);
|
||||
} else {
|
||||
// Container mode: use container IP on the docker network.
|
||||
// Prefer the configured network's IP — the container may be on multiple
|
||||
// networks (bridge + the configured network) when connectToNetworkCmd()
|
||||
// is used instead of withNetworkMode() during creation.
|
||||
String containerIp = resolveContainerIp(inspect, preferredNetwork);
|
||||
return new EndpointInfo(containerIp, containerPort);
|
||||
return new EndpointInfo(resolveReachableHost(inspect, preferredNetwork), containerPort);
|
||||
}
|
||||
}
|
||||
|
||||
private String resolveReachableHost(InspectContainerResponse inspect, String preferredNetwork) {
|
||||
if (isHostNetworkMode(inspect)) {
|
||||
return "localhost";
|
||||
}
|
||||
String owner = networkNamespaceOwner(inspect);
|
||||
if (owner != null) {
|
||||
return resolveReachableHost(dockerClient.inspectContainerCmd(owner).exec(), preferredNetwork);
|
||||
}
|
||||
return resolveContainerIp(inspect, preferredNetwork);
|
||||
}
|
||||
|
||||
private String resolveContainerIp(InspectContainerResponse inspect, String preferredNetwork) {
|
||||
var networks = inspect.getNetworkSettings().getNetworks();
|
||||
if (networks != null) {
|
||||
|
||||
@@ -78,6 +78,7 @@ public record ContainerSpec(
|
||||
) {
|
||||
private static final Pattern LINK_LOCAL_IPV4 = Pattern.compile("^169\\.254\\.(\\d{1,3})\\.(\\d{1,3})$");
|
||||
private static final Set<String> NETWORKS_WITHOUT_ENDPOINT_IPAM = Set.of("bridge", "default", "host", "none");
|
||||
private static final Set<String> NAMESPACE_NETWORK_MODES = Set.of("host", "none");
|
||||
|
||||
public ContainerSpec {
|
||||
if (linkLocalIps != null && !linkLocalIps.isEmpty()) {
|
||||
@@ -203,6 +204,23 @@ public record ContainerSpec(
|
||||
return portBindings != null && !portBindings.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when the network mode hands the container an existing network namespace
|
||||
* ({@code host}, {@code none} or {@code container:<id>}) instead of an endpoint on a Docker
|
||||
* network. Docker applies these modes at creation only and publishes no ports through them.
|
||||
*/
|
||||
public boolean sharesNetworkNamespace() {
|
||||
return networkMode != null
|
||||
&& (NAMESPACE_NETWORK_MODES.contains(networkMode) || networkMode.startsWith("container:"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when Docker can publish the requested port bindings on the host.
|
||||
*/
|
||||
public boolean publishesPorts() {
|
||||
return hasPortBindings() && !sharesNetworkNamespace();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this spec has a memory limit configured.
|
||||
*/
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class ContainerHostNetworkDockerIntegrationTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ContainerHostNetworkDockerIntegrationTest.class);
|
||||
private static final String IMAGE = "public.ecr.aws/docker/library/busybox:1.36";
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
ContainerBuilder containerBuilder;
|
||||
|
||||
@Inject
|
||||
ContainerLifecycleManager lifecycleManager;
|
||||
|
||||
@BeforeEach
|
||||
void requireDocker() {
|
||||
Assumptions.assumeTrue(isDockerAvailable(),
|
||||
"Docker daemon must be available for host network integration tests");
|
||||
}
|
||||
|
||||
@Test
|
||||
void portBoundContainerIsCreatedInHostNetworkMode() {
|
||||
ContainerSpec spec = containerBuilder.newContainer(IMAGE)
|
||||
.withName("floci-host-network-test-" + UUID.randomUUID())
|
||||
.withCmd(List.of("sleep", "30"))
|
||||
.withNetworkMode("host")
|
||||
.withPortBinding(8080, 18080)
|
||||
.build();
|
||||
|
||||
String containerId = null;
|
||||
try {
|
||||
containerId = lifecycleManager.createAndStart(spec).containerId();
|
||||
|
||||
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
|
||||
assertEquals("host", inspect.getHostConfig().getNetworkMode());
|
||||
Ports bindings = inspect.getHostConfig().getPortBindings();
|
||||
assertTrue(bindings == null || bindings.getBindings().isEmpty(),
|
||||
"host network mode publishes no ports, got " + bindings);
|
||||
assertTrue(inspect.getNetworkSettings().getNetworks().containsKey("host"),
|
||||
"container should sit on the host network, got " + inspect.getNetworkSettings().getNetworks().keySet());
|
||||
} finally {
|
||||
if (containerId != null) {
|
||||
dockerClient.removeContainerCmd(containerId).withForce(true).exec();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isDockerAvailable() {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Docker daemon is not available for the host network integration test", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+106
@@ -11,12 +11,17 @@ import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.command.RemoveContainerCmd;
|
||||
import com.github.dockerjava.api.command.StartContainerCmd;
|
||||
import com.github.dockerjava.api.model.ContainerNetwork;
|
||||
import com.github.dockerjava.api.model.HostConfig;
|
||||
import com.github.dockerjava.api.model.NetworkSettings;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.Mock;
|
||||
@@ -26,6 +31,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -152,11 +159,110 @@ class ContainerLifecycleManagerNetworkTest {
|
||||
verify(disconnectCmd, never()).exec();
|
||||
}
|
||||
|
||||
/**
|
||||
* Docker only honours host, none and container:<id> on the HostConfig at creation, refuses
|
||||
* to connect a container to them afterwards, and publishes no host ports through them. A
|
||||
* port-bound spec must therefore still put the mode on the HostConfig instead of taking the
|
||||
* publish-then-connect path, which left such containers on the default bridge.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"host", "none", "container:sibling"})
|
||||
void createAppliesNamespaceNetworkModeInsteadOfPublishingPorts(String networkMode) {
|
||||
manager().create(spec(networkMode, Map.of(9200, 9400), List.of()));
|
||||
|
||||
HostConfig hostConfig = createdHostConfig();
|
||||
assertEquals(networkMode, hostConfig.getNetworkMode());
|
||||
assertNull(hostConfig.getPortBindings());
|
||||
verify(connectCmd, never()).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDoesNotAllocateDynamicPortsOnHostNetwork() {
|
||||
manager().create(spec("host", Map.of(9200, 0), List.of()));
|
||||
|
||||
verify(portAllocator, never()).allocateAny();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndStartDoesNotConnectHostNetworkAfterStart() {
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec("host", Map.of(9200, 9400), List.of()));
|
||||
|
||||
verify(connectCmd, never()).exec();
|
||||
assertTrue(info.publishedHostPort(9200).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointsOfHostNetworkContainersPointAtLocalhostWhenFlociRunsInDocker() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("host"));
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec("host", Map.of(9200, 9400), List.of(9200)));
|
||||
|
||||
assertEquals(new EndpointInfo("localhost", 9200), info.getEndpoint(9200));
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointsOfNamespaceSharingContainersPointAtTheOwningContainerWhenFlociRunsInDocker() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getHostConfig())
|
||||
.thenReturn(HostConfig.newHostConfig().withNetworkMode("container:sibling-id"));
|
||||
InspectContainerResponse owner = inspectOf("sibling-id");
|
||||
NetworkSettings ownerNetworks = mock(NetworkSettings.class);
|
||||
when(owner.getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("bridge"));
|
||||
when(owner.getNetworkSettings()).thenReturn(ownerNetworks);
|
||||
when(ownerNetworks.getNetworks())
|
||||
.thenReturn(Map.of("bridge", new ContainerNetwork().withIpv4Address("172.17.0.7")));
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec("container:sibling", Map.of(9200, 9400), List.of(9200)));
|
||||
|
||||
assertEquals(new EndpointInfo("172.17.0.7", 9200), info.getEndpoint(9200));
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointsOfContainersJoiningAHostNetworkContainerPointAtLocalhostWhenFlociRunsInDocker() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getHostConfig())
|
||||
.thenReturn(HostConfig.newHostConfig().withNetworkMode("container:sibling-id"));
|
||||
when(inspectOf("sibling-id").getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("host"));
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec("container:sibling", Map.of(9200, 9400), List.of(9200)));
|
||||
|
||||
assertEquals(new EndpointInfo("localhost", 9200), info.getEndpoint(9200));
|
||||
}
|
||||
|
||||
private InspectContainerResponse inspectOf(String containerId) {
|
||||
InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class, RETURNS_SELF);
|
||||
InspectContainerResponse inspect = mock(InspectContainerResponse.class);
|
||||
when(dockerClient.inspectContainerCmd(containerId)).thenReturn(inspectCmd);
|
||||
when(inspectCmd.exec()).thenReturn(inspect);
|
||||
return inspect;
|
||||
}
|
||||
|
||||
private HostConfig createdHostConfig() {
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
verify(createCmd).withHostConfig(hostConfig.capture());
|
||||
return hostConfig.getValue();
|
||||
}
|
||||
|
||||
private ContainerLifecycleManager manager() {
|
||||
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector,
|
||||
portAllocator, config);
|
||||
}
|
||||
|
||||
private static ContainerSpec spec(String networkMode, Map<Integer, Integer> portBindings,
|
||||
List<Integer> exposedPorts) {
|
||||
return new ContainerSpec(
|
||||
"busybox:stable", "probe", List.of(), null, null, null, portBindings, List.of(),
|
||||
exposedPorts, networkMode, List.of(), List.of(), List.of(), List.of(), Map.of(), null,
|
||||
false, null, List.of(), null, null, List.of(), List.of(), null, null, false,
|
||||
List.of(), Map.of());
|
||||
}
|
||||
|
||||
private static ContainerSpec specWithLinkLocalIp(Map<Integer, Integer> portBindings) {
|
||||
return new ContainerSpec(
|
||||
"busybox:stable", null, List.of(), null, null, null, portBindings, List.of(),
|
||||
|
||||
Reference in new issue
Block a user