mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
fix(docker): let the daemon pick dynamic host ports (#5928)
A dynamic port binding sent a port Floci picked with ServerSocket(0), probed where Floci runs. On Docker Desktop the daemon binds it inside the VM, whose ephemeral range lies inside the Mac's, so a VM socket on that number failed the start with "address already in use" and CreateDBInstance returned 500. The binding now leaves the host port empty and the endpoint is read back from inspect, as before.
This commit is contained in:
1 parent
e38dba5f11
commit
8972b21e1b
16 files changed
+72
-85
No files matched your search
+10
-15
@@ -90,7 +90,6 @@ public class ContainerLifecycleManager {
|
||||
private final DockerClient dockerClient;
|
||||
private final ImageCacheService imageCacheService;
|
||||
private final ContainerDetector containerDetector;
|
||||
private final PortAllocator portAllocator;
|
||||
private final EmulatorConfig config;
|
||||
|
||||
/** Volumes whose shared-ownership root has already been initialised this process (run-once guard). */
|
||||
@@ -103,12 +102,10 @@ public class ContainerLifecycleManager {
|
||||
public ContainerLifecycleManager(DockerClient dockerClient,
|
||||
ImageCacheService imageCacheService,
|
||||
ContainerDetector containerDetector,
|
||||
PortAllocator portAllocator,
|
||||
EmulatorConfig config) {
|
||||
this.dockerClient = dockerClient;
|
||||
this.imageCacheService = imageCacheService;
|
||||
this.containerDetector = containerDetector;
|
||||
this.portAllocator = portAllocator;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@@ -1272,14 +1269,9 @@ public class ContainerLifecycleManager {
|
||||
for (Map.Entry<Integer, Integer> entry : spec.portBindings().entrySet()) {
|
||||
int containerPort = entry.getKey();
|
||||
int hostPort = entry.getValue();
|
||||
|
||||
// 0 means dynamic allocation
|
||||
if (hostPort == 0) {
|
||||
hostPort = portAllocator.allocateAny();
|
||||
}
|
||||
|
||||
ports.bind(ExposedPort.tcp(containerPort), bindingFor(spec, containerPort, hostPort));
|
||||
LOG.debugv("Port binding: {0} -> {1}", String.valueOf(containerPort), String.valueOf(hostPort));
|
||||
LOG.debugv("Port binding: {0} -> {1}", String.valueOf(containerPort),
|
||||
hostPort == 0 ? "dynamic" : String.valueOf(hostPort));
|
||||
}
|
||||
hostConfig.withPortBindings(ports);
|
||||
} else if (spec.hasPortBindings()) {
|
||||
@@ -1348,17 +1340,20 @@ public class ContainerLifecycleManager {
|
||||
* form of the same decision, for a port that is an implementation detail and must never leave
|
||||
* the host. A port in neither binds every interface, which is Docker's own default and what
|
||||
* every caller that does not ask has always got.
|
||||
*
|
||||
* <p>A host port of 0 leaves the port to the daemon, and the endpoint is read back from
|
||||
* inspect after start. A port Floci picked itself would be probed where Floci runs, not where
|
||||
* the daemon binds it: on Docker Desktop that is the VM, whose ephemeral range lies inside the
|
||||
* Mac's, so a VM socket on the same number fails the start with "address already in use".
|
||||
*/
|
||||
private static Ports.Binding bindingFor(ContainerSpec spec, int containerPort, int hostPort) {
|
||||
String hostIp = spec.portBindingHostIps() == null
|
||||
? null
|
||||
: spec.portBindingHostIps().get(containerPort);
|
||||
if (hostIp != null && !hostIp.isBlank()) {
|
||||
return Ports.Binding.bindIpAndPort(hostIp, hostPort);
|
||||
if (hostIp == null || hostIp.isBlank()) {
|
||||
hostIp = spec.loopbackPortBindings().contains(containerPort) ? LOOPBACK_HOST_IP : null;
|
||||
}
|
||||
return spec.loopbackPortBindings().contains(containerPort)
|
||||
? Ports.Binding.bindIpAndPort(LOOPBACK_HOST_IP, hostPort)
|
||||
: Ports.Binding.bindPort(hostPort);
|
||||
return new Ports.Binding(hostIp, hostPort == 0 ? null : String.valueOf(hostPort));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,9 +18,10 @@ import java.util.concurrent.ConcurrentSkipListSet;
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
/**
|
||||
* Utility for allocating free TCP ports for Docker container port bindings.
|
||||
* Consolidates the free port discovery logic previously duplicated across
|
||||
* container managers (RDS, ElastiCache, MSK, ECR).
|
||||
* Allocates host ports from a configured range for callers that must know the port before
|
||||
* the container starts, or that hand it out themselves (ECR, EC2, MSK and other range-based
|
||||
* managers). A binding that only needs some free port uses a dynamic port instead
|
||||
* ({@code ContainerBuilder.Builder#withDynamicPort}), which leaves the choice to the daemon.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class PortAllocator {
|
||||
@@ -142,24 +143,6 @@ public class PortAllocator {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds any free TCP port using ephemeral port allocation.
|
||||
* This is the fastest method when any port will do.
|
||||
*
|
||||
* @return a free port
|
||||
* @throws RuntimeException if no free port can be allocated
|
||||
*/
|
||||
public int allocateAny() {
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
socket.setReuseAddress(true);
|
||||
int port = socket.getLocalPort();
|
||||
LOG.debugv("Allocated ephemeral port {0}", String.valueOf(port));
|
||||
return port;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Could not find a free port", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a specific port is currently free.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user