From f194abf55216810a0d73f2f77874cf70294e87cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Sun, 11 Jun 2023 10:43:31 +0200 Subject: [PATCH 1/3] Switch to multi-stage build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- Dockerfile | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index dfdb105..fcaf1ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,12 @@ -FROM node:18-alpine3.17 -ENV NODE_ENV=production +FROM node:18-alpine3.18 as builder WORKDIR /usr/src/app -COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"] -RUN npm install --production --silent -RUN mv node_modules ../ -COPY . . +COPY ["package.json", "package-lock.json*", "index.js", "./"] +RUN npm install --omit=dev + +FROM alpine:3.18 as deploy +RUN apk add --no-cache nodejs +COPY --from=builder /usr/src/app /app +WORKDIR /app EXPOSE 3000 HEALTHCHECK --interval=10s --timeout=5s --retries=3 --start-period=5s CMD wget --spider http://localhost:8000 > /dev/null || exit 1 -CMD ["npm", "start"] +CMD ["node", "index.js"] From b5289e0e9d5217c23f856ab97f3c3fefe9ef59a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Tue, 14 May 2024 22:05:45 +0200 Subject: [PATCH 2/3] Update node and alpine version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index fcaf1ff..2db20ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ -FROM node:18-alpine3.18 as builder +FROM node:22-alpine3.19 as builder WORKDIR /usr/src/app COPY ["package.json", "package-lock.json*", "index.js", "./"] RUN npm install --omit=dev -FROM alpine:3.18 as deploy +FROM alpine:3.19 as deploy RUN apk add --no-cache nodejs COPY --from=builder /usr/src/app /app WORKDIR /app From 20bc29adf558b0a047e5b3263b5a0ce6f172ba86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Tue, 14 May 2024 22:33:25 +0200 Subject: [PATCH 3/3] Don't error if containers is empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- index.js | 83 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/index.js b/index.js index 980e92e..6b08aca 100644 --- a/index.js +++ b/index.js @@ -118,58 +118,61 @@ async function list(){ docker.listContainers(opts, function(err, containers) { // check for changes in status (first run is populating data only) let newConArray = []; - containers.forEach(c => { - // if label_enable is false then exclude any specifically false labelled containers - if(LABEL_ENABLE=='false' && JSON.stringify(c.Labels).includes('"monocker.enable":"false"')){ - if(isFirstRun==true){ - console.log(' - Excluding: ' + c.Names[0].replace("/","")); - //send('Excluding: ' + c.Names[0].replace("/","")); - messages += 'Excluding: ' + c.Names[0].replace("/","") + "\r\n"; - } - } - else{ - // If label_enable is true, list the specifically included containers - if(LABEL_ENABLE=='true' && JSON.stringify(c.Labels).includes('"monocker.enable":"true"')){ - if(isFirstRun==true){ - console.log(' - Monitoring: ' + c.Names[0].replace("/","")); - //send('Monitoring: ' + c.Names[0].replace("/","")); - messages += 'Monitoring: ' + c.Names[0].replace("/","") + "\r\n"; + if (containers > 0) { + containers.forEach(c => { + // if label_enable is false then exclude any specifically false labelled containers + if (LABEL_ENABLE == 'false' && JSON.stringify(c.Labels).includes('"monocker.enable":"false"')) { + if (isFirstRun == true) { + console.log(' - Excluding: ' + c.Names[0].replace("/", "")); + //send('Excluding: ' + c.Names[0].replace("/","")); + messages += 'Excluding: ' + c.Names[0].replace("/", "") + "\r\n"; } } - // determine if covered by healthcheck - let hcStatus = ""; - if(c.Status.includes("(healthy)")) hcStatus="(healthy)" - if(c.Status.includes("(unhealthy)")) hcStatus="(unhealthy)" - if(monContainers.includes(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus) == false && monContainers.length !== 0 ){ - // exclude exited status if set - if(EXCLUDE_EXITED == 'true' && c.State.toLocaleLowerCase() == 'exited'){ - // ignore + else { + // If label_enable is true, list the specifically included containers + if (LABEL_ENABLE == 'true' && JSON.stringify(c.Labels).includes('"monocker.enable":"true"')) { + if (isFirstRun == true) { + console.log(' - Monitoring: ' + c.Names[0].replace("/", "")); + //send('Monitoring: ' + c.Names[0].replace("/","")); + messages += 'Monitoring: ' + c.Names[0].replace("/", "") + "\r\n"; + } } - else{ - // if only offline is set, then only show state changes that are offline - var output = c.Names[0].replace("/","") + ": " + c.State + " " + hcStatus; - if(SHA.toLowerCase()=='true'){ - output += " " + c.ImageID + // determine if covered by healthcheck + let hcStatus = ""; + if (c.Status.includes("(healthy)")) hcStatus = "(healthy)" + if (c.Status.includes("(unhealthy)")) hcStatus = "(unhealthy)" + if (monContainers.includes(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus) == false && monContainers.length !== 0) { + // exclude exited status if set + if (EXCLUDE_EXITED == 'true' && c.State.toLocaleLowerCase() == 'exited') { + // ignore } - if(ONLY_OFFLINE_STATES=='true'){ - if(offlineStates.includes(c.State) || offlineStates.includes(c.State + " " + hcStatus)){ + else { + // if only offline is set, then only show state changes that are offline + var output = c.Names[0].replace("/", "") + ": " + c.State + " " + hcStatus; + if (SHA.toLowerCase() == 'true') { + output += " " + c.ImageID + } + if (ONLY_OFFLINE_STATES == 'true') { + if (offlineStates.includes(c.State) || offlineStates.includes(c.State + " " + hcStatus)) { + console.log(" - " + output); + //send(output); + messages += output + "\r\n"; + } + } + else { console.log(" - " + output); //send(output); + console.log('*****' + output); messages += output + "\r\n"; } } - else{ - console.log(" - " + output); - //send(output); - console.log('*****' + output); - messages += output+ "\r\n"; - } } + // create new container array + newConArray.push(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus); } - // create new container array - newConArray.push(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus); } - }); + ); + } if(isFirstRun==true){ console.log(" - Currently monitoring " + newConArray.length + " (running) containers"); if(DISABLE_STARTUP_MSG.toLowerCase()!='true'){