Docker container doesn't restart after nodejs app crashes? - node.js

So i have a running container with node js app inside of it (express). The docker container has restart: always policy and when the app crashes, the container doesn't pick up that crash so it doesn't restart. My question is how do i make the container notice that my app crashed? Or do i use extra libraries like forever or pm2(which will keep my app running from INSIDE container) or how do i handle this specific case?

Related

Azure app service doesn't install docker properly

I am currently trying to start ElasticSearch on an Azure app service using Docker. I install docker through the ssh available in azure app services. Docker seem to install alright in the console, however when I run
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.6.2
I get the following error in the ssh console:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
I have installed and uninstall Docker several times, however I still get the same error
Azure App service does not allow you to run Elastic Search due to its limitations
You may use Elastic as a Service on Azure or install it in AKS or VM.
https://azuremarketplace.microsoft.com/en-us/marketplace/apps/elastic.ec-azure?tab=Overview
You are trying to install Docker inside the Docker container
App Service for Linux comes with a bunch of preconfigured containers such as Node, PHP, Java, Python, Ruby and .NET Core.
https://anthonychu.ca/post/jekyll-azure-app-service-linux/
The exact issue you mentioned means that Docker daemon is not started in your Linux environment
To start the Docker daemon use command:
systemctl start docker

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.

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.

How to deploy docker node.js application onto Amazon EC2 with Amazon EC2 container service

I have built a node.js application in the docker, every time when I need to run it, I just run the docker and run the command node app.js.
I have set it up on Amazon EC2, but in a vanilla way by register and log into the Amazon EC2 instance, pull the docker image, then run and log into the docker and run the command node app.js.
Now, since Amazon has this EC2 container service, I was told that I can do these two things automatically:
EC2 runs the docker
Docker runs node app.js
The advantage of doing this is that whenever either docker is crashed or the app is crashed, both of them are crashed, therefore that EC2 can automatically run the command again and rescue them.
How can I set this function up?
It comes by default when you set up an ECS task. Make sure the task is marked as 'essential' in your task's container and that you have at least one task requested in your ECS service, and it will automatically restart a failed/crashed container for you.

Kubernetes restarting pods

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.

Resources