diff --git a/nodejs-mini-message-board/.dockerignore b/nodejs-mini-message-board/.dockerignore index 8f94bde..c5757e6 100644 --- a/nodejs-mini-message-board/.dockerignore +++ b/nodejs-mini-message-board/.dockerignore @@ -1,3 +1,2 @@ node_modules/ Dockerfile -package-lock.json diff --git a/nodejs-mini-message-board/Dockerfile b/nodejs-mini-message-board/Dockerfile index c22d00e..b619451 100644 --- a/nodejs-mini-message-board/Dockerfile +++ b/nodejs-mini-message-board/Dockerfile @@ -1,15 +1,17 @@ FROM node:23-alpine -COPY package.json package-lock.json /app +COPY package*.json /app/package*.json WORKDIR /app RUN npm install . -COPY --chown nobody:nobody ./src /app +COPY --chown=nobody:nobody ./src /app/src EXPOSE 3000 +USER nobody + CMD ["node", "src/app.js"] diff --git a/nodejs-mini-message-board/src/app.js b/nodejs-mini-message-board/src/app.js index b8d6151..5d9ea3a 100644 --- a/nodejs-mini-message-board/src/app.js +++ b/nodejs-mini-message-board/src/app.js @@ -18,6 +18,19 @@ app.use(express.urlencoded({ extended: true })); app.use("/", indexRouter); //app.use("/new", msgRouter); -app.listen(port, () => { +const server = app.listen(port, () => { console.log(`Webserver running on ${port}.`); }); + +// Shutdown Logic +const gracefulShutdownHandler = (signal) => { + console.log(`Caught ${signal}, gracefully shutting down`); + + server.close(() => { + console.log(`Shutting down server`); + process.exit(); + }); +}; + +process.on("SIGINT", gracefulShutdownHandler); +process.on("SIGTERM", gracefulShutdownHandler);