Please start by reading BEST-PRACTICES.md which summarizes some of the core principles to respect when running containerized applications.
All the current examples are using Node.js but keep in mind that they are valid for most platforms running code.
If anyone is interested in providing other examples for the JVM, Python, Go, Rust... Feel free to submit PRs :)
To follow the article with examples, you can use the three provided Dockerfile.
-
Docker
needs to be up and running on the host machine. -
Use
./nodejs/docker-build.sh
to build the three images:node-shell-form
,node-exec-form
,node-with-tini
. -
Then just
docker run <image>
.
docker run node-shell-form
- Then access the container
docker exec -it node-shell-form /bin/sh
- In the container shell, run
ps aux
- Observe
/bin/sh
being PID1 - Run
docker stop <node-shell-form-container-id>
- Observe the Node.js script not receiving the SIGTERM signal hence keeping running until Docker sends SIGKILL after 10 seconds.
docker run node-exec-form
- Then access the container
docker exec -it <node-exec-form-container-id> /bin/sh
- In the container shell, run
ps aux
- Observe
Node.js
being PID1 - Run
docker stop <node-exec-form-container-id>
- Observe the Node.js script receiving the SIGTERM signal hence allowing the graceful shutdown
docker run node-with-tini
- Then access the container
docker exec -it <node-with-tini-container-id> /bin/sh
- In the container shell, run
ps aux
- Observe
Tini
being PID1 - Run
docker stop <node-with-tini-container-id>
- Observe Tini receiving the SIGTERM signal and forwarding it to Node.js hence allowing the graceful shutdown