Module P-1·30 min read

Container lifecycle, the PID 1 problem in Node.js, SIGTERM vs SIGKILL, and container inspection tools.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

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

  1. docker create <image>: Creates a writable container layer over the specified image and prepares it for execution. It does not start the process.
  2. docker start <container>: Starts the process inside an existing, stopped container.
  3. docker run <image>: A convenience command. It is identical to running docker create followed immediately by docker start.
  4. docker stop <container>: Sends a graceful SIGTERM signal to the container's main process, waits a grace period (default 10 seconds), and then sends a forceful SIGKILL if the process hasn't exited.
  5. docker restart <container>: Executes a stop followed immediately by a start.

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: Like always, but if you manually run docker stop, it will not auto-restart when the Docker daemon reboots. This is generally the recommended policy for production services.

Example:

bash

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:

  1. Reaping Zombie Processes: It must clean up child processes that have finished executing.
  2. 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:

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 & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.