https://github.com/trugamr/docker-node-graceful-exit
Gracefully exit node app when docker container stops
https://github.com/trugamr/docker-node-graceful-exit
docker graceful-shutdown health-check nodejs
Last synced: about 1 month ago
JSON representation
Gracefully exit node app when docker container stops
- Host: GitHub
- URL: https://github.com/trugamr/docker-node-graceful-exit
- Owner: Trugamr
- License: mit
- Created: 2023-04-17T15:36:34.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-17T15:36:42.000Z (about 2 years ago)
- Last Synced: 2025-05-01T10:53:40.728Z (about 1 month ago)
- Topics: docker, graceful-shutdown, health-check, nodejs
- Language: Dockerfile
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Node.js App with Graceful Shutdown
This is a sample Node.js app that demonstrates how to gracefully stop the app when a Docker container is stopped.
## Building and Running the Docker Container
To build and run the Docker container, use the following commands:
```bash
# Building the Docker Image
docker build -t docker-node-graceful-exit .# Running the Docker Container
docker run -p 3000:3000 --name node-app docker-node-graceful-exit
```This will start the container and map port 3000 from the container to port 3000 on your host machine.
## Testing Graceful Shutdown
You can test the graceful shutdown of the Node.js app by manually stopping the Docker container using the `docker stop` command.
By default, Docker waits for 10 seconds for a container to shut down cleanly after sending it a `SIGTERM` signal before forcefully terminating it. However, this Node.js app has a signal handler that listens for the `SIGTERM` signal and performs a graceful shutdown, closing any open connections and releasing any resources before exiting.
To manually stop the container and test the graceful shutdown, use the following command:
```bash
docker stop node-app
```This will send a `SIGTERM` signal to the container and give it a few seconds to gracefully shut down. However, since the Node.js app handles the `SIGTERM` signal and performs a graceful shutdown, it should stop much faster than the default 10 seconds that Docker waits before forcefully terminating the container.
That's it! This sample project demonstrates how to test the graceful shutdown of a Node.js app when a Docker container is stopped, using the `docker stop` command to give the app a few seconds to shut down cleanly. You can use this approach in your own projects to ensure that your app shuts down cleanly and doesn't leave any resources hanging when running in a containerized environment.