All posts
Article · 1 min ·

Docker: Init Systems (PID 1)

Why does CTRL+C not kill your Docker container sometimes?

The PID 1 Problem

In Linux, PID 1 is the init system (Systemd). It has special responsibilities:
1. Reaping zombie processes.
2. Handling signals (SIGTERM, SIGINT).

When you run CMD ["python", "app.py"], Python becomes PID 1.
Python is not designed to be an init system. It might ignore SIGTERM.

The Solution: Tini

Use tini or --init flag in Docker. It inserts a tiny init process as PID 1, which spawns your app as PID 2. Tini handles the signals and forwards them correctly.

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["python", "app.py"]

Conclusion

Respect the Linux process hierarchy, even inside containers.

Related posts