mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-27 19:17:13 +08:00
PR remarks
This commit is contained in:
@ -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)
|
||||
|
||||
|
||||
@ -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`
|
||||
|
||||
Reference in New Issue
Block a user