From 01ff3a75c8d3df47ccc596eeaeaa43b72051a63f Mon Sep 17 00:00:00 2001 From: Kevyn Bruyere Date: Wed, 29 Jul 2020 09:02:28 +0200 Subject: [PATCH] PR remarks --- README.md | 2 +- sections/docker/bootstrap-using-node.md | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ffc47c45..229990e4 100644 --- a/README.md +++ b/README.md @@ -1097,7 +1097,7 @@ Bear in mind that with the introduction of the new V8 engine alongside the new E **TL;DR:** use `CMD ['node','server.js']` to start your app. This prevents problems with child-process, signal handling and avoid creating unnecessary processes. -**Otherwise:** You'll have hard shutdowns, possibly losing current requests and/or data +**Otherwise:** When no signals are passed in you'll have hard shutdowns, possibly losing current requests and/or data [**Read More: Bootstrap container using node command, avoid npm start**](/sections/docker/bootstrap-using-node.md) diff --git a/sections/docker/bootstrap-using-node.md b/sections/docker/bootstrap-using-node.md index e636471d..be2e1d9b 100644 --- a/sections/docker/bootstrap-using-node.md +++ b/sections/docker/bootstrap-using-node.md @@ -2,18 +2,26 @@ ## One paragraph explainer -We are used to see code examples where folks start their app using `CMD 'npm start'`. This is a bad practice. The `npm` binary will not forward signals to your app which prevents graceful shutdown (see #716). If you are using Child-processes they won’t be cleaned up correctly in case of unexpected shutdown, leaving zombie processes on your host. `npm start` also results in having an extra process for no benefit. To start you app use `CMD ['node','server.js']`. +We are used to see code examples where folks start their app using `CMD 'npm start'`. This is a bad practice. The `npm` binary will not forward signals to your app which prevents graceful shutdown (see [/sections/docker/graceful-shutdown.md]). If you are using Child-processes they won’t be cleaned up correctly in case of unexpected shutdown, leaving zombie processes on your host. `npm start` also results in having an extra process for no benefit. To start you app use `CMD ['node','server.js']`. If your app spawns child-processes also use `TINI` as an entrypoint. ### Code example ```dockerfile FROM node:12-slim AS build + +# Add Tini if using child-processes +ENV TINI_VERSION v0.19.0 +ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini +RUN chmod +x /tini + WORKDIR /usr/src/app COPY package.json package-lock.json ./ RUN npm ci --production && npm clean cache --force -CMD ['node', 'server.js'] +ENTRYPOINT ["/tini", "--"] + +CMD ["node", "server.js"] ``` ### Antipatterns @@ -27,7 +35,7 @@ COPY package.json package-lock.json ./ RUN npm ci --production && npm clean cache --force # don’t do that! -CMD “npm start” +CMD "npm start" ``` Using node in a single string will start a bash/ash shell process to execute your command. That is almost the same as using `npm`