- Beginner
- Containers and images
How it works
The main process runs as PID 1 in the container. It receives the stop signal of docker stop and must handle it. A server has to stay in the foreground: a service that puts itself in the background returns at once, and the container exits.
What breaks
With the shell form CMD node server.js, Docker starts /bin/sh -c, and the shell does not pass SIGTERM on to node: docker stop waits 10 seconds then kills everything, cutting the requests in flight. The exec form CMD ["node", "server.js"] makes node itself PID 1.
In a terminal
docker run -d --name web nginx:1.27
docker top web
docker inspect -f '{{.Config.Cmd}}' web
See it break, then fix it
Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.