Kubernetes restarting pods - node.js

I have containerized node js app running on kubernetes which has volume mounted to host.
For development purpose when there is any change in the host volume dir / files the node app should restart.
In Dockerfile i have
CMD ["forever", "index.js"]
This will just start the app when container starts, but it is not restarting when the change occurs.
I have cross checked and made sure that changes are syncing properly from host volume to container

forever needs a flag to restart on file changes. Try with:
CMD ["forever", "-w", "index.js"]
I tend to use nodemon in development because it watches file changes by default and won't try to restart the app if it fails (only a file change triggers a start), forever will try to restart forever.

Related

Debug dockerized nodejs application on startup

I have a setup of containers running ( docker-compose ) and one with a nodejs application running inside of it. Currently i debug the application by connecting via VS Code to the debug port (9229) of the application. The problem with this approach is that i can't connect to the application on startup. If the error is on some event like an http connection that is no problem, but if i want to check the initialisation process the process is already running for some time until i can connect so the process ran past my debug points.
Is there a solution to this?
Run the following commands to find the running container and navigate into the container...
List all Docker images: docker image ls
View contents of a running Docker container: docker exec -it <container-id> bash
once inside the container, then you can stop the node process inside the container and start by node app.js where you will be able to see the logs from initialisation Or if you have a logs file then there aswell you can check.
The basic idea here is to navigate inside the docker container and then its like running node server like how you would run normally from ay linux terminal.

File system events not triggered in docker container when files in mounted volume are changed on the host

I would like to use nodemon to restart my project when its files are changed. I think nodemon works by listening for inotify events to trigger reloading a node.js project.
The project runs in a docker container, and the project files are in a mounted volume.
When the project files are edited from inside the docker container, for example
docker-compose exec dev vim server.js
nodemon works correctly and restarts the server.
However, when an editor running on the host machine is used, nodemon does not pick up the changes and restart the program.
The contents of the files in the docker container do in fact change, so I suspect editing files this way just doesn't trigger an FS event.
Is it possible to set this up so that editing files on the host machine causes file system events to occur in the Docker container? Why does this not happen already?
Platform Info:
Docker for Windows (Hyper-V)
node docker container
WebStorm -- Host based editor
It looks like file system events just don't work when Docker is running in Hyper-V and the changes happen on the host. But, it's possible to work around that limitation by enabling polling in nodemon:
nodemon -L server.js
In WebStorm the full command that ends up getting used is
docker-compose run dev node node_packages/nodemon/bin/nodemon.js -L server.js
More info:
https://github.com/remy/nodemon#application-isnt-restarting

Can I use restarting docker instead restarting app in container?

I run app in Docker container. I didn't bundle the app's code into image, but using -v to map the code into container in order to upgrade the code more conveniently.
I used to use pm2 to manage the process, and when I upgrade the code, I use docker exec -it app bash to go into the container and run pm2 restart.
But now I didn't use pm2 any more, just run node app.js. When I upgraded the code and need to restart the app, I run docker restart to restart the container directly.
If there is any side effect of docker restart? Or is there a better way to restart a node app?
Doing a docker restart will just restart the node process in your container, not much. So there's no side effect.

Live reload Node.js dev environment with Docker

I'm trying to work on a dev environment with Node.js and Docker.
I want to be able to:
run my docker container when I boot my computer once and for all;
make changes in my local source code and see the changes without interacting with the docker container (with a mount).
I've tried the Node image and, if I understand correctly, it is not what I'm looking for.
I know how to make the mount point, but I'm missing how the server is supposed to detect the changes and "relaunch" itself.
I'm new to Node.js so if there is a better way to do things, feel free to share.
run my docker container when I boot my computer once and for all;
start containers automatically with the docker daemon or with your process manager
make changes in my local source code and see the changes without
interacting with the docker container (with a mount).
You need to mount your dev app folder as a volume
$ docker run --name myapp -v /app/src:/app image/app
and set in your Dockerfile nodeJs
CMD ["nodemon", "-L", "/app"]

Nodemon Doesn't Restart in Windows Docker Environment

My goal is to set up a Docker container that automatically restarts a NodeJS server when file changes are detected from the host machine.
I have chosen nodemon to watch the files for changes.
On Linux and Mac environments, nodemon and docker are working flawlessly.
However, when I am in a Windows environment, nodemon doesn't restart the server.
The files are updated on the host machine, and are linked using the volumes parameter in my docker-compose.yml file.
I can see the files have changed when I run docker exec <container-name> cat /path/to/fileChanged.js. This way I know the files are being linked correctly and have been modified in the container.
Is there any reason why nodemon doesn't restart the server for Windows?
Use nodemon --legacy-watch to poll for file changes instead of listening to file system events.
VirtualBox doesn't pass file system events over the vboxfs share to your Linux VM. If you're using Docker for Windows, it would appear HyperV doesn't propagate file system events either.
As a 2021 side note, Docker for Mac/Windows new GRPCfuse file system for mounting local files into the VM should send file system events across now.
2022 note: Looks like Windows/WSL Docker doesn't share FS events to the Linux VM (see comments #Mohamed Mirghani and #Ryan Wheale and github issue).
It is simple, according to the doc you must change:
nodemon server.js
to:
nodemon --legacy-watch server.js
As mentioned by others, using node --legacy-watch will work, however, the default polling rate is quite taxing on your cpu. In my case, it was consuming 30% of my CPU just by looping through all the files in my project. I would advise you to specify the polling interval as mention by #Sandokan El Cojo.
You can do so by either adding "pollingInterval": 4000 (4 seconds in this example) to your nodemon.json file or specifying it with the -P or --polling-interval flag in the command.
This was an issue in the docker for Windows. Now it's fixed
https://www.docker.com/blog/new-filesharing-implementation-in-docker-desktop-windows/

Resources