How I saved memory In Docker Using Tini

By Hamza
DockerTiniProcess ManagementLinuxContainer ManagementDevOps

When running applications in Docker containers, you may encounter processes that don't terminate properly. These can be zombie processes (defunct but not reaped), orphaned processes (parent died), or child processes that simply hang. In Linux, when a child process exits, it becomes a "zombie" until the parent calls wait() to read its exit status. If the parent doesn't do this, zombies accumulate. In Docker, the main container process runs as PID 1, which has special responsibilities including adopting orphaned processes.


It started as a subtle issue. My Change Monitor application was running fine in Docker, but after a few hours, memory usage spiked and I had no idea why. Upon investigation, there were dozens of Chrome-related processes hanging around, even though my Puppeteer code was supposed to close the browser after each check. They weren't exactly "zombie" processes in the strict Linux sense, but they were definitely stuck - consuming resources and refusing to die. Every time my app checked a website, it spawned chrome and chrome_crashpad processes. When the check finished, most would close, but some always remained behind.


The Root Cause


After digging through Docker documentation and Linux process management, I realized the issue: my Node.js application was running as PID 1 in the container. In Linux, PID 1 has special responsibilities. It's supposed to adopt orphaned processes and reap dead ones. But my Node.js app wasn't designed for this. It was just trying to run my monitoring code and unable to kill those processes properly.


The solution to this problem was Tini, a tiny init system designed specifically for containers. It acts as PID 1 and handles all the process management stuff that my app wasn't doing. It comes already with Docker and to activate it you have to pass the --init flag in Docker run but I was Docker compose so I just decided to add it to my Dockerfile.


# Install Tini & Set tini as init proces
RUN apt-get update && apt-get install -y tini
 
ENTRYPOINT ["/usr/bin/tini", "--"]

These 2 lines of code solved my memory issues and since then my application has been consuming least amount of memory even after running 100s of monitor checks with Puppeteer.