Skip to content

Commit 44ca674

Browse files
authored
Merge pull request #76 from yubiuser/multi-stage
Switch to multi-stage build
2 parents f2270f4 + 20bc29a commit 44ca674

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

Dockerfile

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
FROM node:18-alpine3.17
2-
ENV NODE_ENV=production
1+
FROM node:22-alpine3.19 as builder
32
WORKDIR /usr/src/app
4-
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
5-
RUN npm install --production --silent
6-
RUN mv node_modules ../
7-
COPY . .
3+
COPY ["package.json", "package-lock.json*", "index.js", "./"]
4+
RUN npm install --omit=dev
5+
6+
FROM alpine:3.19 as deploy
7+
RUN apk add --no-cache nodejs
8+
COPY --from=builder /usr/src/app /app
9+
WORKDIR /app
810
EXPOSE 3000
911
HEALTHCHECK --interval=10s --timeout=5s --retries=3 --start-period=5s CMD wget --spider http://localhost:8000 > /dev/null || exit 1
10-
CMD ["npm", "start"]
12+
CMD ["node", "index.js"]

index.js

+43-40
Original file line numberDiff line numberDiff line change
@@ -118,58 +118,61 @@ async function list(){
118118
docker.listContainers(opts, function(err, containers) {
119119
// check for changes in status (first run is populating data only)
120120
let newConArray = [];
121-
containers.forEach(c => {
122-
// if label_enable is false then exclude any specifically false labelled containers
123-
if(LABEL_ENABLE=='false' && JSON.stringify(c.Labels).includes('"monocker.enable":"false"')){
124-
if(isFirstRun==true){
125-
console.log(' - Excluding: ' + c.Names[0].replace("/",""));
126-
//send('Excluding: ' + c.Names[0].replace("/",""));
127-
messages += 'Excluding: ' + c.Names[0].replace("/","") + "\r\n";
128-
}
129-
}
130-
else{
131-
// If label_enable is true, list the specifically included containers
132-
if(LABEL_ENABLE=='true' && JSON.stringify(c.Labels).includes('"monocker.enable":"true"')){
133-
if(isFirstRun==true){
134-
console.log(' - Monitoring: ' + c.Names[0].replace("/",""));
135-
//send('Monitoring: ' + c.Names[0].replace("/",""));
136-
messages += 'Monitoring: ' + c.Names[0].replace("/","") + "\r\n";
121+
if (containers > 0) {
122+
containers.forEach(c => {
123+
// if label_enable is false then exclude any specifically false labelled containers
124+
if (LABEL_ENABLE == 'false' && JSON.stringify(c.Labels).includes('"monocker.enable":"false"')) {
125+
if (isFirstRun == true) {
126+
console.log(' - Excluding: ' + c.Names[0].replace("/", ""));
127+
//send('Excluding: ' + c.Names[0].replace("/",""));
128+
messages += 'Excluding: ' + c.Names[0].replace("/", "") + "\r\n";
137129
}
138130
}
139-
// determine if covered by healthcheck
140-
let hcStatus = "";
141-
if(c.Status.includes("(healthy)")) hcStatus="(healthy)"
142-
if(c.Status.includes("(unhealthy)")) hcStatus="(unhealthy)"
143-
if(monContainers.includes(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus) == false && monContainers.length !== 0 ){
144-
// exclude exited status if set
145-
if(EXCLUDE_EXITED == 'true' && c.State.toLocaleLowerCase() == 'exited'){
146-
// ignore
131+
else {
132+
// If label_enable is true, list the specifically included containers
133+
if (LABEL_ENABLE == 'true' && JSON.stringify(c.Labels).includes('"monocker.enable":"true"')) {
134+
if (isFirstRun == true) {
135+
console.log(' - Monitoring: ' + c.Names[0].replace("/", ""));
136+
//send('Monitoring: ' + c.Names[0].replace("/",""));
137+
messages += 'Monitoring: ' + c.Names[0].replace("/", "") + "\r\n";
138+
}
147139
}
148-
else{
149-
// if only offline is set, then only show state changes that are offline
150-
var output = c.Names[0].replace("/","") + ": " + c.State + " " + hcStatus;
151-
if(SHA.toLowerCase()=='true'){
152-
output += " " + c.ImageID
140+
// determine if covered by healthcheck
141+
let hcStatus = "";
142+
if (c.Status.includes("(healthy)")) hcStatus = "(healthy)"
143+
if (c.Status.includes("(unhealthy)")) hcStatus = "(unhealthy)"
144+
if (monContainers.includes(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus) == false && monContainers.length !== 0) {
145+
// exclude exited status if set
146+
if (EXCLUDE_EXITED == 'true' && c.State.toLocaleLowerCase() == 'exited') {
147+
// ignore
153148
}
154-
if(ONLY_OFFLINE_STATES=='true'){
155-
if(offlineStates.includes(c.State) || offlineStates.includes(c.State + " " + hcStatus)){
149+
else {
150+
// if only offline is set, then only show state changes that are offline
151+
var output = c.Names[0].replace("/", "") + ": " + c.State + " " + hcStatus;
152+
if (SHA.toLowerCase() == 'true') {
153+
output += " " + c.ImageID
154+
}
155+
if (ONLY_OFFLINE_STATES == 'true') {
156+
if (offlineStates.includes(c.State) || offlineStates.includes(c.State + " " + hcStatus)) {
157+
console.log(" - " + output);
158+
//send(output);
159+
messages += output + "\r\n";
160+
}
161+
}
162+
else {
156163
console.log(" - " + output);
157164
//send(output);
165+
console.log('*****' + output);
158166
messages += output + "\r\n";
159167
}
160168
}
161-
else{
162-
console.log(" - " + output);
163-
//send(output);
164-
console.log('*****' + output);
165-
messages += output+ "\r\n";
166-
}
167169
}
170+
// create new container array
171+
newConArray.push(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus);
168172
}
169-
// create new container array
170-
newConArray.push(c.Id + "," + c.State + "," + c.Names[0] + "," + hcStatus);
171173
}
172-
});
174+
);
175+
}
173176
if(isFirstRun==true){
174177
console.log(" - Currently monitoring " + newConArray.length + " (running) containers");
175178
if(DISABLE_STARTUP_MSG.toLowerCase()!='true'){

0 commit comments

Comments
 (0)