diff --git a/src/pages/api/docker/stats/[...service].js b/src/pages/api/docker/stats/[...service].js index f5212aa6c..0258bc68d 100644 --- a/src/pages/api/docker/stats/[...service].js +++ b/src/pages/api/docker/stats/[...service].js @@ -14,7 +14,8 @@ export default async function handler(req, res) { } try { - const docker = new Docker(getDockerArguments(containerServer)); + const dockerArgs = getDockerArguments(containerServer) + const docker = new Docker(dockerArgs.conn); const containers = await docker.listContainers({ all: true, }); @@ -41,27 +42,30 @@ export default async function handler(req, res) { return; } - // Try with a service deployed in Docker Swarm - const tasks = await docker.listTasks({ - filters: { - service: [containerName], - // A service can have several offline containers, so we only look for an active one. - 'desired-state': ['running'] - } - }).catch(() => []); + // Try with a service deployed in Docker Swarm, if enabled + if (dockerArgs.swarm) { + const tasks = await docker.listTasks({ + filters: { + service: [containerName], + // A service can have several offline containers, so we only look for an active one. + "desired-state": ["running"], + }, + }) + .catch(() => []); - // For now we are only interested in the first one (in case replicas > 1). - // TODO: Show the result for all replicas/containers? - const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID + // For now we are only interested in the first one (in case replicas > 1). + // TODO: Show the result for all replicas/containers? + const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID; - if (taskContainerId) { - const container = docker.getContainer(taskContainerId); - const stats = await container.stats({ stream: false }); + if (taskContainerId) { + const container = docker.getContainer(taskContainerId); + const stats = await container.stats({ stream: false }); - res.status(200).json({ - stats, - }); - return; + res.status(200).json({ + stats, + }); + return; + } } res.status(200).send({ diff --git a/src/pages/api/docker/status/[...service].js b/src/pages/api/docker/status/[...service].js index 71635d1e6..fa54e6f3b 100644 --- a/src/pages/api/docker/status/[...service].js +++ b/src/pages/api/docker/status/[...service].js @@ -13,7 +13,8 @@ export default async function handler(req, res) { } try { - const docker = new Docker(getDockerArguments(containerServer)); + const dockerArgs = getDockerArguments(containerServer); + const docker = new Docker(dockerArgs.conn); const containers = await docker.listContainers({ all: true, }); @@ -35,30 +36,33 @@ export default async function handler(req, res) { return res.status(200).json({ status: info.State.Status, - health: info.State.Health?.Status + health: info.State.Health?.Status, }); } - const tasks = await docker.listTasks({ - filters: { - service: [containerName], - // A service can have several offline containers, we only look for an active one. - 'desired-state': ['running'] - } - }).catch(() => []); + if (dockerArgs.swarm) { + const tasks = await docker.listTasks({ + filters: { + service: [containerName], + // A service can have several offline containers, we only look for an active one. + "desired-state": ["running"], + }, + }) + .catch(() => []); - // For now we are only interested in the first one (in case replicas > 1). - // TODO: Show the result for all replicas/containers? - const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID + // For now we are only interested in the first one (in case replicas > 1). + // TODO: Show the result for all replicas/containers? + const taskContainerId = tasks.at(0)?.Status?.ContainerStatus?.ContainerID; - if (taskContainerId) { - const container = docker.getContainer(taskContainerId); - const info = await container.inspect(); + if (taskContainerId) { + const container = docker.getContainer(taskContainerId); + const info = await container.inspect(); - return res.status(200).json({ - status: info.State.Status, - health: info.State.Health?.Status - }); + return res.status(200).json({ + status: info.State.Status, + health: info.State.Health?.Status, + }); + } } return res.status(200).send({ diff --git a/src/utils/config/docker.js b/src/utils/config/docker.js index 9aef74086..ada0c031e 100644 --- a/src/utils/config/docker.js +++ b/src/utils/config/docker.js @@ -22,11 +22,11 @@ export default function getDockerArguments(server) { if (servers[server]) { if (servers[server].socket) { - return { socketPath: servers[server].socket }; + return { conn: { socketPath: servers[server].socket }, swarm: servers[server].swarm }; } if (servers[server].host) { - return { host: servers[server].host, port: servers[server].port || null }; + return { conn: { host: servers[server].host, port: servers[server].port || null }, swarm: servers[server].swarm }; } return servers[server]; diff --git a/src/utils/config/service-helpers.js b/src/utils/config/service-helpers.js index e0a9f23f1..46bda80fb 100644 --- a/src/utils/config/service-helpers.js +++ b/src/utils/config/service-helpers.js @@ -44,7 +44,7 @@ export async function servicesFromDocker() { const serviceServers = await Promise.all( Object.keys(servers).map(async (serverName) => { - const docker = new Docker(getDockerArguments(serverName)); + const docker = new Docker(getDockerArguments(serverName).conn); const containers = await docker.listContainers({ all: true, }); @@ -167,4 +167,4 @@ export default async function getServiceWidget(group, service) { } return false; -} \ No newline at end of file +}