69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
const Docker = require('dockerode');
|
|
const os = require('os');
|
|
|
|
const docker = new Docker();
|
|
|
|
// Helper to detect the host IP (e.g. 192.168.x.x)
|
|
function getHostIP() {
|
|
const interfaces = os.networkInterfaces();
|
|
for (const iface of Object.values(interfaces)) {
|
|
for (const net of iface) {
|
|
if (net.family === 'IPv4' && !net.internal && net.address.startsWith('192.')) {
|
|
return net.address;
|
|
}
|
|
}
|
|
}
|
|
return 'localhost'; // Fallback
|
|
}
|
|
|
|
async function getContainerData() {
|
|
const containers = await docker.listContainers({ all: true });
|
|
const hostname = os.hostname();
|
|
const hostIp = getHostIP();
|
|
|
|
const output = [];
|
|
|
|
for (const containerInfo of containers) {
|
|
const container = docker.getContainer(containerInfo.Id);
|
|
const inspect = await container.inspect();
|
|
|
|
const rawName = inspect.Name || containerInfo.Names?.[0] || '';
|
|
const name = rawName.replace(/^\//, '');
|
|
|
|
const networks = inspect.NetworkSettings.Networks || {};
|
|
let staticIP = null;
|
|
|
|
// Try to find a meaningful internal Docker-assigned static IP
|
|
for (const netName in networks) {
|
|
const ip = networks[netName].IPAddress;
|
|
if (ip && (ip.startsWith('192.') || ip.startsWith('10.'))) {
|
|
staticIP = ip;
|
|
break;
|
|
}
|
|
}
|
|
|
|
const rawPorts = Object.values(inspect.NetworkSettings.Ports || {})
|
|
.flat()
|
|
.filter(Boolean);
|
|
const ports = rawPorts.map(p => p.HostPort);
|
|
|
|
// Prefer static IP if available, fallback to host IP
|
|
let url = '(none)';
|
|
if (ports.length > 0) {
|
|
const accessIP = staticIP || hostIp;
|
|
url = `http://${accessIP}:${ports[0]}`;
|
|
}
|
|
|
|
output.push({
|
|
name,
|
|
ip: staticIP || 'N/A',
|
|
ports,
|
|
hostname,
|
|
url,
|
|
});
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
module.exports = { getContainerData }; |