mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ecr): fixing port and network issues on ecr (#483)
* fix(ecr): fixing port and network issues on ecr * fix: issue on merge with main * fix(ecr): connect containers to network after start only when port bindings exist
This commit is contained in:
1 parent
37dc19925f
commit
e9f6417713
6 files changed
+95
-16
No files matched your search
+41
-6
@@ -95,6 +95,22 @@ public class ContainerLifecycleManager {
|
||||
dockerClient.startContainerCmd(containerId).exec();
|
||||
LOG.infov("Started container {0}", containerId);
|
||||
|
||||
// For containers with port bindings, withNetworkMode was skipped during creation
|
||||
// (it suppresses port publishing on macOS Docker Desktop). Connect to the
|
||||
// configured network now, after the host port bindings are established.
|
||||
if (spec.networkMode() != null && !spec.networkMode().isBlank() && spec.hasPortBindings()) {
|
||||
try {
|
||||
dockerClient.connectToNetworkCmd()
|
||||
.withContainerId(containerId)
|
||||
.withNetworkId(spec.networkMode())
|
||||
.exec();
|
||||
LOG.debugv("Connected container {0} to network {1}", containerId, spec.networkMode());
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Could not connect container {0} to network {1}: {2}",
|
||||
containerId, spec.networkMode(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve endpoints
|
||||
Map<Integer, EndpointInfo> endpoints = resolveEndpoints(containerId, spec);
|
||||
|
||||
@@ -266,8 +282,11 @@ public class ContainerLifecycleManager {
|
||||
hostConfig.withPortBindings(ports);
|
||||
}
|
||||
|
||||
// Network mode
|
||||
if (spec.networkMode() != null && !spec.networkMode().isBlank()) {
|
||||
// Network mode: only set during creation when there are no host port bindings.
|
||||
// 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()) {
|
||||
hostConfig.withNetworkMode(spec.networkMode());
|
||||
}
|
||||
|
||||
@@ -303,13 +322,17 @@ public class ContainerLifecycleManager {
|
||||
Map<Integer, EndpointInfo> endpoints = new HashMap<>();
|
||||
|
||||
for (int containerPort : spec.exposedPorts()) {
|
||||
endpoints.put(containerPort, resolveEndpoint(inspect, containerPort));
|
||||
endpoints.put(containerPort, resolveEndpoint(inspect, containerPort, spec.networkMode()));
|
||||
}
|
||||
|
||||
return endpoints;
|
||||
}
|
||||
|
||||
private EndpointInfo resolveEndpoint(InspectContainerResponse inspect, int containerPort) {
|
||||
return resolveEndpoint(inspect, containerPort, null);
|
||||
}
|
||||
|
||||
private EndpointInfo resolveEndpoint(InspectContainerResponse inspect, int containerPort, String preferredNetwork) {
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
// Native mode: use localhost and the bound host port
|
||||
var bindings = inspect.getNetworkSettings().getPorts().getBindings();
|
||||
@@ -322,15 +345,27 @@ public class ContainerLifecycleManager {
|
||||
// Fallback to container port
|
||||
return new EndpointInfo("localhost", containerPort);
|
||||
} else {
|
||||
// Container mode: use container IP on the docker network
|
||||
String containerIp = resolveContainerIp(inspect);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
private String resolveContainerIp(InspectContainerResponse inspect) {
|
||||
private String resolveContainerIp(InspectContainerResponse inspect, String preferredNetwork) {
|
||||
var networks = inspect.getNetworkSettings().getNetworks();
|
||||
if (networks != null) {
|
||||
// Prefer the configured network so that when the container is on both
|
||||
// bridge (default) and the service network, we return the right IP.
|
||||
if (preferredNetwork != null && networks.containsKey(preferredNetwork)) {
|
||||
String ip = networks.get(preferredNetwork).getIpAddress();
|
||||
if (ip != null && !ip.isBlank()) {
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
// Fall back to any network
|
||||
for (Map.Entry<String, ContainerNetwork> entry : networks.entrySet()) {
|
||||
String ip = entry.getValue().getIpAddress();
|
||||
if (ip != null && !ip.isBlank()) {
|
||||
|
||||
+8
-3
@@ -196,10 +196,15 @@ public class EcrRegistryManager {
|
||||
specBuilder.withBind(hostPersistentPath, internalMountPath);
|
||||
env.add("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=" + internalMountPath + "/ecr/registry");
|
||||
} else {
|
||||
// Host path bind-mount
|
||||
// Host path bind-mount.
|
||||
// normalize() eliminates any "./" segments produced by toAbsolutePath() on a
|
||||
// relative config path (e.g. "./data/ecr" → "/app/./data/ecr") so the
|
||||
// replace() matches reliably.
|
||||
String dataPath = Paths.get(config.services().ecr().dataPath(), "registry")
|
||||
.toAbsolutePath().toString();
|
||||
String hostDataPath = dataPath.replace(config.storage().persistentPath(), hostPersistentPath);
|
||||
.toAbsolutePath().normalize().toString();
|
||||
String persistentPath = Paths.get(config.storage().persistentPath())
|
||||
.toAbsolutePath().normalize().toString();
|
||||
String hostDataPath = dataPath.replace(persistentPath, hostPersistentPath);
|
||||
if (!inContainer) {
|
||||
ensureDataDir();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user