mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix Lambda container networking when Floci runs in Docker (#765)
Automatically detect the Docker network of the running Floci container and use it for spawned containers when no explicit Docker network is configured. This lets Lambda containers reach Floci’s Runtime API in Docker Compose setups without requiring users to manually set FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK. Also update Lambda/Docker docs to clarify the automatic network inheritance behavior. Tested with: ./mvnw -q -Dtest=ContainerBuilderTest,CurrentContainerNetworkResolverTest,DockerHostResolverTest,ContainerLauncherTest test
This commit is contained in:
1 parent
c8adf75370
commit
ded329beac
10 files changed
+347
-11
No files matched your search
@@ -38,13 +38,21 @@ public class ContainerBuilder {
|
||||
private final EmulatorConfig config;
|
||||
private final DockerHostResolver dockerHostResolver;
|
||||
private final EmbeddedDnsServer embeddedDnsServer;
|
||||
private final CurrentContainerNetworkResolver currentContainerNetworkResolver;
|
||||
|
||||
@Inject
|
||||
public ContainerBuilder(EmulatorConfig config, DockerHostResolver dockerHostResolver,
|
||||
EmbeddedDnsServer embeddedDnsServer) {
|
||||
EmbeddedDnsServer embeddedDnsServer,
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver) {
|
||||
this.config = config;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.embeddedDnsServer = embeddedDnsServer;
|
||||
this.currentContainerNetworkResolver = currentContainerNetworkResolver;
|
||||
}
|
||||
|
||||
public ContainerBuilder(EmulatorConfig config, DockerHostResolver dockerHostResolver,
|
||||
EmbeddedDnsServer embeddedDnsServer) {
|
||||
this(config, dockerHostResolver, embeddedDnsServer, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +62,7 @@ public class ContainerBuilder {
|
||||
* @return a new Builder instance
|
||||
*/
|
||||
public Builder newContainer(String image) {
|
||||
return new Builder(image, config, dockerHostResolver, embeddedDnsServer);
|
||||
return new Builder(image, config, dockerHostResolver, embeddedDnsServer, currentContainerNetworkResolver);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,6 +73,7 @@ public class ContainerBuilder {
|
||||
private final EmulatorConfig config;
|
||||
private final DockerHostResolver dockerHostResolver;
|
||||
private final EmbeddedDnsServer embeddedDnsServer;
|
||||
private final CurrentContainerNetworkResolver currentContainerNetworkResolver;
|
||||
|
||||
private String name;
|
||||
private final List<String> env = new ArrayList<>();
|
||||
@@ -83,11 +92,13 @@ public class ContainerBuilder {
|
||||
private final List<String> dnsServers = new ArrayList<>();
|
||||
|
||||
Builder(String image, EmulatorConfig config, DockerHostResolver dockerHostResolver,
|
||||
EmbeddedDnsServer embeddedDnsServer) {
|
||||
EmbeddedDnsServer embeddedDnsServer,
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver) {
|
||||
this.image = image;
|
||||
this.config = config;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.embeddedDnsServer = embeddedDnsServer;
|
||||
this.currentContainerNetworkResolver = currentContainerNetworkResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,10 +213,13 @@ public class ContainerBuilder {
|
||||
* This is the standard pattern for Floci container services.
|
||||
*/
|
||||
public Builder withDockerNetwork(Optional<String> serviceNetwork) {
|
||||
serviceNetwork
|
||||
Optional<String> configuredNetwork = serviceNetwork
|
||||
.or(() -> config.services().dockerNetwork())
|
||||
.filter(n -> !n.isBlank())
|
||||
.ifPresent(n -> this.networkMode = n);
|
||||
.or(() -> currentContainerNetworkResolver != null
|
||||
? currentContainerNetworkResolver.resolveNetworkName()
|
||||
: Optional.empty());
|
||||
configuredNetwork.ifPresent(n -> this.networkMode = n);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
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.ContainerNetwork;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Resolves the Docker network used by the Floci container itself.
|
||||
*
|
||||
* <p>When Floci runs in Docker and launches sibling containers through the
|
||||
* mounted Docker socket, those siblings must join the same Docker network to
|
||||
* reach Floci's in-container Runtime API and service endpoints.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class CurrentContainerNetworkResolver {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CurrentContainerNetworkResolver.class);
|
||||
|
||||
private static final String HOSTNAME_FILE = "/etc/hostname";
|
||||
|
||||
private final DockerClient dockerClient;
|
||||
private final ContainerDetector containerDetector;
|
||||
|
||||
private volatile Optional<CurrentContainerNetwork> cachedNetwork;
|
||||
|
||||
@Inject
|
||||
public CurrentContainerNetworkResolver(DockerClient dockerClient, ContainerDetector containerDetector) {
|
||||
this.dockerClient = dockerClient;
|
||||
this.containerDetector = containerDetector;
|
||||
}
|
||||
|
||||
public Optional<String> resolveNetworkName() {
|
||||
return resolve().map(CurrentContainerNetwork::name);
|
||||
}
|
||||
|
||||
public Optional<String> resolveContainerIp() {
|
||||
return resolve().map(CurrentContainerNetwork::ipAddress);
|
||||
}
|
||||
|
||||
Optional<CurrentContainerNetwork> resolve() {
|
||||
Optional<CurrentContainerNetwork> cached = cachedNetwork;
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
cachedNetwork = detect();
|
||||
return cachedNetwork;
|
||||
}
|
||||
|
||||
private Optional<CurrentContainerNetwork> detect() {
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
String containerId = currentContainerId();
|
||||
if (containerId.isBlank()) {
|
||||
LOG.debug("Could not determine current Docker container id");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
try {
|
||||
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
|
||||
Map<String, ContainerNetwork> networks = inspect.getNetworkSettings().getNetworks();
|
||||
if (networks == null || networks.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Optional<CurrentContainerNetwork> selected = selectNetwork(networks);
|
||||
selected.ifPresent(network -> LOG.infov(
|
||||
"Detected current Docker network for spawned containers: {0} ({1})",
|
||||
network.name(), network.ipAddress()));
|
||||
return selected;
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not inspect current Docker container {0}: {1}", containerId, e.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<CurrentContainerNetwork> selectNetwork(Map<String, ContainerNetwork> networks) {
|
||||
return networks.entrySet().stream()
|
||||
.filter(entry -> isUsable(entry.getValue()))
|
||||
.filter(entry -> isUserDefinedNetwork(entry.getKey()))
|
||||
.findFirst()
|
||||
.or(() -> networks.entrySet().stream()
|
||||
.filter(entry -> isUsable(entry.getValue()))
|
||||
.findFirst())
|
||||
.map(entry -> new CurrentContainerNetwork(entry.getKey(), entry.getValue().getIpAddress()));
|
||||
}
|
||||
|
||||
private boolean isUsable(ContainerNetwork network) {
|
||||
return network != null && network.getIpAddress() != null && !network.getIpAddress().isBlank();
|
||||
}
|
||||
|
||||
private boolean isUserDefinedNetwork(String networkName) {
|
||||
return !"bridge".equals(networkName) && !"host".equals(networkName) && !"none".equals(networkName);
|
||||
}
|
||||
|
||||
String currentContainerId() {
|
||||
try {
|
||||
return Files.readString(Path.of(HOSTNAME_FILE)).trim();
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not read {0}: {1}", HOSTNAME_FILE, e.getMessage());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
record CurrentContainerNetwork(String name, String ipAddress) {
|
||||
}
|
||||
}
|
||||
+17
-1
@@ -24,11 +24,18 @@ public class DockerHostResolver {
|
||||
private final AtomicBoolean ufwHintLogged = new AtomicBoolean(false);
|
||||
|
||||
private final ContainerDetector containerDetector;
|
||||
private final CurrentContainerNetworkResolver currentContainerNetworkResolver;
|
||||
|
||||
@Inject
|
||||
public DockerHostResolver(EmulatorConfig config, ContainerDetector containerDetector) {
|
||||
public DockerHostResolver(EmulatorConfig config, ContainerDetector containerDetector,
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver) {
|
||||
this.config = config;
|
||||
this.containerDetector = containerDetector;
|
||||
this.currentContainerNetworkResolver = currentContainerNetworkResolver;
|
||||
}
|
||||
|
||||
public DockerHostResolver(EmulatorConfig config, ContainerDetector containerDetector) {
|
||||
this(config, containerDetector, null);
|
||||
}
|
||||
|
||||
public String resolve() {
|
||||
@@ -39,6 +46,15 @@ public class DockerHostResolver {
|
||||
}
|
||||
|
||||
if (containerDetector.isRunningInContainer()) {
|
||||
if (currentContainerNetworkResolver != null) {
|
||||
java.util.Optional<String> currentNetworkIp = currentContainerNetworkResolver.resolveContainerIp();
|
||||
if (currentNetworkIp.isPresent()) {
|
||||
LOG.infov("Running in Docker — using current network IP for Runtime API: {0}",
|
||||
currentNetworkIp.get());
|
||||
return currentNetworkIp.get();
|
||||
}
|
||||
}
|
||||
|
||||
// Use this container's own IP so Lambda containers on the same network
|
||||
// can reach the Runtime API server bound to all interfaces inside this container.
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user