diff --git a/Dockerfile b/Dockerfile index a537e16..19c805e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ -# Use Alpine Linux as base image -FROM node:slim +# Use Debian as build OS (https://github.com/nodejs/docker-node/issues/1973) +FROM node:slim AS BUILD # Set default env variable values ENV PORT=3000 @@ -21,11 +21,22 @@ COPY package*.json ./ # Install dependencies RUN npm i --verbose +# Use Alpine Linux as base image +FROM node:alpine + +WORKDIR /app + +# Copy from the build stage +COPY --from=0 . . + # Copy the rest of the application code COPY . . # Expose the port on which the Node.js application will run EXPOSE 3000 +# Configure healthcheck +HEALTHCHECK --interval=1m --timeout=5s CMD node healtcheck.js || exit 1 + # Command to run the Node.js application CMD ["node", "server.js"] diff --git a/healthcheck.js b/healthcheck.js new file mode 100644 index 0000000..a200975 --- /dev/null +++ b/healthcheck.js @@ -0,0 +1,7 @@ +require('dotenv').config(); + +fetch(`http://localhost:${process.env.PORT}/healthcheck`).then(res => { + return process.exit(res.status === 204 ? 0:1); +}).catch(() => { + process.exit(1); +}); diff --git a/package.json b/package.json index 3c500c6..3cb54de 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "main": "server.js", "private": true, "scripts": { - "start": "node server.js" + "start": "node server.js", + "health": "node healthcheck.js" }, "keywords": [ "unifi", diff --git a/server.js b/server.js index 8f81631..ec9823a 100644 --- a/server.js +++ b/server.js @@ -115,6 +115,10 @@ app.get('/img/bg.jpg', async (req, res) => { }); }); +app.get('/healthcheck', (req,res)=> { + res.sendStatus(204) +}) + // Serve static files app.use(express.static('./node_modules/bootstrap-icons/font/')); app.use(express.static('./node_modules/@fontsource/'));