buildpacks
buildpacks copied to clipboard
Improve documentation for Nodejs developers who wish to catch SIGTERM
Hello!
found a problem with a simple nodejs application deployed to Cloud Run. The problem is that the Cloud Run container never receives the SIGTERM event on instance Graceful shutdown
see https://github.com/dimmetrius/cloudRunHello
step 1 build container: npm build step 2 deploy container: npm deploy
commands used: gcloud --project ${PROJECT} builds submit --pack=image=gcr.io/${PROJECT}/cloudrunhello,env=GOOGLE_RUNTIME="nodejs"
gcloud --project ${PROJECT} run deploy cloudrunhello --image=gcr.io/${PROJECT}/cloudrunhello --region=us-central1 --platform=managed
If we compare deploying this sample with Dockerfile and with Buildpacks
The node app is able to catch the SIGTERM signal when using Dockerfile but not Builpacks, this is why we believe this is an issue with buildpacks
@dimmetrius this is a long standing issue with NPM. By default the nodejs buildpacks will run your app by calling npm run start
. Unfortunately this causes npm
to swallow SIGTERM. You can override the default startup process by including a Procfile with a web
process that invokes node
directly. So, for your sample app, the Procfile would be:
web: node index.js
@matthewrobertson Thank you! After Procfile adding the application started to receive SIGTERM signal
Thank you so much for helping resolve our issue!!
In the interest of being charitable to others who inevitably will stub their toe against this also, is there a good way of suggesting a feature request for the nodejs buildpack team or at least having a prominent findable FAQ that addresses the problem and one-line fix?
I suggest we re-open this issue and only close it when either:
- GCP Buildpacks have a better default experience that would result in the SIGTERM signal not being swallowed when possible
- the use of Procfile for Node.js startup commands is better documented
@steren I agree on 2. I have re-opened the issue.
On 1, I don't think we can change the default behaviour as npm start
has a bunch of side effects that customers might be relying on (eg running prestart
scripts). Changing this now would be a breaking change.
Thanks Steren & Matthew. I wonder; could a build-time or run-time warning be issued when building or running a nodejs image with no Procfile?
"WARNING: No Procfile detected. Will assume 'npm start'; signals like SIGTERM may go untrapped by your script. See https://X.Y.Z/ for more info"