mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -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
+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.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user