Container lifecycle, the PID 1 problem in Node.js, SIGTERM vs SIGKILL, and container inspection tools.
Introduction
You have built a highly optimized, multi-stage Node.js image. Now you need to run it.
In this module, we transition from Image Engineering to Container Operations. We will explore the container lifecycle, how to inspect running processes, and most importantly, the infamous PID 1 Problem that plagues almost every Node.js container in production.
The Container Lifecycle
A container is not a permanent fixture. It is ephemeral. Understanding its lifecycle is crucial for production operations.
Core Lifecycle Commands
docker create <image>: Creates a writable container layer over the specified image and prepares it for execution. It does not start the process.docker start <container>: Starts the process inside an existing, stopped container.docker run <image>: A convenience command. It is identical to runningdocker createfollowed immediately bydocker start.docker stop <container>: Sends a gracefulSIGTERMsignal to the container's main process, waits a grace period (default 10 seconds), and then sends a forcefulSIGKILLif the process hasn't exited.docker restart <container>: Executes astopfollowed immediately by astart.
Restart Policies
What happens if your Node.js application throws an unhandled exception and crashes at 3:00 AM?
If you didn't configure a restart policy, the container stops, and your API goes offline until you manually restart it.
You can configure Docker to automatically resurrect dead containers using the --restart flag:
no: (Default) Do not automatically restart.on-failure: Restart only if the container exits with a non-zero exit code (a crash).always: Always restart the container if it stops, regardless of the exit code. If the Docker daemon restarts, the container will also restart.unless-stopped: Likealways, but if you manually rundocker stop, it will not auto-restart when the Docker daemon reboots. This is generally the recommended policy for production services.
Example:
The PID 1 Problem
This is the most common architectural mistake in JavaScript containers.
When you run a container, the command you specify (e.g., CMD ["npm", "start"]) becomes Process ID 1 (PID 1) inside the container's PID Namespace.
In Linux, PID 1 has two very special, hardcoded responsibilities:
- Reaping Zombie Processes: It must clean up child processes that have finished executing.
- Signal Forwarding: It must properly handle termination signals (
SIGTERM,SIGINT) from the operating system and pass them to child processes.
Why npm start is Dangerous
Look at this common Dockerfile:
If you run this, npm becomes PID 1. The npm process then spawns node server.js as a child process.
Sign in to keep reading
The rest of this module is free — sign in with Google to unlock it and track your progress.
Sign in & RegisterDiscussion
0Join the discussion