I have a Node.js application deployed in Kubernetes and my docker file looks like the following:
FROM node:8.9.4
RUN mkdir -p /opt/amy-app-folder
RUN chown -R node:node /opt
WORKDIR /opt/my-app-folder
COPY dist /opt/my-app-folder
RUN npm install pm2 -g
EXPOSE 8000
USER node
CMD ["pm2-docker", "bin/www.js"]
This was working fine until I recently did a deployment for the application. I have configured the CI/CD job in such a way that it will build and deploy the application once I merge the PR to the development
branch. When I checked the pods, it is showing CrashLoopBackOff
as the status of the pod in which my app is running.
I checked the logs using the command
$kubectl logs -f my-app-name-qa-d8744bf4f-sh6ks -n wea-qa --tail 1000
It is showing the pm2
has been crashed with the following error message
2022-03-21T11:50:46: PM2 log: App [www:0] starting in -fork mode-
2022-03-21T11:50:46: PM2 log: App [www:0] online
/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:30
import(url.pathToFileURL(process.env.pm_exec_path));
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:607:28)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
2022-03-21T11:50:46: PM2 log: App [www:0] exited with code [1] via signal [SIGINT]
2022-03-21T11:50:46: PM2 log: Script /opt/my-appl/bin/www.js had too many unstable restarts (16). Stopped. "errored"
2022-03-21T11:50:50: PM2 log: 0 application online, retry = 3
2022-03-21T11:50:52: PM2 log: 0 application online, retry = 2
2022-03-21T11:50:54: PM2 log: 0 application online, retry = 1
2022-03-21T11:50:56: PM2 log: 0 application online, retry = 0
2022-03-21T11:50:56: PM2 log: Stopping app:www id:0
2022-03-21T11:50:56: PM2 error: app=www id=0 does not have a pid
2022-03-21T11:50:57: PM2 log: PM2 successfully stopped
The only change I have made in the code is to add a console.log
and it is removed later. Now, this is the staging
version of my application and I checked the code with the master
branch, which is running fine in production. There is no change in the code and so I am stuck here and don't know what to do next. As the pod is stopped, I can't go inside the pod and run commands. I redeployed the code again, but still, the pod is starting, and then it is crashed.
I am able to run the code in my local machine and there I am not using Kubernetes or Docker. My node version in local is 12.0.0
Can someone help me to fix this issue? Thanks in advance.
Here's what I think it's happening with your app:
...but with Nodejs not recognizing import
when trying to run pm2
.
The nodejs version v8.x probably does not support importing modules this way.
Well, Docker is installing the latest version of pm2
to run your app here:
RUN npm install pm2 -g
Therefore, when you added something to your code, the new build must have downloaded a new version of pm2
, one not compatible with that nodejs version. This seems to be similar to this answer: https://stackoverflow.com/a/59840576/1971120
From this answer (another question), I would suggest you to upgrade nodejs to +v13 (using a LTS version would be ideal), but in case you can not do that, stick with a compatible pm2 version in the docker descriptor like this:
RUN npm install pm2@x.y.z -g
Where x.y.z
is a compatible version.